Modularized and Tested™

This commit is contained in:
Uiri 2011-09-29 21:04:18 -04:00
parent 3aa3b9aea8
commit 0fbda5d077
2 changed files with 20 additions and 6 deletions

View file

@ -9,17 +9,21 @@ class Services::Facebook < Service
Rails.logger.debug("event=post_to_service type=facebook sender_id=#{self.user_id}")
message = public_message(post, url)
begin
if /https?:\/\/(\S+)/ =~ message
link = /https?:\/\/(\S+)/.match(message)[0]
Faraday.post("https://graph.facebook.com/me/feed", {:message => message, :link => link, :access_token => self.access_token}.to_param)
else
Faraday.post("https://graph.facebook.com/me/feed", {:message => message, :access_token => self.access_token}.to_param)
end
post_params = self.create_post_params(message)
Faraday.post("https://graph.facebook.com/me/feed", post_params.to_param)
rescue Exception => e
Rails.logger.info("#{e.message} failed to post to facebook")
end
end
def create_post_params(message)
hash = {:message => message, :access_token => self.access_token}
if /https?:\/\/(\S+)/ =~ message
hash.merge!({:link => /https?:\/\/(\S+)/.match(message)[0]})
end
return hash
end
def public_message(post, url)
super(post, MAX_CHARACTERS, url)
end

View file

@ -14,6 +14,7 @@ describe Services::Facebook do
@service.post(@post)
WebMock.should have_requested(:post, "https://graph.facebook.com/me/feed").with(:body => {:message => @post.text, :access_token => @service.access_token}.to_param)
end
it 'swallows exception raised by facebook always being down' do
stub_request(:post,"https://graph.facebook.com/me/feed").
to_raise(StandardError)
@ -26,6 +27,15 @@ describe Services::Facebook do
@service.post(@post, url)
end
end
describe '#create_post_params' do
it 'should have a link when the message has a link' do
@service.create_post_params("http://example.com/ test message")[:link].should == "http://example.com/"
end
it 'should not have a link when the message has no link' do
@service.create_post_params("test message")[:link].should == nil
end
end
context 'finder' do
before do