fb-core 1.0.0.alpha2 → 1.0.0.alpha4

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: 5a276100e70569cfea82d59f7b717fed5abd4738
4
- data.tar.gz: 91bc74bf0cb37e2e35571762af7113c3a12c710d
3
+ metadata.gz: 95ee401be1930e9abee5c75dd16756a55d88b8c0
4
+ data.tar.gz: 3f8eb395c5ddb81ecbe32e5350e27d301793dc11
5
5
  SHA512:
6
- metadata.gz: 3f51e0ac980b581dc401dba9e05f4bfbbe3ed2047ad4fa9fc51097993ba3c47ea9c80b4f2e688d3a45a6ac3f4668d30d44f612afd9d7b03e1a1d5af1888e05aa
7
- data.tar.gz: 52f941c27635d888f3b5d4d9c12caaf5009e0bf91994768334226dae1a28398579d94b6f03607100509c69472aca6f2ca44679acec4f197baf89b684f097601e
6
+ metadata.gz: 78d6a2ca60bfb77814197651fb078481e07ad9cf3164d5489b2880cce292db930dcae9c9843d21b12bc7a673f31c029355f1c480b3730046fc37f33e5851af7a
7
+ data.tar.gz: 86a411fe281ec2210fd5617b8edbb753287baa407ac69b0e256c46d8d804cb4bdccdafef27041ea8212e815d32e1a93b471a02cce209014e8c23719370b4e6de
@@ -10,3 +10,6 @@ For more information about changelogs, check
10
10
 
11
11
  * [FEATURE] Added `Fb::User`
12
12
  * [FEATURE] Added `Fb::Page`
13
+ * [FEATURE] Added `Fb::Page#posts`
14
+ * [FEATURE] Added `Fb::Page#like_count`
15
+ * [FEATURE] Added `Fb::Page#view_count`
data/README.md CHANGED
@@ -33,7 +33,7 @@ user.email # => 'john.smith@example.com'
33
33
  Fb::User#pages
34
34
  --------------
35
35
 
36
- Given an user access token with the `manage_pages` scope, you can get the list of Facebook pages managed by the user by calling:
36
+ Given an user access token with the `pages_show_list` scope, you can get the list of Facebook pages managed by the user by calling:
37
37
 
38
38
  ```ruby
39
39
  user = Fb::User.new access_token: '--valid-access-token--'
@@ -41,6 +41,53 @@ user.pages
41
41
  # => [#<Fb::Page: id="1234", name="sample1">, #<Fb::Page: id="5678", name="sample2">]
42
42
  ```
43
43
 
44
+ Fb::Page#posts
45
+ --------------
46
+
47
+ Given a page with posts, you can get the posts on the page since creation by calling:
48
+
49
+ ```ruby
50
+ page = Fb::Page.new access_token: '--valid-access-token--', id: '--valid-id--'
51
+ page.posts
52
+ # => [#<Fb::Post: id="1234", type="video">, #<Fb::Post: id="5678", type="video">]
53
+ ```
54
+
55
+ Fb::Page#like_count
56
+ --------------
57
+
58
+ Given a page, you can get the the number of likes for the page.
59
+
60
+ ```ruby
61
+ page = Fb::Page.new access_token: '--valid-access-token--', id: '--valid-id--'
62
+ page.like_count
63
+ # => 15000
64
+ ```
65
+
66
+ Pass an `until` option to get the count at a certain date.
67
+
68
+ ```ruby
69
+ page.like_count until: Date.today - 7
70
+ # => 10000
71
+ ```
72
+
73
+ Fb::Page#view_count
74
+ --------------
75
+
76
+ You can also get the the number of views for the page.
77
+
78
+ ```ruby
79
+ page = Fb::Page.new access_token: '--valid-access-token--', id: '--valid-id--'
80
+ page.view_count
81
+ # => 12345
82
+ ```
83
+
84
+ Pass an `until` option to get the count at a certain date.
85
+
86
+ ```ruby
87
+ page.view_count until: Date.today - 7
88
+ # => 10000
89
+ ```
90
+
44
91
  ## Development
45
92
 
46
93
  To run tests, obtain a long-term access token for a Facebook user who manages
@@ -15,6 +15,8 @@ Gem::Specification.new do |spec|
15
15
  spec.homepage = 'https://github.com/Fullscreen/fb-core'
16
16
  spec.license = 'MIT'
17
17
 
18
+ spec.required_ruby_version = '>= 2.4'
19
+
18
20
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
21
  f.match(%r{^(test|spec|features)/})
20
22
  end
@@ -23,7 +25,6 @@ Gem::Specification.new do |spec|
23
25
  spec.require_paths = ['lib']
24
26
 
25
27
  spec.add_dependency 'fb-support', '~> 1.0.0.alpha1'
26
-
27
28
  spec.add_development_dependency 'bundler', '~> 1.15'
28
29
  spec.add_development_dependency 'rake', '~> 10.0'
29
30
  spec.add_development_dependency 'rspec', '~> 3.0'
@@ -1,7 +1,9 @@
1
1
  require 'fb/support'
2
+ require 'fb/paginated_request'
3
+ require 'fb/resource'
2
4
  require 'fb/page'
3
5
  require 'fb/user'
4
-
6
+ require 'fb/post'
5
7
  # An object-oriented Ruby client for the Facebook Graph API.
6
8
  # @see http://www.rubydoc.info/gems/fb-core/
7
9
  module Fb
@@ -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.alpha2'
6
+ VERSION = '1.0.0.alpha4'
7
7
  end
8
8
  end
@@ -1,7 +1,7 @@
1
1
  module Fb
2
2
  # Provides methods to interact with Facebook pages through the Graph API.
3
3
  # @see https://developers.facebook.com/docs/graph-api/reference/page/
4
- class Page
4
+ class Page < Resource
5
5
  # @option [String] the page’s unique ID.
6
6
  attr_reader :id
7
7
 
@@ -15,15 +15,55 @@ module Fb
15
15
  # @option [String] :id The page’s unique ID.
16
16
  # @option [String] :name The page’s name.
17
17
  # @option [String] :category The page’s category.
18
+ # @option [String] :access_token an access token for the page.
18
19
  def initialize(options = {})
19
20
  @id = options[:id]
20
21
  @name = options[:name]
21
22
  @category = options[:category]
23
+ @access_token = options[:access_token]
24
+ end
25
+
26
+ # @return [Integer] the number of views of the page.
27
+ # @param [Hash] options the options
28
+ # @option [Date] :until only count the views until this day.
29
+ 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
33
+ end
34
+
35
+ # @return [Integer] the number of likes of the page.
36
+ # @param [Hash] options the options
37
+ # @option [Date] :until only count the likes until this day.
38
+ def like_count(options = {})
39
+ 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
42
+ end
43
+
44
+ # @return [Array<Fb::Post>] the posts published on the page.
45
+ def posts
46
+ @posts ||= begin
47
+ fields = %i(type created_time).join ','
48
+ params = {access_token: @access_token, limit: 100, fields: fields}
49
+ request = PaginatedRequest.new path: "/v2.9/#{@id}/posts", params: params
50
+ request.run.body['data'].map do |post_data|
51
+ Post.new symbolize_keys post_data
52
+ end
53
+ end
22
54
  end
23
55
 
24
56
  # @return [String] the representation of the page.
25
57
  def to_s
26
58
  %Q(#<#{self.class.name} #{@id} "#{@name}">)
27
59
  end
60
+
61
+ private
62
+
63
+ def insights(metric, options = {})
64
+ params = options.merge metric: metric, access_token: @access_token
65
+ request = HTTPRequest.new path: "/v2.9/#{@id}/insights", params: params
66
+ request.run.body['data'].find{|data| data['name'] == metric}['values']
67
+ end
28
68
  end
29
69
  end
@@ -0,0 +1,22 @@
1
+ module Fb
2
+ # Provides a wrapper for HTTPRequest when the result has pagination links.
3
+ # @api private
4
+ class PaginatedRequest < HTTPRequest
5
+ # Sends the request and returns the response with the body parsed from JSON.
6
+ # If the response body contains a link to the next page, fetches that page
7
+ # as well and combines the data with the previous page.
8
+ # @return [Net::HTTPResponse] if the request succeeds.
9
+ # @raise [Fb::HTTPError] if the request fails.
10
+ def run
11
+ response = super
12
+ while after = response.body.dig('paging', 'cursors', 'after')
13
+ next_params = @params.merge after: after, limit: 100
14
+ next_request = HTTPRequest.new path: @path, params: next_params
15
+ next_body = next_request.run.body
16
+ response.body['paging'] = next_body['paging']
17
+ response.body['data'].concat next_body['data']
18
+ end
19
+ response
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ # Ruby client to authenticate a Facebook user.
2
+ # @see http://www.rubydoc.info/gems/Fb/
3
+ module Fb
4
+ # Fb::Post reprensents a Facebook post. Post provides getters for:
5
+ # :id, :title, :url, :created_time, and :type.
6
+ class Post
7
+ # @option [String] the post’s unique ID.
8
+ attr_reader :id
9
+
10
+ # @option [String] the post’s type.
11
+ attr_reader :type
12
+
13
+ # @option [Time] the post’s creation time.
14
+ attr_reader :created_at
15
+
16
+ # @param [Hash] options the options to initialize an instance of Fb::Post.
17
+ # @option [String] :id the post id.
18
+ # @option [String] :type the post’s type.
19
+ # @option [String] :created_time the post’s creation time in iso8601 format.
20
+ def initialize(options = {})
21
+ @id = options[:id]
22
+ @type = options[:type]
23
+ @created_at = Time.parse(options[:created_time]) if options[:created_time]
24
+ end
25
+
26
+ # @return [String] the representation of the post.
27
+ def to_s
28
+ %Q(#<#{self.class.name} #{@id} "#{@type}">)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,11 @@
1
+ module Fb
2
+ # Provides a base class for Facebook resources (users, pages, ...)
3
+ class Resource
4
+ private
5
+ def symbolize_keys(hash)
6
+ {}.tap do |new_hash|
7
+ hash.each_key{|key| new_hash[key.to_sym] = hash[key]}
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,7 +1,7 @@
1
1
  module Fb
2
2
  # Provides methods to interact with Facebook users through the Graph API.
3
3
  # @see https://developers.facebook.com/docs/graph-api/reference/user/
4
- class User
4
+ class User < Resource
5
5
  # @param [Hash] options to initialize a User object.
6
6
  # @option [String] :access_token an access token for the user.
7
7
  def initialize(options = {})
@@ -23,17 +23,9 @@ module Fb
23
23
  params = {access_token: @access_token}
24
24
  request = HTTPRequest.new path: '/me/accounts', params: params
25
25
  request.run.body['data'].map do |page_data|
26
- Page.new symbolize_keys(page_data)
26
+ Page.new symbolize_keys(page_data.merge access_token: @access_token)
27
27
  end
28
28
  end
29
29
  end
30
-
31
- private
32
-
33
- def symbolize_keys(hash)
34
- {}.tap do |new_hash|
35
- hash.each_key{|key| new_hash[key.to_sym] = hash[key]}
36
- end
37
- end
38
30
  end
39
31
  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.alpha2
4
+ version: 1.0.0.alpha4
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-07-24 00:00:00.000000000 Z
11
+ date: 2017-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fb-support
@@ -118,6 +118,9 @@ files:
118
118
  - lib/fb/core.rb
119
119
  - lib/fb/core/version.rb
120
120
  - lib/fb/page.rb
121
+ - lib/fb/paginated_request.rb
122
+ - lib/fb/post.rb
123
+ - lib/fb/resource.rb
121
124
  - lib/fb/user.rb
122
125
  homepage: https://github.com/Fullscreen/fb-core
123
126
  licenses:
@@ -131,7 +134,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
131
134
  requirements:
132
135
  - - ">="
133
136
  - !ruby/object:Gem::Version
134
- version: '0'
137
+ version: '2.4'
135
138
  required_rubygems_version: !ruby/object:Gem::Requirement
136
139
  requirements:
137
140
  - - ">"