Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For more information about changelogs, check

* [IMPROVEMENT] Return UTC time for String value of `created_time`, `backdated_time`,
`end_time`, etc from Facebook to have exact time.
* [FEATURE] Add Fb::Post#lifetime_insights` method to get metrics of each post.

## 1.0.0.beta10 - 2018/05/01

Expand Down
2 changes: 2 additions & 0 deletions lib/fb/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Page < Resource
# @option [String] the page’s category.
attr_reader :category

attr_reader :access_token

# @param [Hash] options to initialize a Page object.
# @option [String] :id The page’s unique ID.
# @option [String] :name The page’s name.
Expand Down
16 changes: 15 additions & 1 deletion lib/fb/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Fb
# :id, :url, :created_at, :type, :message, :length, engaged_users,
# video_views, video_views_organic, video_views_paid, and so on.
# @see https://developers.facebook.com/docs/graph-api/reference/v2.10/post
class Post
class Post < Resource
attr_accessor :custom_labels

# @option [String] the post’s unique ID.
Expand Down Expand Up @@ -300,6 +300,20 @@ def initialize(options = {})
@video_view_time_organic = options[:post_video_view_time_organic]
end

# @return [Hash] a hash of metrics mapped to their values.
# @param [Array<String, Symbol>] :metrics the metrics to fetch.
# @param [String] :page_access_token page access token of its page.
def lifetime_insights(metrics, page_access_token)
params = { metric: Array(metrics).join(","), access_token: page_access_token, period: "lifetime", ids: id }
request = HTTPRequest.new path: "/v2.9/insights", params: params
insights = request.run.body

data = insights[id]['data'].map do |metric|
[metric['name'], metric['values'].last.fetch('value', 0)]
end.to_h
symbolize_keys data
end

# @return [String] the representation of the post.
def to_s
%Q(#<#{self.class.name} #{@id} "#{@type}">)
Expand Down
26 changes: 26 additions & 0 deletions spec/post/lifetime_insights_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'

RSpec.describe 'Fb::Post#lifetime_insights' do
let(:user) { Fb::User.new access_token: ENV['FB_TEST_ACCESS_TOKEN'] }
let(:page) { user.pages.first }
let(:post) { page.posts.first }

context 'given a post and valid metrics' do
let(:metrics) { %i(post_video_views post_video_view_time) }

it 'returns a hash of given metrics mapped to their values' do
lifetime_insights = post.lifetime_insights metrics, page.access_token
expect(lifetime_insights).to be_a(Hash)
expect(lifetime_insights.keys).to match_array metrics
expect(lifetime_insights.values).to all (be_an Integer)
end
end

context 'given a page and invalid metrics' do
let(:metrics) { %i(invalid_metric) }

it 'raises an HTTPError' do
expect{post.lifetime_insights metrics, page.access_token}.to raise_error Fb::HTTPError
end
end
end