MSSM services are now broken out into seperate classes, and we cleaned up posting to them

This commit is contained in:
maxwell 2010-12-08 00:27:44 -08:00
parent 2be0666d80
commit 041e747db6
11 changed files with 136 additions and 43 deletions

View file

@ -19,11 +19,12 @@ class ServicesController < ApplicationController
provider = auth['provider']
user = auth['user_info']
current_user.services.create(:nickname => user['nickname'],
service = "Services::#{provider.camelize}".constantize.new(:nickname => user['nickname'],
:access_token => toke,
:access_secret => secret,
:provider => provider,
:uid => auth['uid'])
current_user.services << service
flash[:notice] = I18n.t 'services.create.success'
if current_user.getting_started

View file

@ -13,4 +13,11 @@ class Service
key :access_secret, String
key :nickname, String
timestamps!
def public_message(length, url = "")
space_for_url = url.blank? ? 0 : (url.length + 1)
truncated = truncate(self.message, :length => (length - space_for_url))
truncated = "#{truncated} #{url}" unless url.blank?
return truncated
end
end

View file

@ -0,0 +1,10 @@
class Services::Facebook < Service
def post(message)
Rails.logger.debug("event=post_to_service type=facebook sender_id=#{self.user_id}")
begin
RestClient.post("https://graph.facebook.com/me/feed", :message => message, :access_token => self.access_token)
rescue Exception => e
Rails.logger.info("#{e.message} failed to post to facebook")
end
end
end

View file

@ -0,0 +1,21 @@
class Services::Twitter < Service
def post(message)
Rails.logger.debug("event=post_to_service type=twitter sender_id=#{self.user_id}")
twitter_key = SERVICES['twitter']['consumer_key']
twitter_consumer_secret = SERVICES['twitter']['consumer_secret']
if twitter_consumer_secret.blank? || twitter_consumer_secret.blank?
Rails.logger.info "you have a blank twitter key or secret.... you should look into that"
end
Twitter.configure do |config|
config.consumer_key = twitter_key
config.consumer_secret = twitter_consumer_secret
config.oauth_token = self.access_token
config.oauth_token_secret = self.access_secret
end
Twitter.update(message)
end
end

View file

@ -6,6 +6,7 @@ class StatusMessage < Post
include Diaspora::Socketable
include YoutubeTitles
require File.join(Rails.root, 'lib/youtube_titles')
include ActionView::Helpers::TextHelper
validates_length_of :message, :maximum => 1000, :message => "please make your status messages less than 1000 characters"
xml_name :status_message
@ -34,6 +35,14 @@ class StatusMessage < Post
XML
end
def public_message(length, url = "")
space_for_url = url.blank? ? 0 : (url.length + 1)
truncated = truncate(self.message, :length => (length - space_for_url))
truncated = "#{truncated} #{url}" unless url.blank?
return truncated
end
protected
def message_or_photos_present?

View file

@ -166,40 +166,11 @@ class User
end
self.services.each do |service|
self.send("post_to_#{service.provider}".to_sym, service, message)
service.post(message)
end
end
end
def post_to_facebook(service, message)
Rails.logger.debug("event=post_to_service type=facebook sender_handle=#{self.diaspora_handle}")
begin
RestClient.post("https://graph.facebook.com/me/feed", :message => message, :access_token => service.access_token)
rescue Exception => e
Rails.logger.info("#{e.message} failed to post to facebook")
end
end
def post_to_twitter(service, message)
Rails.logger.debug("event=post_to_service type=twitter sender_handle=#{self.diaspora_handle}")
twitter_key = SERVICES['twitter']['consumer_key']
twitter_consumer_secret = SERVICES['twitter']['consumer_secret']
if twitter_consumer_secret.blank? || twitter_consumer_secret.blank?
Rails.logger.info "you have a blank twitter key or secret.... you should look into that"
end
Twitter.configure do |config|
config.consumer_key = twitter_key
config.consumer_secret = twitter_consumer_secret
config.oauth_token = service.access_token
config.oauth_token_secret = service.access_secret
end
Twitter.update(message)
end
def post_to_hub(post)
Rails.logger.debug("event=post_to_service type=pubsub sender_handle=#{self.diaspora_handle}")

View file

@ -9,10 +9,6 @@ describe ServicesController do
let(:user) { make_user }
let!(:aspect) { user.aspects.create(:name => "lame-os") }
let!(:service1) {a = Factory(:service); user.services << a; a}
let!(:service2) {a = Factory(:service); user.services << a; a}
let!(:service3) {a = Factory(:service); user.services << a; a}
let!(:service4) {a = Factory(:service); user.services << a; a}
let(:mock_access_token) { Object.new }
@ -29,6 +25,11 @@ describe ServicesController do
end
describe '#index' do
let!(:service1) {a = Factory(:service); user.services << a; a}
let!(:service2) {a = Factory(:service); user.services << a; a}
let!(:service3) {a = Factory(:service); user.services << a; a}
let!(:service4) {a = Factory(:service); user.services << a; a}
it 'displays all connected serivices for a user' do
get :index
assigns[:services].should == user.services
@ -54,9 +55,18 @@ describe ServicesController do
post :create
response.should redirect_to services_url
end
it 'creates a twitter service' do
user.getting_started = false
request.env['omniauth.auth'] = omniauth_auth
post :create
user.services.first.class.name.should == "Services::Twitter"
end
end
describe '#destroy' do
let!(:service1) {a = Factory(:service); user.services << a; a}
it 'should destroy a service of a users with the id' do
lambda{delete :destroy, :id => service1.id.to_s}.should change(user.services, :count).by(-1)
end

View file

@ -0,0 +1,23 @@
require 'spec_helper'
describe Services::Facebook do
before do
@user = make_user
@user.aspects.create(:name => "whatever")
@post = @user.post(:status_message, :message => "hello", :to =>@user.aspects.first.id)
@service = Services::Facebook.new(:access_token => "yeah")
@user.services << @service
end
describe '#post' do
it 'posts a status message to facebook' do
RestClient.should_receive(:post).with("https://graph.facebook.com/me/feed", :message => @post.message, :access_token => @service.access_token)
@service.post(@post.message)
end
it 'swallows exception raised by facebook always being down' do
RestClient.should_receive(:post).and_raise
@service.post(@post.message)
end
end
end

View file

@ -0,0 +1,19 @@
require 'spec_helper'
describe Services::Twitter do
before do
@user = make_user
@user.aspects.create(:name => "whatever")
@post = @user.post(:status_message, :message => "hello", :to =>@user.aspects.first.id)
@service = Services::Twitter.new(:access_token => "yeah", :access_secret => "foobar")
@user.services << @service
end
describe '#post' do
it 'posts a status message to twitter' do
Twitter.should_receive(:update).with(@post.message)
@service.post(@post.message)
end
end
end

View file

@ -72,8 +72,26 @@ describe StatusMessage do
post.save!
post[:youtube_titles].should == {video_id => expected_title}
end
end
describe '#public_message' do
before do
message = ""
440.times{message << 'd'}
@status_message = @user.post(:status_message, :message => message, :to => @aspect.id)
end
it 'truncates the message' do
@status_message.public_message(140).length.should == 140
@status_message.public_message(420).length.should == 420
end
it 'has the correct length if a url is present' do
@status_message.public_message(140, "a_url_goes_here").length.should == 140
end
it 'adds the public link if present' do
@status_message.public_message(140, "/p/#{@status_message.id}").should include "/p/#{@status_message.id}"
end
end
end

View file

@ -70,9 +70,13 @@ describe User do
end
describe 'services'
describe '#dispatch_post' do
include Rails.application.routes.url_helpers
let(:status) {user.build_post(:status_message, @status_opts)}
before do
@message = "hello, world!"
@status_opts = {:to => "all", :message => @message}