Module: Instagram::Client::Subscriptions
- Included in:
- Instagram::Client
- Defined in:
- lib/instagram/client/subscriptions.rb
Overview
Defines methods related to real-time
Instance Method Summary collapse
-
#create_subscription(*args) ⇒ Object
Creates a real-time subscription.
-
#delete_subscription(*args) ⇒ Object
Deletes a real-time subscription.
-
#meet_challenge(params, verify_token = nil) { ... } ⇒ Object
As a security measure (to prevent DDoS attacks), Instagram sends a verification request to your server after you request a subscription.
-
#process_subscription(json, &block) ⇒ nil
Process a subscription notification JSON payload.
-
#subscriptions(options = {}) ⇒ Hashie::Mash
Returns a list of active real-time subscriptions.
-
#validate_update(body, headers) ⇒ Object
Public: As a security measure, all updates from Instagram are signed using X-Hub-Signature: sha1=XXXX where XXX is the sha1 of the json payload using your application secret as the key.
Instance Method Details
#create_subscription(options = {}) ⇒ Object #create_subscription(object, callback_url, aspect = "media", options = {}) ⇒ Hashie::Mash
Creates a real-time subscription
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/instagram/client/subscriptions.rb', line 56 def create_subscription(*args) = args.last.is_a?(Hash) ? args.pop : {} object = args.shift callback_url = args.shift aspect = args.shift .tap {|o| o[:object] = object unless object.nil? o[:callback_url] = callback_url unless callback_url.nil? o[:aspect] = aspect || o[:aspect] || "media" } response = post("subscriptions", .merge(:client_secret => client_secret)) response end |
#delete_subscription(options = {}) ⇒ Object #delete_subscription(subscription_id, options = {}) ⇒ Hashie::Mash
Deletes a real-time subscription
91 92 93 94 95 96 97 |
# File 'lib/instagram/client/subscriptions.rb', line 91 def delete_subscription(*args) = args.last.is_a?(Hash) ? args.pop : {} subscription_id = args.first .merge!(:id => subscription_id) if subscription_id response = delete("subscriptions", .merge(:client_secret => client_secret)) response end |
#meet_challenge(params, verify_token = nil) { ... } ⇒ Object
As a security measure (to prevent DDoS attacks), Instagram sends a verification request to your server after you request a subscription. This method parses the challenge params and makes sure the call is legitimate.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/instagram/client/subscriptions.rb', line 112 def 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 Instagram # 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 |
#process_subscription(json, &block) ⇒ nil
Process a subscription notification JSON payload
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/instagram/client/subscriptions.rb', line 179 def process_subscription(json, ={}, &block) raise ArgumentError, "callbacks block expected" unless block_given? if .has_key?(:signature) if !client_secret raise ArgumentError, "client_secret must be set during configure" end digest = OpenSSL::Digest::Digest.new('sha1') verify_signature = OpenSSL::HMAC.hexdigest(digest, client_secret, json) if [:signature] != verify_signature raise Instagram::InvalidSignature, "invalid X-Hub-Signature does not match verify signature against client_secret" end end payload = MultiJson.decode(json) @changes = Hash.new { |h,k| h[k] = [] } for change in payload @changes[change['object']] << change end block.call(self) end |
#subscriptions(options = {}) ⇒ Hashie::Mash
Returns a list of active real-time subscriptions
20 21 22 23 |
# File 'lib/instagram/client/subscriptions.rb', line 20 def subscriptions(={}) response = get("subscriptions", .merge(:client_secret => client_secret)) response end |
#validate_update(body, headers) ⇒ Object
Public: As a security measure, all updates from Instagram are signed using X-Hub-Signature: sha1=XXXX where XXX is the sha1 of the json payload using your application secret as the key.
Example: # in Rails controller def receive_update if Instagram.validate_update(request.body, headers) ... else render text: "not authorized", status: 401 end end
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/instagram/client/subscriptions.rb', line 141 def validate_update(body, headers) unless client_secret raise ArgumentError, "client_secret must be set during configure" end if request_signature = headers['X-Hub-Signature'] || headers['HTTP_X_HUB_SIGNATURE'] and signature_parts = request_signature.split('sha1=') request_signature = signature_parts[1] calculated_signature = OpenSSL::HMAC.hexdigest('sha1', client_secret, body) calculated_signature == request_signature end end |