fb-core 1.0.0.alpha4 → 1.0.0.alpha5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 95ee401be1930e9abee5c75dd16756a55d88b8c0
4
- data.tar.gz: 3f8eb395c5ddb81ecbe32e5350e27d301793dc11
3
+ metadata.gz: a4214f25d9507a9e9f17592b3338bd21575e99a0
4
+ data.tar.gz: 7267a5466b17a6d546b2b8c6f9c01eeb1e928ee3
5
5
  SHA512:
6
- metadata.gz: 78d6a2ca60bfb77814197651fb078481e07ad9cf3164d5489b2880cce292db930dcae9c9843d21b12bc7a673f31c029355f1c480b3730046fc37f33e5851af7a
7
- data.tar.gz: 86a411fe281ec2210fd5617b8edbb753287baa407ac69b0e256c46d8d804cb4bdccdafef27041ea8212e815d32e1a93b471a02cce209014e8c23719370b4e6de
6
+ metadata.gz: 74cd023662d2a69c70adc0cc16e9aa414f7864f73b19be3b0681a1cba91b696294457ede3f4a86d3707f547b93baef62abd3c578b11688fe9ef6947365b12c31
7
+ data.tar.gz: df05d7363d617bcb843b27249d3f4db253a0abdc26772fb63856a64115943edef1b0ad3ab888c188ca9929e7a5ed81cc0902875d654ecd45dc6ccdaa0fb2ce59
@@ -13,3 +13,4 @@ For more information about changelogs, check
13
13
  * [FEATURE] Added `Fb::Page#posts`
14
14
  * [FEATURE] Added `Fb::Page#like_count`
15
15
  * [FEATURE] Added `Fb::Page#view_count`
16
+ * [FEATURE] Added `Fb::Page#weekly_metrics`
data/README.md CHANGED
@@ -63,7 +63,7 @@ page.like_count
63
63
  # => 15000
64
64
  ```
65
65
 
66
- Pass an `until` option to get the count at a certain date.
66
+ Pass an `until` (`Date`) option to get the count at a certain date.
67
67
 
68
68
  ```ruby
69
69
  page.like_count until: Date.today - 7
@@ -81,13 +81,39 @@ page.view_count
81
81
  # => 12345
82
82
  ```
83
83
 
84
- Pass an `until` option to get the count at a certain date.
84
+ Pass an `until` (`Date`) option to get the count at a certain date.
85
85
 
86
86
  ```ruby
87
87
  page.view_count until: Date.today - 7
88
88
  # => 10000
89
89
  ```
90
90
 
91
+ Fb::Page#weekly_metrics
92
+ --------------
93
+
94
+ `weekly_metrics` allows you to get metrics that are available weekly such as
95
+ `page_views_total`, `page_impressions`, `page_fan_adds`, etc. Refer to
96
+ [metrics](https://developers.facebook.com/docs/graph-api/reference/v2.9/insights#availmetrics)
97
+ for a list of available weekly metrics.
98
+
99
+ This method takes an array metrics and returns a hash of the metrics mapped to
100
+ their values. Each metric value is the sum of the last 7 days. If today is July 20th,
101
+ then the values will be for July 14 - July 20.
102
+
103
+ ```ruby
104
+ metrics = %i(page_impressions page_fan_adds)
105
+ page = Fb::Page.new access_token: '--valid-access-token--', id: '--valid-id--'
106
+ page.weekly_metrics metrics # sum for July 14 - 20
107
+ # => {:page_impressions => 1234, :page_fan_adds => 5678}
108
+ ```
109
+
110
+ Pass an `until` (`Date`) option such as `Date.today - 7` to fetch 7 days prior to July 14th.
111
+
112
+ ```ruby
113
+ page.weekly_metrics metrics, until: Date.today - 7 # sum for July 8 - 14
114
+ # => {:page_impressions => 1234, :page_fan_adds => 5678}
115
+ ```
116
+
91
117
  ## Development
92
118
 
93
119
  To run tests, obtain a long-term access token for a Facebook user who manages
@@ -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.alpha4'
6
+ VERSION = '1.0.0.alpha5'
7
7
  end
8
8
  end
@@ -23,13 +23,25 @@ module Fb
23
23
  @access_token = options[:access_token]
24
24
  end
25
25
 
26
+ # @return [Hash] a hash of metrics mapped to their values.
27
+ # @param [Array<String, Symbol>] :metrics the metrics to fetch.
28
+ # @option [Date] :until only sum seven days before this date.
29
+ def weekly_metrics(metrics, options = {})
30
+ since_date = options.fetch :until, Date.today - 1
31
+ params = {period: :week, since: since_date, until: since_date + 2}
32
+ metrics = insights Array(metrics), params
33
+ metrics.map {|m| [m['name'].to_sym, m['values'].last.fetch('value', 0)]}.to_h
34
+ end
35
+
26
36
  # @return [Integer] the number of views of the page.
27
37
  # @param [Hash] options the options
28
38
  # @option [Date] :until only count the views until this day.
29
39
  def view_count(options = {})
30
- views = insights 'page_video_views', period: :day, since: '1652 days ago'
31
- views.select!{|v| v['end_time'] < options[:until].strftime('%Y-%m-%dT%H:%M:%S+0000')} if options[:until]
32
- views.sum{|x| x.fetch('value', 0)} || 0
40
+ views = single_metric 'page_views_total', period: :day, since: '1652 days ago'
41
+ if options[:until]
42
+ views.select!{|v| v['end_time'] < options[:until].strftime('%Y-%m-%dT%H:%M:%S+0000')}
43
+ end
44
+ views.sum{|x| x.fetch('value', 0)}
33
45
  end
34
46
 
35
47
  # @return [Integer] the number of likes of the page.
@@ -37,8 +49,8 @@ module Fb
37
49
  # @option [Date] :until only count the likes until this day.
38
50
  def like_count(options = {})
39
51
  since_date = options.fetch :until, Date.today - 1
40
- likes = insights('page_fans', since: since_date, until: since_date + 2)
41
- likes.last['value'] || 0
52
+ likes = single_metric 'page_fans', since: since_date, until: since_date + 2
53
+ likes.last.fetch('value', 0)
42
54
  end
43
55
 
44
56
  # @return [Array<Fb::Post>] the posts published on the page.
@@ -60,10 +72,15 @@ module Fb
60
72
 
61
73
  private
62
74
 
63
- def insights(metric, options = {})
64
- params = options.merge metric: metric, access_token: @access_token
75
+ def single_metric(metric, options = {})
76
+ insights = insights Array(metric), options
77
+ insights.find{|data| data['name'] == metric}['values']
78
+ end
79
+
80
+ def insights(metrics, options = {})
81
+ params = options.merge metric: metrics.join(','), access_token: @access_token
65
82
  request = HTTPRequest.new path: "/v2.9/#{@id}/insights", params: params
66
- request.run.body['data'].find{|data| data['name'] == metric}['values']
83
+ request.run.body['data']
67
84
  end
68
85
  end
69
86
  end
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.alpha4
4
+ version: 1.0.0.alpha5
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-03 00:00:00.000000000 Z
11
+ date: 2017-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fb-support