Class: FaceGroups::FbApi

Inherits:
Object
  • Object
show all
Defined in:
lib/facegroups/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
30
31
32
# 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

.configObject



38
39
40
41
42
43
44
# 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) ⇒ Object



34
35
36
# File 'lib/facegroups/fb_api.rb', line 34

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

.fb_resource_url(id) ⇒ Object



75
76
77
# 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

.graphql_query(resource_id, resource_key) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object



46
47
48
# 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) ⇒ Object



65
66
67
68
69
70
71
# 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) ⇒ Object



50
51
52
# File 'lib/facegroups/fb_api.rb', line 50

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