From 9023d7290a3ce6a7987b1d40f81acf4523981976 Mon Sep 17 00:00:00 2001 From: Kang-Kyu Lee Date: Thu, 17 May 2018 10:33:50 -0700 Subject: [PATCH 1/3] Add lifetime_insights method to Post to get numbers per each post --- lib/fb/page.rb | 2 ++ lib/fb/post.rb | 13 ++++++++++++- spec/post/lifetime_insights_spec.rb | 30 +++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 spec/post/lifetime_insights_spec.rb diff --git a/lib/fb/page.rb b/lib/fb/page.rb index d480fae..3b4cd52 100644 --- a/lib/fb/page.rb +++ b/lib/fb/page.rb @@ -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. diff --git a/lib/fb/post.rb b/lib/fb/post.rb index 5d937f9..07ed2a8 100644 --- a/lib/fb/post.rb +++ b/lib/fb/post.rb @@ -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. @@ -300,6 +300,17 @@ def initialize(options = {}) @video_view_time_organic = options[:post_video_view_time_organic] end + 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}">) diff --git a/spec/post/lifetime_insights_spec.rb b/spec/post/lifetime_insights_spec.rb new file mode 100644 index 0000000..4a3f89d --- /dev/null +++ b/spec/post/lifetime_insights_spec.rb @@ -0,0 +1,30 @@ +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(:options) {{ + since: Time.parse((Date.today - 7).to_s), + until: Time.parse((Date.today + 1).to_s) + }} + let(:post) { page.posts(options).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 From 4c356cac1b7ac73b1423e151d2fa0088e3a8eff4 Mon Sep 17 00:00:00 2001 From: Kang-Kyu Lee Date: Thu, 17 May 2018 10:44:44 -0700 Subject: [PATCH 2/3] Add documentation for the lifetime_insights method --- lib/fb/post.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/fb/post.rb b/lib/fb/post.rb index 07ed2a8..1418b13 100644 --- a/lib/fb/post.rb +++ b/lib/fb/post.rb @@ -300,6 +300,9 @@ 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] :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 From 746d3ea4b7184def1f7035c4e57e7c53a10b937f Mon Sep 17 00:00:00 2001 From: Kang-Kyu Lee Date: Thu, 17 May 2018 10:54:08 -0700 Subject: [PATCH 3/3] Do not put date range in the test - no posts there --- CHANGELOG.md | 1 + spec/post/lifetime_insights_spec.rb | 6 +----- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c87656f..33a234c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/spec/post/lifetime_insights_spec.rb b/spec/post/lifetime_insights_spec.rb index 4a3f89d..eb4add7 100644 --- a/spec/post/lifetime_insights_spec.rb +++ b/spec/post/lifetime_insights_spec.rb @@ -3,11 +3,7 @@ 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(:options) {{ - since: Time.parse((Date.today - 7).to_s), - until: Time.parse((Date.today + 1).to_s) - }} - let(:post) { page.posts(options).first } + let(:post) { page.posts.first } context 'given a post and valid metrics' do let(:metrics) { %i(post_video_views post_video_view_time) }