Sha256: fe5613ee4b5ff3cec8058f323b8c13f9ba965d3f0019406195fba0a33b466f50

Contents?: true

Size: 1.86 KB

Versions: 5

Compression:

Stored size: 1.86 KB

Contents

# coding: utf-8
# frozen_string_literal: true

require 'faraday'

require 'stealth/services/facebook/message_handler'
require 'stealth/services/facebook/reply_handler'
require 'stealth/services/facebook/setup'

module Stealth
  module Services
    module Facebook

      class Client < Stealth::Services::BaseClient
        FB_ENDPOINT = "https://graph.facebook.com/v2.10/me"

        attr_reader :api_endpoint, :reply

        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

        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

        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
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
stealth-facebook-0.11.4 lib/stealth/services/facebook/client.rb
stealth-facebook-0.11.3 lib/stealth/services/facebook/client.rb
stealth-facebook-0.11.2 lib/stealth/services/facebook/client.rb
stealth-facebook-0.11.1 lib/stealth/services/facebook/client.rb
stealth-facebook-0.11.0 lib/stealth/services/facebook/client.rb