class FaceGroups::FbApi

Service for all FB API calls

Constants

API_VER
FB_API_URL
FB_TOKEN_URL
FB_URL
GRAPH_QUERY
TOKEN_KEY

Public Class Methods

access_token() click to toggle source
# File lib/facegroups/fb_api.rb, line 20
def self.access_token
  return @access_token if @access_token

  access_token_response = HTTP.get(
    FB_TOKEN_URL,
    params: {
      client_id: config[:client_id],
      client_secret: config[:client_secret],
      grant_type: 'client_credentials'
    }
  )
  @access_token = access_token_response.parse['access_token']
end
config() click to toggle source
# File lib/facegroups/fb_api.rb, line 38
def self.config
  return @config if @config
  @config = {
    client_id: ENV['FB_CLIENT_ID'],
    client_secret: ENV['FB_CLIENT_SECRET']
  }
end
config=(credentials) click to toggle source
# File lib/facegroups/fb_api.rb, line 34
def self.config=(credentials)
  @config ? @config.update(credentials) : @config = credentials
end
graphql_query(resource_id, resource_key) click to toggle source
# File lib/facegroups/fb_api.rb, line 54
def self.graphql_query(resource_id, resource_key)
  response = HTTP.get(
    fb_resource_url(resource_id),
    params: {
      fields: GRAPH_QUERY[resource_key],
      access_token: access_token
    }
  )
  JSON.load(response.to_s)
end
group_data(group_id) click to toggle source
# File lib/facegroups/fb_api.rb, line 46
def self.group_data(group_id)
  graphql_query(group_id, :group)
end
newest_group_postings(group_id) click to toggle source
# File lib/facegroups/fb_api.rb, line 65
def self.newest_group_postings(group_id)
  feed_response = HTTP.get(
    fb_resource_url(group_id) + '/feed',
    params: { access_token: access_token }
  )
  JSON.parse(feed_response)['data']
end
posting_data(posting_id) click to toggle source
# File lib/facegroups/fb_api.rb, line 50
def self.posting_data(posting_id)
  graphql_query(posting_id, :posting)
end

Private Class Methods

fb_resource_url(id) click to toggle source
# File lib/facegroups/fb_api.rb, line 75
def self.fb_resource_url(id)
  URI.join(FB_API_URL, id.to_s).to_s
end