Class: Stealth::Services::Facebook::Client
- Inherits:
-
BaseClient
- Object
- BaseClient
- Stealth::Services::Facebook::Client
- Defined in:
- lib/stealth/services/facebook/client.rb
Constant Summary collapse
- FB_ENDPOINT =
"https://graph.facebook.com/v2.10/me"
Instance Attribute Summary collapse
-
#api_endpoint ⇒ Object
readonly
Returns the value of attribute api_endpoint.
-
#reply ⇒ Object
readonly
Returns the value of attribute reply.
Class Method Summary collapse
- .fetch_profile(recipient_id:, fields: nil) ⇒ Object
- .track(recipient_id:, metric:, value:, options: {}) ⇒ Object
Instance Method Summary collapse
-
#initialize(reply:, endpoint: 'messages') ⇒ Client
constructor
A new instance of Client.
- #transmit ⇒ Object
Constructor Details
#initialize(reply:, endpoint: 'messages') ⇒ Client
Returns a new instance of Client.
19 20 21 22 23 |
# File 'lib/stealth/services/facebook/client.rb', line 19 def initialize(reply:, endpoint: 'messages') @reply = reply access_token = "access_token=#{Stealth.config.facebook.page_access_token}" @api_endpoint = [[FB_ENDPOINT, endpoint].join('/'), access_token].join('?') end |
Instance Attribute Details
#api_endpoint ⇒ Object (readonly)
Returns the value of attribute api_endpoint.
17 18 19 |
# File 'lib/stealth/services/facebook/client.rb', line 17 def api_endpoint @api_endpoint end |
#reply ⇒ Object (readonly)
Returns the value of attribute reply.
17 18 19 |
# File 'lib/stealth/services/facebook/client.rb', line 17 def reply @reply end |
Class Method Details
.fetch_profile(recipient_id:, fields: nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/stealth/services/facebook/client.rb', line 31 def self.fetch_profile(recipient_id:, fields: nil) if fields.blank? fields = [:id, :name, :first_name, :last_name, :profile_pic] end query_hash = { fields: fields.join(','), access_token: Stealth.config.facebook.page_access_token } uri = URI::HTTPS.build( host: "graph.facebook.com", path: "/#{recipient_id}", query: query_hash.to_query ) response = Faraday.get(uri.to_s) Stealth::Logger.l(topic: "facebook", message: "Requested user profile for #{recipient_id}. Response: #{response.status}: #{response.body}") if response.status.in?(200..299) MultiJson.load(response.body) else raise(Stealth::Errors::ServiceError, "Facebook error #{response.status}: #{response.body}") end end |
.track(recipient_id:, metric:, value:, options: {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/stealth/services/facebook/client.rb', line 57 def self.track(recipient_id:, metric:, value:, options: {}) metric_values = [{ '_eventName' => metric, '_valueToSum' => value }] metric_values.first.merge!() params = { event: 'CUSTOM_APP_EVENTS', custom_events: MultiJson.dump(metric_values), advertiser_tracking_enabled: 1, application_tracking_enabled: 1, extinfo: MultiJson.dump(['mb1']), page_scoped_user_id: recipient_id, page_id: Stealth.config.facebook.page_id } uri = URI::HTTPS.build( host: "graph.facebook.com", path: "/#{Stealth.config.facebook.app_id}/activities" ) response = Faraday.post(uri.to_s, params) Stealth::Logger.l(topic: "facebook", message: "Sending custom event for metric: #{metric} and value: #{value}. Response: #{response.status}: #{response.body}") if response.status.in?(200..299) MultiJson.load(response.body) else raise(Stealth::Errors::ServiceError, "Facebook error #{response.status}: #{response.body}") end end |
Instance Method Details
#transmit ⇒ Object
25 26 27 28 29 |
# File 'lib/stealth/services/facebook/client.rb', line 25 def transmit headers = { "Content-Type" => "application/json" } response = Faraday.post(api_endpoint, reply.to_json, headers) Stealth::Logger.l(topic: "facebook", message: "Transmitting. Response: #{response.status}: #{response.body}") end |