All Files
(100.0%
covered at
6.82
hits/line)
5 files in total.
76 relevant lines.
76 lines covered and
0 lines missed
-
1
files = Dir.glob(File.join(File.dirname(__FILE__), 'fanswatch/*.rb'))
-
5
files.each { |lib| require_relative lib }
-
1
module FansWatch
-
# Attached URL to Posting
-
1
class Attachment
-
1
attr_reader :description, :url
-
-
1
def initialize(data)
-
1
return unless data
-
1
@description = data['description']
-
1
@url = data['url']
-
end
-
-
end
-
end
-
1
require 'http'
-
1
require 'json'
-
-
1
module FansWatch
-
# Service for all FB API calls
-
1
class FbApi
-
1
FB_URL = 'https://graph.facebook.com'
-
1
API_VER = 'v2.8'
-
1
FB_API_URL = URI.join(FB_URL, "#{API_VER}/")
-
1
FB_TOKEN_URL = URI.join(FB_API_URL, 'oauth/access_token')
-
-
1
attr_reader :access_token
-
-
1
def initialize(client_id:, client_secret:)
-
6
access_token_response =
-
HTTP.get(FB_TOKEN_URL,
-
params: { client_id: client_id,
-
client_secret: client_secret,
-
grant_type: 'client_credentials' })
-
6
@access_token = JSON.load(access_token_response.to_s)['access_token']
-
end
-
-
1
def fb_resource(id)
-
4
response = HTTP.get(
-
fb_resource_url(id),
-
params: {access_token: @access_token })
-
4
JSON.load(response.to_s)
-
end
-
-
1
def page_info(page_id)
-
3
fb_resource(page_id)
-
end
-
-
1
def posting(posting_id)
-
1
fb_resource(posting_id)
-
end
-
-
1
def page_feed(page_id)
-
2
feed_response =
-
HTTP.get(URI.join(fb_resource_url(page_id), 'feed'),
-
params: { access_token: @access_token })
-
2
JSON.load(feed_response.to_s)['data']
-
end
-
-
1
def posting_attachments(posting_id)
-
1
attachments_response =
-
HTTP.get(URI.join(fb_resource_url(posting_id), 'attachments'),
-
params: { access_token: @access_token })
-
1
JSON.load(attachments_response.to_s)['data'].first
-
end
-
-
-
-
-
1
private
-
-
1
def fb_resource_url(id)
-
7
URI.join(FB_API_URL, "/#{id}/")
-
end
-
-
end
-
end
-
-
1
require_relative 'fb_api'
-
1
require_relative 'posting'
-
-
1
module FansWatch
-
# Main class to setup a Facebook group
-
1
class Page
-
1
attr_reader :name
-
-
1
def initialize(fb_api, data:)
-
3
@fb_api = fb_api
-
3
@name = data['name']
-
3
@id = data['id']
-
end
-
-
1
def feed
-
2
return @feed if @feed
-
2
raw_feed = @fb_api.page_feed(@id)
-
2
@feed = raw_feed.map do |posting|
-
50
FansWatch::Posting.new(
-
@fb_api,
-
data: posting
-
)
-
end
-
end
-
-
1
def self.find(fb_api, id:)
-
3
page_data = fb_api.page_info(id)
-
3
new(fb_api, data: page_data)
-
end
-
-
end
-
end
-
1
require_relative 'fb_api'
-
1
require_relative 'attachment'
-
-
1
module FansWatch
-
# Single posting on group's feed
-
1
class Posting
-
1
attr_reader :message, :created_time, :id
-
-
1
def initialize(fb_api, data: nil)
-
51
@fb_api = fb_api
-
51
load_data(data)
-
end
-
-
1
def attachment
-
2
return @attachment if @attachment
-
-
1
attached_data = @fb_api.posting_attachments(@id)
-
1
@attachment = Attachment.new(attached_data)
-
end
-
-
1
def self.find(fb_api, id:)
-
1
posting_data = fb_api.posting(id)
-
1
new(fb_api, data:posting_data)
-
end
-
-
1
private
-
-
1
def load_data(posting_data)
-
51
@id = posting_data['id']
-
# @updated_time = posting_data['updated_time']
-
51
@created_time = posting_data['created_time']
-
51
@message = posting_data['message']
-
51
attached = posting_data['attachment']
-
51
@attachment = Attachment.new(attached) if attached
-
end
-
end
-
end