fb-core 1.0.0.alpha5 → 1.0.0.alpha6

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: a4214f25d9507a9e9f17592b3338bd21575e99a0
4
- data.tar.gz: 7267a5466b17a6d546b2b8c6f9c01eeb1e928ee3
3
+ metadata.gz: eba1fc16c8b7532b533f2f9de7dab75ce28207e3
4
+ data.tar.gz: b1a2873d94e44bc65d4f8f1da60924c9df6a43ee
5
5
  SHA512:
6
- metadata.gz: 74cd023662d2a69c70adc0cc16e9aa414f7864f73b19be3b0681a1cba91b696294457ede3f4a86d3707f547b93baef62abd3c578b11688fe9ef6947365b12c31
7
- data.tar.gz: df05d7363d617bcb843b27249d3f4db253a0abdc26772fb63856a64115943edef1b0ad3ab888c188ca9929e7a5ed81cc0902875d654ecd45dc6ccdaa0fb2ce59
6
+ metadata.gz: 817e444187eb6b6487c78386bc10c34e087ccac19762821d8eb7274331096138e7ab1bfdc8f2889f56a61dc40027883658e0ae08e72cb0fe44435d3ddf4d0a9f
7
+ data.tar.gz: c2373650288e74af845087a8d28d51caa515844465f452a58accbd6ac1b9d23d58f2bfda297bdcccf310f46607a4139d0ba9105f530c2b3d4355faaed34f58c7
@@ -13,4 +13,5 @@ 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`
16
+ * [FEATURE] Added `Fb::Page#weekly_insights`
17
+ * [FEATURE] Added `Fb::Page#metric_insights`
data/README.md CHANGED
@@ -88,10 +88,10 @@ page.view_count until: Date.today - 7
88
88
  # => 10000
89
89
  ```
90
90
 
91
- Fb::Page#weekly_metrics
91
+ Fb::Page#weekly_insights
92
92
  --------------
93
93
 
94
- `weekly_metrics` allows you to get metrics that are available weekly such as
94
+ `weekly_insights` allows you to get metrics that are available weekly such as
95
95
  `page_views_total`, `page_impressions`, `page_fan_adds`, etc. Refer to
96
96
  [metrics](https://developers.facebook.com/docs/graph-api/reference/v2.9/insights#availmetrics)
97
97
  for a list of available weekly metrics.
@@ -103,17 +103,47 @@ then the values will be for July 14 - July 20.
103
103
  ```ruby
104
104
  metrics = %i(page_impressions page_fan_adds)
105
105
  page = Fb::Page.new access_token: '--valid-access-token--', id: '--valid-id--'
106
- page.weekly_metrics metrics # sum for July 14 - 20
106
+ page.weekly_insights metrics # sum for July 14 - 20
107
107
  # => {:page_impressions => 1234, :page_fan_adds => 5678}
108
108
  ```
109
109
 
110
110
  Pass an `until` (`Date`) option such as `Date.today - 7` to fetch 7 days prior to July 14th.
111
111
 
112
112
  ```ruby
113
- page.weekly_metrics metrics, until: Date.today - 7 # sum for July 8 - 14
113
+ page.weekly_insights metrics, until: Date.today - 7 # sum for July 8 - 14
114
114
  # => {:page_impressions => 1234, :page_fan_adds => 5678}
115
115
  ```
116
116
 
117
+ Fb::Page#metric_insights
118
+ --------------
119
+
120
+ You can get an individual metric through using `metric_insights` which takes a
121
+ metric, period, and options (since & until). Refer to
122
+ [metrics](https://developers.facebook.com/docs/graph-api/reference/v2.9/insights#availmetrics)
123
+ for a list of available weekly metrics and periods.
124
+
125
+ **Ensure that the period that you are using is supported by the metric**.
126
+ For example, `page_views_total` (page views) is available for `week`, `week`, and `days_28`
127
+ whereas `page_fans` (page likes) is only available as `lifetime`.
128
+
129
+ `metric_insights` returns a hash of Dates mapped to values. Passing `since` only return dates ahead
130
+ to this date (lower bound) and passing `until` will return dates previous to this day
131
+ (upper bound & defaults to 2 days ago). Combining both options will return the dates in between.
132
+
133
+ ```ruby
134
+ page = Fb::Page.new access_token: '--valid-access-token--', id: '--valid-id--'
135
+ metric_insights = page.metric_insights 'page_views_total', :day # let today be August 7th
136
+ # => {#<Date: 2017-08-04> => 598, <Date: 2017-08-05> => 548}
137
+ ```
138
+
139
+ With `until` (`Date`) and `since` (`Date`).
140
+
141
+ ```ruby
142
+ options = {since: Date.today - 9, until: Date.today}
143
+ metric_insights = page.metric_insights metric, :day, options
144
+ # => {#<Date: 2017-08-01> => 639,..,#<Date: 2017-08-05 => 548}
145
+ ```
146
+
117
147
  ## Development
118
148
 
119
149
  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.alpha5'
6
+ VERSION = '1.0.0.alpha6'
7
7
  end
8
8
  end
@@ -23,10 +23,24 @@ module Fb
23
23
  @access_token = options[:access_token]
24
24
  end
25
25
 
26
+ # @return [Hash<Date, Integer>] a hash of Dates mapped to their values.
27
+ # @param [String] :metric the metric to fetch.
28
+ # @param [<String, Symbol>] :period the aggregate period (day, week, days_28).
29
+ # @option [Date] :since only return dates ahead to this date (lower bound).
30
+ # @option [Date] :until only return dates previous to this day (upper bound).
31
+ def metric_insights(metric, period, options = {})
32
+ insights = insights Array(metric), options.merge(period: period)
33
+ values = insights.find{|data| data['name'] == metric}['values']
34
+ values.map do |v|
35
+ [Date.strptime(v['end_time'], '%Y-%m-%dT%H:%M:%S+0000'), v.fetch('value', 0)]
36
+ end.to_h
37
+ end
38
+
39
+ #values.map {|v| [Date.strptime(v['end_time'], '%Y-%m-%dT%H:%M:%S+0000'), v.fetch('value', 0)]}.to_h
26
40
  # @return [Hash] a hash of metrics mapped to their values.
27
41
  # @param [Array<String, Symbol>] :metrics the metrics to fetch.
28
42
  # @option [Date] :until only sum seven days before this date.
29
- def weekly_metrics(metrics, options = {})
43
+ def weekly_insights(metrics, options = {})
30
44
  since_date = options.fetch :until, Date.today - 1
31
45
  params = {period: :week, since: since_date, until: since_date + 2}
32
46
  metrics = insights Array(metrics), params
@@ -37,11 +51,9 @@ module Fb
37
51
  # @param [Hash] options the options
38
52
  # @option [Date] :until only count the views until this day.
39
53
  def view_count(options = {})
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)}
54
+ views = metric_insights 'page_views_total', 'day', since: '1652 days ago'
55
+ views.select!{|date, _| date < options[:until]} if options[:until]
56
+ views.values.sum
45
57
  end
46
58
 
47
59
  # @return [Integer] the number of likes of the page.
@@ -49,8 +61,8 @@ module Fb
49
61
  # @option [Date] :until only count the likes until this day.
50
62
  def like_count(options = {})
51
63
  since_date = options.fetch :until, Date.today - 1
52
- likes = single_metric 'page_fans', since: since_date, until: since_date + 2
53
- likes.last.fetch('value', 0)
64
+ likes = metric_insights 'page_fans', 'lifetime', since: since_date, until: since_date + 2
65
+ likes[since_date+1]
54
66
  end
55
67
 
56
68
  # @return [Array<Fb::Post>] the posts published on the page.
@@ -72,11 +84,6 @@ module Fb
72
84
 
73
85
  private
74
86
 
75
- def single_metric(metric, options = {})
76
- insights = insights Array(metric), options
77
- insights.find{|data| data['name'] == metric}['values']
78
- end
79
-
80
87
  def insights(metrics, options = {})
81
88
  params = options.merge metric: metrics.join(','), access_token: @access_token
82
89
  request = HTTPRequest.new path: "/v2.9/#{@id}/insights", params: params
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.alpha5
4
+ version: 1.0.0.alpha6
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-07 00:00:00.000000000 Z
11
+ date: 2017-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fb-support