Sha256: b3bfa8bf63642c00eb8d7d45f718e750155290bfc7d91d3a418500b499358c10

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

require 'http'
require 'json'

module FansWatch
  # Service for all FB API calls
  class FbApi
    FB_URL = 'https://graph.facebook.com'
    API_VER = 'v2.8'
    FB_API_URL = URI.join(FB_URL, "#{API_VER}/")
    FB_TOKEN_URL = URI.join(FB_API_URL, 'oauth/access_token')

    attr_reader :access_token

    def initialize(client_id:, client_secret:)
      access_token_response = 
      	HTTP.get(FB_TOKEN_URL,
      					 params: { client_id: client_id,
      										 client_secret: client_secret,
      										 grant_type: 'client_credentials' })
      @access_token = JSON.load(access_token_response.to_s)['access_token'] 
    end

    def fb_resource(id)
      response = HTTP.get(
        fb_resource_url(id),
        params: {access_token: @access_token })
      JSON.load(response.to_s)
    end

    def page_info(page_id)
      fb_resource(page_id)
    end

    def posting(posting_id) 
      fb_resource(posting_id) 
    end

    def page_feed(page_id)
      feed_response = 
      	HTTP.get(URI.join(fb_resource_url(page_id), 'feed'),
                 params: { access_token: @access_token })
      JSON.load(feed_response.to_s)['data']
    end

    def posting_attachments(posting_id)
      attachments_response = 
        HTTP.get(URI.join(fb_resource_url(posting_id), 'attachments'), 
                 params: { access_token: @access_token })
        JSON.load(attachments_response.to_s)['data'].first
    end

    


    private

    def fb_resource_url(id) 
      URI.join(FB_API_URL, "/#{id}/") 
    end 
  
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fanswatch-0.0.0 lib/fanswatch/fb_api.rb