Class: FaceGroup::FbApi

Inherits:
Object
  • Object
show all
Defined in:
lib/facegroup/fb_api.rb

Overview

Service for all FB API calls

Constant Summary collapse

FB_URL =
'https://graph.facebook.com'
API_VER =
'v2.8'
FB_API_URL =
[FB_URL, API_VER].join('/')
FB_TOKEN_URL =
[FB_API_URL, 'oauth/access_token'].join('/')
TOKEN_KEY =
'fbapi_access_token'
GRAPH_QUERY =
{
  group:   'id,name,feed{name,message,updated_time,created_time,'\
           'attachments{title,description,url,media}}',
  posting: 'name,message,updated_time,created_time,'\
           'attachments{title,description,url,media}'
}.freeze

Class Method Summary collapse

Class Method Details

.access_tokenObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/facegroup/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

.configObject



35
36
37
38
39
# File 'lib/facegroup/fb_api.rb', line 35

def self.config
  return @config if @config
  @config = { client_id:     ENV['FB_CLIENT_ID'],
              client_secret: ENV['FB_CLIENT_SECRET'] }
end

.config=(credentials) ⇒ Object



31
32
33
# File 'lib/facegroup/fb_api.rb', line 31

def self.config=(credentials)
  @config ? @config.update(credentials) : @config = credentials
end

.fb_resource_url(id) ⇒ Object



68
69
70
# File 'lib/facegroup/fb_api.rb', line 68

def self.fb_resource_url(id)
  URI.join(FB_API_URL, id.to_s).to_s
end

.graphql_query(resource_id, resource_key) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/facegroup/fb_api.rb', line 49

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) ⇒ Object



41
42
43
# File 'lib/facegroup/fb_api.rb', line 41

def self.group_data(group_id)
  graphql_query(group_id, :group)
end

.newest_group_postings(group_id) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/facegroup/fb_api.rb', line 58

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) ⇒ Object



45
46
47
# File 'lib/facegroup/fb_api.rb', line 45

def self.posting_data(posting_id)
  graphql_query(posting_id, :posting)
end