Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
First test of subscriptions
  • Loading branch information
mmangino committed Jan 13, 2011
1 parent 1893aa7 commit 046df2a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/mogli.rb
Expand Up @@ -35,3 +35,4 @@ module Mogli
require "mogli/work"
require "mogli/user"
require "mogli/client"
require "mogli/app_client"
23 changes: 23 additions & 0 deletions lib/mogli/app_client.rb
@@ -0,0 +1,23 @@
require "mogli/client/event"
require "mogli/client/user"

module Mogli
class AppClient < Client
attr_accessor :application_id

def subscription_url
"http://graph.facebook.com/#{application_id}/subscriptions"
end

def subscribe_to_model(model,options)
options_to_send = options.dup
options_to_send[:fields] = Array(options[:fields]).join(",")
options_to_send[:object] = name_for_class(model)
self.class.post(subscription_url,:body=>default_params.merge(options_to_send))
end

def name_for_class(klass)
klass.name.split("::").last.downcase
end
end
end
9 changes: 8 additions & 1 deletion lib/mogli/client.rb
Expand Up @@ -83,7 +83,9 @@ def self.create_from_session_key(session_key, client_id, secret)
def self.create_and_authenticate_as_application(client_id, secret)
authenticator = Mogli::Authenticator.new(client_id, secret, nil)
access_data = authenticator.get_access_token_for_application
new(access_data)
client = AppClient.new(access_data)
client.application_id = client_id
client
end

def post(path,klass,body_args)
Expand All @@ -95,6 +97,11 @@ def delete(path)
self.class.delete(api_path(path),:query=>default_params)
end

def subscribe_to_model(model,options)
options_to_send=options.dup
self.class.post("http://")
end

def fql_query(query,klass=nil,format="json")
data = self.class.post(fql_path,:body=>default_params.merge({:query=>query,:format=>format}))
return data unless format=="json"
Expand Down
36 changes: 36 additions & 0 deletions spec/app_client_spec.rb
@@ -0,0 +1,36 @@
require "spec_helper"
describe Mogli::AppClient do
describe "subscribing" do
let :client do
a=Mogli::AppClient.new('key')
a.application_id="123"
a
end

it "should allow you to subscribe with callback url, fields and verify token" do
Mogli::Client.should_receive(:post).with("http://graph.facebook.com/123/subscriptions",an_instance_of(Hash))
client.subscribe_to_model(Mogli::User,:callback_url=>"http://localhost:3000",:fields=>["username"])
end

it "should pass the type as the object field" do
Mogli::Client.should_receive(:post).with("http://graph.facebook.com/123/subscriptions",{:body=>hash_including(:object=>"user")})
client.subscribe_to_model(Mogli::User,:callback_url=>"http://localhost:3000",:fields=>["username"])
end

it "sends the list of fields comma separated" do
Mogli::Client.should_receive(:post).with("http://graph.facebook.com/123/subscriptions",{:body=>hash_including(:fields=>"username,friends")})
client.subscribe_to_model(Mogli::User,:callback_url=>"http://localhost:3000",:fields=>["username,friends"])
end
it "sends the callback url" do
Mogli::Client.should_receive(:post).with("http://graph.facebook.com/123/subscriptions",{:body=>hash_including(:callback_url=>"http://localhost:3000")})
client.subscribe_to_model(Mogli::User,:callback_url=>"http://localhost:3000",:fields=>["username"])
end

it "should send the verify token if provided" do
Mogli::Client.should_receive(:post).with("http://graph.facebook.com/123/subscriptions",{:body=>hash_including(:verify_token=>"TOKEN")})
client.subscribe_to_model(Mogli::User,:callback_url=>"http://localhost:3000",:fields=>["username"],:verify_token=>"TOKEN")

end
end

end

0 comments on commit 046df2a

Please sign in to comment.