Skip to content
This repository has been archived by the owner on Mar 17, 2020. It is now read-only.

Commit

Permalink
Implemented Facebook Medium based on the koala gem
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Bunsch committed Jun 1, 2011
1 parent 3e02e0f commit 099545e
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 19 deletions.
19 changes: 0 additions & 19 deletions facebook_test/fb_test.rb

This file was deleted.

25 changes: 25 additions & 0 deletions lib/broadcast/media/facebook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'koala'

class Broadcast::Medium::Facebook < Broadcast::Medium::Oauth

def publish(message)
# We do not rescue any Koala exceptions to make them crash the sendout
# This should make debugging easier i.e. when fb has privilege issues

# Connect to facebook and get info about current user
graph = Koala::Facebook::GraphAPI.new(options.token)
me = graph.get_object('me')
# Get the connections to retrieve the appropriate page
connections = graph.get_connections(me['id'], 'accounts')
raise "No pages available" if connections.size == 0

# Find the page to post to
page = connections.find { |connection| connection['name'] == options.page }
raise 'Page not found' if !page

# Create a new graph so that the page posts to itself
page_graph = Koala::Facebook::GraphAPI.new(page['access_token'])
page_graph.put_wall_post(message.body)
end

end
66 changes: 66 additions & 0 deletions spec/lib/broadcast/media/facebook_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'spec_helper'

describe Broadcast::Medium::Facebook do

describe '.new' do

it "should create a new instance with options provided in config" do
medium = Broadcast::Medium::Facebook.new
medium.options.token.should == 'fb_token'
medium.options.page.should == 'My Page'
end

it "should prioritize options argument over options provided in config" do
medium = Broadcast::Medium::Facebook.new(:page => 'Different Page')
medium.options.token.should == 'fb_token'
medium.options.page.should == 'Different Page'
end

end

describe '#publish' do

before do
@medium = Broadcast::Medium::Facebook.new
@message = Broadcast::Message::SpecWithChagingContent.new
@me = {"name"=>"Your Name", "username"=>"yourname", "timezone"=>0, "gender"=>"male", "id"=>"1", "last_name"=>"Name", "updated_time"=>"2011-06-01T17:29:02+0000", "verified"=>true, "locale"=>"en_US", "hometown"=>{"name"=>"Palo Alto", "id"=>"1"}, "link"=>"http://www.facebook.com/yourname", "first_name"=>"Your"}
@connections = [{"name"=>"My Page", "category"=>"Software", "id"=>"123", "access_token"=>"page_access_token"}]
end

it "should send the message to a Facebook page" do
mock_graph = mock
mock_page_graph = mock
Koala::Facebook::GraphAPI.should_receive(:new).with('fb_token').and_return(mock_graph)
Koala::Facebook::GraphAPI.should_receive(:new).with('page_access_token').and_return(mock_page_graph)
mock_graph.should_receive(:get_object).with('me').and_return(@me)
mock_graph.should_receive(:get_connections).with('1', 'accounts').and_return(@connections)

mock_page_graph.should_receive(:put_wall_post).with(@message.body)
@medium.publish(@message)
end

it "should raise an error when no pages are available" do
mock_graph = mock
mock_page_graph = mock
Koala::Facebook::GraphAPI.should_receive(:new).with('fb_token').and_return(mock_graph)
mock_graph.should_receive(:get_object).with('me').and_return(@me)
mock_graph.should_receive(:get_connections).with('1', 'accounts').and_return([])

lambda { @medium.publish(@message) }.should raise_error('No pages available')
end

it "should raise an error when no page was not found" do
mock_graph = mock
mock_page_graph = mock
Koala::Facebook::GraphAPI.should_receive(:new).with('fb_token').and_return(mock_graph)
mock_graph.should_receive(:get_object).with('me').and_return(@me)

@connections.first['name'] = 'Different name'
mock_graph.should_receive(:get_connections).with('1', 'accounts').and_return(@connections)

lambda { @medium.publish(@message) }.should raise_error('Page not found')
end

end

end
5 changes: 5 additions & 0 deletions spec/support/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
campfire.room = 'My Room'
}

config.facebook { |facebook|
facebook.token = 'fb_token'
facebook.page = 'My Page'
}

config.irc { |irc|
irc.username = 'foo'
irc.server = 'irc.freenode.net'
Expand Down
7 changes: 7 additions & 0 deletions spec/support/examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@ def body
end
end

class Broadcast::Message::SpecWithChagingContent < Broadcast::Message
def body
"message | " + Time.now.to_s
end
end


class Broadcast::Medium::Spec < Broadcast::Medium
end

0 comments on commit 099545e

Please sign in to comment.