Module: Koala::Facebook::RealtimeUpdateMethods
- Included in:
- RealtimeUpdates
- Defined in:
- lib/koala/realtime_updates.rb
Class Method Summary collapse
-
.included(base) ⇒ Object
note: to subscribe to real-time updates, you must have an application access token.
Instance Method Summary collapse
-
#api(*args) ⇒ Object
same as GraphAPI.
- #initialize(options = {}) ⇒ Object
- #list_subscriptions ⇒ Object
-
#subscribe(object, fields, callback_url, verify_token) ⇒ Object
subscribes for realtime updates your callback_url must be set up to handle the verification request or the subscription will not be set up developers.facebook.com/docs/api/realtime.
-
#unsubscribe(object = nil) ⇒ Object
removes subscription for object if object is nil, it will remove all subscriptions.
Class Method Details
.included(base) ⇒ Object
note: to subscribe to real-time updates, you must have an application access token
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/koala/realtime_updates.rb', line 8 def self.included(base) # make the attributes readable base.class_eval do attr_reader :app_id, :app_access_token, :secret # parses the challenge params and makes sure the call is legitimate # returns the challenge string to be sent back to facebook if true # returns false otherwise # this is a class method, since you don't need to know anything about the app # saves a potential trip fetching the app access token def self.meet_challenge(params, verify_token = nil, &verification_block) if params["hub.mode"] == "subscribe" && # you can make sure this is legitimate through two ways # if your store the token across the calls, you can pass in the token value # and we'll make sure it matches (verify_token && params["hub.verify_token"] == verify_token) || # alternately, if you sent a specially-constructed value (such as a hash of various secret values) # you can pass in a block, which we'll call with the verify_token sent by Facebook # if it's legit, return anything that evaluates to true; otherwise, return nil or false (verification_block && yield(params["hub.verify_token"])) params["hub.challenge"] else false end end end end |
Instance Method Details
#api(*args) ⇒ Object
same as GraphAPI
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/koala/realtime_updates.rb', line 77 def api(*args) # same as GraphAPI response = super(*args) do |response| # check for subscription errors if response.is_a?(Hash) && error_details = response["error"] raise APIError.new(error_details) end end response end |
#initialize(options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/koala/realtime_updates.rb', line 36 def initialize( = {}) @app_id = [:app_id] @app_access_token = [:app_access_token] @secret = [:secret] unless @app_id && (@app_access_token || @secret) # make sure we have what we need raise ArgumentError, "Initialize must receive a hash with :app_id and either :app_access_token or :secret! (received #{.inspect})" end # fetch the access token if we're provided a secret if @secret && !@app_access_token oauth = Koala::Facebook::OAuth.new(@app_id, @secret) @app_access_token = oauth.get_app_access_token end end |
#list_subscriptions ⇒ Object
73 74 75 |
# File 'lib/koala/realtime_updates.rb', line 73 def list_subscriptions api(subscription_path)["data"] end |
#subscribe(object, fields, callback_url, verify_token) ⇒ Object
subscribes for realtime updates your callback_url must be set up to handle the verification request or the subscription will not be set up developers.facebook.com/docs/api/realtime
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/koala/realtime_updates.rb', line 54 def subscribe(object, fields, callback_url, verify_token) args = { :object => object, :fields => fields, :callback_url => callback_url, :verify_token => verify_token } # a subscription is a success if Facebook returns a 200 (after hitting your server for verification) api(subscription_path, args, 'post', :http_component => :status) == 200 end |
#unsubscribe(object = nil) ⇒ Object
removes subscription for object if object is nil, it will remove all subscriptions
67 68 69 70 71 |
# File 'lib/koala/realtime_updates.rb', line 67 def unsubscribe(object = nil) args = {} args[:object] = object if object api(subscription_path, args, 'delete', :http_component => :status) == 200 end |