From 6925094ef7f6a7d3cffccce9a7c61a541e28850e Mon Sep 17 00:00:00 2001 From: Kang-Kyu Lee Date: Wed, 6 Sep 2017 15:48:12 -0700 Subject: [PATCH] Add number of comments, reactions, likes for each post --- lib/fb/page.rb | 3 ++- lib/fb/post.rb | 13 +++++++++++++ spec/page/posts_spec.rb | 4 ++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/fb/page.rb b/lib/fb/page.rb index c1d1bdd..6b6d828 100644 --- a/lib/fb/page.rb +++ b/lib/fb/page.rb @@ -117,7 +117,8 @@ def posts_params {}.tap do |params| params[:access_token] = @access_token params[:limit] = 100 - params[:fields]= %i(id message permalink_url created_time type properties).join ',' + params[:fields]= ['id', 'message', 'permalink_url', 'created_time', 'type', 'properties', + 'comments.limit(0).summary(true)', 'likes.limit(0).summary(true)', 'reactions.limit(0).summary(true)'].join(',') end end diff --git a/lib/fb/post.rb b/lib/fb/post.rb index 18a8f41..d15c46c 100644 --- a/lib/fb/post.rb +++ b/lib/fb/post.rb @@ -39,6 +39,15 @@ class Post # @option [Integer] the total number of milliseconds your video was watched. attr_reader :video_view_time + # @return [Integer] the number of comments of the post. + attr_reader :comment_count + + # @return [Integer] the number of likes of the post. + attr_reader :like_count + + # @return [Integer] the number of reactions of the post. + attr_reader :reaction_count + # @param [Hash] options the options to initialize an instance of Fb::Post. # @option [String] :id The post id. # @option [String] :message The status message in the post or post story. @@ -65,6 +74,10 @@ def initialize(options = {}) @video_views_organic = options[:post_video_views_organic] if options[:post_video_views_organic] @video_views_paid = options[:post_video_views_paid] if options[:post_video_views_paid] @video_view_time = options[:post_video_view_time] if options[:post_video_view_time] + + @comment_count = options[:comments]['summary']['total_count'] if options[:comments] + @reaction_count = options[:reactions]['summary']['total_count'] if options[:reactions] + @like_count = options[:likes]['summary']['total_count'] if options[:likes] end # @return [String] the representation of the post. diff --git a/spec/page/posts_spec.rb b/spec/page/posts_spec.rb index 730f53c..b9db3a3 100644 --- a/spec/page/posts_spec.rb +++ b/spec/page/posts_spec.rb @@ -14,6 +14,10 @@ expect(page.posts.map &:type).to all(be_a String) expect(page.posts.map &:created_at).to all(be_a Time) expect(page.posts.map &:length).to all(be_a String) + + expect(page.posts.map &:comment_count).to all(be_a Integer) + expect(page.posts.map &:like_count).to all(be_a Integer) + expect(page.posts.map &:reaction_count).to all(be_a Integer) end end