fb-core 1.0.0.alpha7 → 1.0.0.alpha8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fb91cd80d655f7e81bf36f22a491bff96f8b2e27
4
- data.tar.gz: e6b0a8a0dfa8f2142103b57f8f5f6fd9cce4a904
3
+ metadata.gz: b6cd31e0997675afe22e67ce5d9a413dbd1bb31f
4
+ data.tar.gz: 12fb33cc93515ef59c82d27b52af8c419821b23b
5
5
  SHA512:
6
- metadata.gz: 4cd41e9397e2c3798ed31fcb3b870ff953e66fb55c7176077acedd0291dcdcdd2db34b6a55bb128b1c1d5f877007d2295ba8fd4a172f25f6145088ed5d819675
7
- data.tar.gz: 7960deaf0f1d145bd5772d292a2033ec2071d027b9f09b044463d8f0a315110591785c206415d758138126e9ff73ff707a20e9b2344aa2fdf071d7d493b70551
6
+ metadata.gz: 9729cc3f46d822bf3450efddaf64af4936857895b2181647c6a34706519f8d16c24520751ce7ec32459296cac8334d11a646a99095f8d3ac554a3ef74029775e
7
+ data.tar.gz: 10cd071faebf75ee7bf69a8e7c9acee0d18afd88cb0b99f3efe2812377ab5957967d8842934d40aeb0d1b028bcdaa916ec361a6d3f698dd2a75f055a73e7b836
data/README.md CHANGED
@@ -56,10 +56,26 @@ Pass `since` (`Time`) and `until` (`Time`) options to get posts in between the t
56
56
 
57
57
  ```ruby
58
58
  options = { since: Time.new(2015, 05, 15), until: Time.now }
59
- page.posts(options)
59
+ page.posts options
60
60
  # => [#<Fb::Post: id="5678", type="video">,..]
61
61
  ```
62
62
 
63
+ Pass `with_metrics: true` to include post insights for the following metrics...
64
+ All post types: `post_engaged_users`
65
+ Video posts only: `post_video_views_organic`, `post_video_views_paid`, `post_video_views`, and `post_video_view_time`
66
+
67
+ ```ruby
68
+ page.posts with_metrics: true
69
+ # => [#<Fb::Post: id="5678", type="video">,..]
70
+ ```
71
+
72
+ You can also combine all three options...
73
+
74
+ ```ruby
75
+ options = { since: Time.new(2015, 05, 15), until: Time.now, with_metrics: true }
76
+ page.posts options
77
+ ```
78
+
63
79
  Fb::Page#like_count
64
80
  --------------
65
81
 
@@ -3,6 +3,6 @@ module Fb
3
3
  class Core
4
4
  # @return [String] the SemVer-compatible gem version.
5
5
  # @see http://semver.org
6
- VERSION = '1.0.0.alpha7'
6
+ VERSION = '1.0.0.alpha8'
7
7
  end
8
8
  end
data/lib/fb/page.rb CHANGED
@@ -29,7 +29,7 @@ module Fb
29
29
  # @option [Date] :since only return dates ahead to this date (lower bound).
30
30
  # @option [Date] :until only return dates previous to this day (upper bound).
31
31
  def metric_insights(metric, period, options = {})
32
- insights = insights Array(metric), options.merge(period: period)
32
+ insights = page_insights Array(metric), options.merge(period: period)
33
33
  values = insights.find{|data| data['name'] == metric}['values']
34
34
  values.map do |v|
35
35
  [Date.strptime(v['end_time'], '%Y-%m-%dT%H:%M:%S+0000'), v.fetch('value', 0)]
@@ -42,7 +42,7 @@ module Fb
42
42
  def weekly_insights(metrics, options = {})
43
43
  since_date = options.fetch :until, Date.today - 1
44
44
  params = {period: :week, since: since_date, until: since_date + 2}
45
- metrics = insights Array(metrics), params
45
+ metrics = page_insights Array(metrics), params
46
46
  metrics.map {|m| [m['name'].to_sym, m['values'].last.fetch('value', 0)]}.to_h
47
47
  end
48
48
 
@@ -67,13 +67,13 @@ module Fb
67
67
  # @return [Array<Fb::Post>] the posts published on the page.
68
68
  # @option [Time] :since only return posts ahead of this time.
69
69
  # @option [Time] :until only return posts previous to this time.
70
+ # @option [Boolean] :with_metrics whether to include insights for the posts.
70
71
  def posts(options = {})
71
72
  @posts ||= begin
72
73
  params = posts_params.merge options
73
74
  request = PaginatedRequest.new path: "/v2.9/#{@id}/posts", params: params
74
- request.run.body['data'].map do |post_data|
75
- Post.new symbolize_keys post_data
76
- end
75
+ data = request.run.body['data']
76
+ options[:with_metrics] ? posts_with_metrics_from(data) : posts_from(data)
77
77
  end
78
78
  end
79
79
 
@@ -84,10 +84,33 @@ module Fb
84
84
 
85
85
  private
86
86
 
87
+ def page_insights(metrics, options = {})
88
+ insights(metrics, options.merge(ids: id))[id]['data']
89
+ end
90
+
91
+ def posts_from(data)
92
+ data.map do |post_data|
93
+ Post.new symbolize_keys post_data
94
+ end
95
+ end
96
+
97
+ def posts_with_metrics_from(data)
98
+ data.each_slice(50).flat_map do |post_slice|
99
+ post_ids = post_slice.map{|post| post['id']}.join ','
100
+ metrics = insights post_metrics, ids: post_ids, period: :lifetime
101
+ post_slice.map do |post_data|
102
+ insights_data = metrics[post_data['id']]['data'].map do |metric|
103
+ [metric['name'], metric['values'].last.fetch('value', 0)]
104
+ end.to_h
105
+ Post.new symbolize_keys post_data.merge(insights_data)
106
+ end
107
+ end
108
+ end
109
+
87
110
  def insights(metrics, options = {})
88
111
  params = options.merge metric: metrics.join(','), access_token: @access_token
89
- request = HTTPRequest.new path: "/v2.9/#{@id}/insights", params: params
90
- request.run.body['data']
112
+ request = HTTPRequest.new path: "/v2.9/insights", params: params
113
+ request.run.body
91
114
  end
92
115
 
93
116
  def posts_params
@@ -97,5 +120,9 @@ module Fb
97
120
  params[:fields]= %i(id message permalink_url created_time type properties).join ','
98
121
  end
99
122
  end
123
+
124
+ def post_metrics
125
+ %i(post_engaged_users post_video_views_organic post_video_views_paid post_video_views post_video_view_time)
126
+ end
100
127
  end
101
128
  end
data/lib/fb/post.rb CHANGED
@@ -2,7 +2,8 @@
2
2
  # @see http://www.rubydoc.info/gems/Fb/
3
3
  module Fb
4
4
  # Fb::Post reprensents a Facebook post. Post provides getters for:
5
- # :id, :url, :created_at, :type, :message, and :length.
5
+ # :id, :url, :created_at, :type, :message, :length, engaged_users,
6
+ # video_views, video_views_organic, video_views_paid, and total_minutes_watched.
6
7
  class Post
7
8
 
8
9
  # @option [String] the post’s unique ID.
@@ -23,6 +24,21 @@ module Fb
23
24
  # @option [String] the attached video's length or n/a.
24
25
  attr_reader :length
25
26
 
27
+ # @option [Integer] the number of people who clicked anywhere on the post.
28
+ attr_reader :engaged_users
29
+
30
+ # @option [Integer] video views of 3 seconds or more.
31
+ attr_reader :video_views
32
+
33
+ # @option [Integer] organic video views of 3 seconds or more.
34
+ attr_reader :video_views_organic
35
+
36
+ # @option [Integer] paid video views of 3 seconds or more.
37
+ attr_reader :video_views_paid
38
+
39
+ # @option [Integer] total minutes watched by all users who viewed the video.
40
+ attr_reader :total_minutes_watched
41
+
26
42
  # @param [Hash] options the options to initialize an instance of Fb::Post.
27
43
  # @option [String] :id The post id.
28
44
  # @option [String] :message The status message in the post or post story.
@@ -30,6 +46,11 @@ module Fb
30
46
  # @option [String] :created_time The time the post was initially published.
31
47
  # @option [String] :type A string indicating the object type of this post.
32
48
  # @option [String] :properties of the post (e.g. length).
49
+ # @option [Integer] :post_engaged_users The number of people who clicked anywhere on the post.
50
+ # @option [Integer] :post_video_views Video views of 3 seconds or more.
51
+ # @option [Integer] :post_video_views_organic Organic video views of 3 seconds or more.
52
+ # @option [Integer] :post_video_views_paid Paid video views of 3 seconds or more.
53
+ # @option [Integer] :post_video_view_time Total video view time (miliseconds).
33
54
  def initialize(options = {})
34
55
  @id = options[:id]
35
56
  @url = options[:permalink_url]
@@ -39,6 +60,11 @@ module Fb
39
60
  @length = options.fetch(:properties, []).find(-> { {'text' => 'n/a'} }) do |property|
40
61
  property['name'] == 'Length'
41
62
  end['text']
63
+ @engaged_users = options[:post_engaged_users] if options[:post_engaged_users]
64
+ @video_views = options[:post_video_views] if options[:post_video_views]
65
+ @video_views_organic = options[:post_video_views_organic] if options[:post_video_views_organic]
66
+ @video_views_paid = options[:post_video_views_paid] if options[:post_video_views_paid]
67
+ @total_minutes_watched = (options[:post_video_view_time]/1000) if options[:post_video_view_time]
42
68
  end
43
69
 
44
70
  # @return [String] the representation of the post.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fb-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.alpha7
4
+ version: 1.0.0.alpha8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claudio Baccigalupo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-10 00:00:00.000000000 Z
11
+ date: 2017-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fb-support