This repository has been archived by the owner on Mar 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Facebook Medium based on the koala gem
- Loading branch information
Marcin Bunsch
committed
Jun 1, 2011
1 parent
3e02e0f
commit 099545e
Showing
5 changed files
with
103 additions
and
19 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters