fb-auth 0.1.3 → 1.0.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +4 -64
- data/fb-auth.gemspec +13 -11
- data/lib/fb/auth.rb +23 -26
- data/lib/fb/auth/version.rb +1 -1
- metadata +18 -8
- data/lib/fb/configuration.rb +0 -33
- data/lib/fb/metric.rb +0 -35
- data/lib/fb/page.rb +0 -61
- data/lib/fb/user.rb +0 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b92a001d9a902960bdbb31dfe72cd02191901df1
|
4
|
+
data.tar.gz: 718e5ac23244e8789bff97d82baeefe2e81c751b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 39775c3dfb582f352fc9d73df5b9b93aec902439b15bba17f0dd890b279ca5794b9f213cd310cf40ed0b85e5a3262072d40f8137e0e154a3b1113753b3bffa8c
|
7
|
+
data.tar.gz: bdff56d25598cf2bea7d3fd2b934d924a1207c3943648601c76538aeef5bf3fe474b61f7ad16b52e61355a8c793973fcef81d593fca63d69eda8011505f2fc10
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,16 @@ For more information about changelogs, check
|
|
6
6
|
[Keep a Changelog](http://keepachangelog.com) and
|
7
7
|
[Vandamme](http://tech-angels.github.io/vandamme).
|
8
8
|
|
9
|
+
## 1.0.0 - 2017/07/24
|
10
|
+
|
11
|
+
This library was originally intended to provide methods to authenticate but
|
12
|
+
has grown to include other methods to configure, get posts, insights etc.
|
13
|
+
Most of those methods have been extracted into separate libraries.
|
14
|
+
|
15
|
+
* [REMOVAL] Moved Fb::Page to fb-core gem (without insights)
|
16
|
+
* [REMOVAL] Moved Fb::User to fb-core gem
|
17
|
+
* [REMOVAL] Moved Fb::Configuration to fb-support gem
|
18
|
+
|
9
19
|
## 0.1.3 - 2017/07/20
|
10
20
|
|
11
21
|
* [BUGFIX] Fixed `require` statement not loading for some classes.
|
data/README.md
CHANGED
@@ -29,7 +29,7 @@ By default, Fb::Auth will look for the environment variables called `FB_CLIENT_I
|
|
29
29
|
## Usage
|
30
30
|
|
31
31
|
Fb::Auth#url
|
32
|
-
|
32
|
+
------------
|
33
33
|
|
34
34
|
The `url` method helps you obtain a URL where to redirect users who need to
|
35
35
|
authenticate with their Facebook account in order to use your application:
|
@@ -40,6 +40,9 @@ Fb::Auth.new(redirect_uri: redirect_uri).url
|
|
40
40
|
# => https://www.facebook.com/dialog/oauth?client_id=...&scope=manage_pages&redirect_uri=https%3A%2F%2Fexample.com%2Fauth
|
41
41
|
```
|
42
42
|
|
43
|
+
Note that access is always requested with permission to access email,
|
44
|
+
manage pages and read insights.
|
45
|
+
|
43
46
|
Fb::Auth#access_token
|
44
47
|
---------------------
|
45
48
|
|
@@ -56,69 +59,6 @@ Fb::Auth.new(redirect_uri: redirect_uri, code: code).access_token
|
|
56
59
|
# => "kefjej49s82hFS@2333233222FDh66"
|
57
60
|
```
|
58
61
|
|
59
|
-
Fb::User#pages
|
60
|
-
---------------------
|
61
|
-
|
62
|
-
Once you have successfully obtain an access token, you can fetch the pages managed
|
63
|
-
by that access token. Calling `Fb::User.new(access_token).pages` will return an
|
64
|
-
array of type Fb::Page, each with an id and name.
|
65
|
-
|
66
|
-
```ruby
|
67
|
-
access_token = Fb::Auth.new(redirect_uri: redirect_uri, code: code).access_token
|
68
|
-
Fb::User.new(access_token).pages
|
69
|
-
# => [#<Fb::Page: id="1234", name="sample1">, #<Fb::Page: id="5678", name="sample2">]
|
70
|
-
```
|
71
|
-
|
72
|
-
Fb::User#email
|
73
|
-
---------------------
|
74
|
-
|
75
|
-
Similarly, you can get the email of the user by calling `Fb::User.new(access_token).email`.
|
76
|
-
|
77
|
-
```ruby
|
78
|
-
access_token = Fb::Auth.new(redirect_uri: redirect_uri, code: code).access_token
|
79
|
-
Fb::User.new(access_token).email
|
80
|
-
# => "john.smith@example.com"
|
81
|
-
```
|
82
|
-
|
83
|
-
Fb::Page#insights
|
84
|
-
---------------------
|
85
|
-
|
86
|
-
For each page object, you can fetch page insights for these [available metrics](https://developers.facebook.com/docs/graph-api/reference/v2.9/insights#availmetrics). The insights method takes a hash of three options:
|
87
|
-
|
88
|
-
[String] :since The lower bound of the time range to consider.
|
89
|
-
[String] :period The aggregation period (must be available to all
|
90
|
-
given metrics).
|
91
|
-
[Array] :metric A list of metrics.
|
92
|
-
|
93
|
-
Insights will return a hash with the name of each metric as the key and the metric object as the value.
|
94
|
-
|
95
|
-
```ruby
|
96
|
-
options = {
|
97
|
-
metric: %i(page_fan_adds_unique page_engaged_users page_views_total),
|
98
|
-
period: :week,
|
99
|
-
since: '2017-06-09' # sample date format
|
100
|
-
}
|
101
|
-
page = Fb::User.new('token').pages.first
|
102
|
-
page.insights(options)
|
103
|
-
# => {"page_fan_adds_unique"=>#<Fb::Metric:0x123abc
|
104
|
-
# @name="page_fan_adds_unique", @description="Weekly: The
|
105
|
-
# number of new people who have liked your Page (Unique Users)",
|
106
|
-
# @value=123>,..}
|
107
|
-
```
|
108
|
-
|
109
|
-
Fb::Error
|
110
|
-
-------------
|
111
|
-
|
112
|
-
`Fb::Error` will be raised when an issue occurs during the Facebook authentication process.
|
113
|
-
The message of the error will include the details:
|
114
|
-
|
115
|
-
```ruby
|
116
|
-
redirect_uri = 'https://example.com/auth' # REPLACE WITH REAL ONE
|
117
|
-
code = 'invalid-code'
|
118
|
-
Fb::Auth.new(redirect_uri: redirect_uri, code: code).access_token
|
119
|
-
# => Fb::Error: Invalid verification code format.
|
120
|
-
```
|
121
|
-
|
122
62
|
## Development
|
123
63
|
|
124
64
|
After checking out the repo, run `bin/setup` to install dependencies.
|
data/fb-auth.gemspec
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
lib = File.expand_path(
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
4
|
+
require 'fb/auth/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'fb-auth'
|
8
8
|
spec.version = Fb::Auth::VERSION
|
9
9
|
spec.authors = ['Aaron Dao', 'Claudio Baccigalupo']
|
10
10
|
spec.email = ['aaron.dao@fullscreen.com', 'claudio@fullscreen.net']
|
@@ -12,19 +12,21 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.summary = %q{Ruby client to authenticate a Facebook user.}
|
13
13
|
spec.description = %q{Fb::Auth provides methods to obtain an access token to
|
14
14
|
manage pages of a Facebook user.}
|
15
|
-
spec.homepage =
|
16
|
-
spec.license =
|
15
|
+
spec.homepage = 'https://github.com/Fullscreen/fb-auth'
|
16
|
+
spec.license = 'MIT'
|
17
17
|
|
18
18
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
19
|
f.match(%r{^(test|spec|features)/})
|
20
20
|
end
|
21
|
-
spec.bindir =
|
21
|
+
spec.bindir = 'exe'
|
22
22
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
-
spec.require_paths = [
|
23
|
+
spec.require_paths = ['lib']
|
24
24
|
|
25
|
-
spec.
|
26
|
-
|
27
|
-
spec.add_development_dependency
|
28
|
-
spec.add_development_dependency
|
25
|
+
spec.add_dependency 'fb-support', '~> 1.0.0.alpha1'
|
26
|
+
|
27
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
28
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
29
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
30
|
+
spec.add_development_dependency 'yard', '~> 0.9.9'
|
29
31
|
spec.add_development_dependency 'coveralls'
|
30
32
|
end
|
data/lib/fb/auth.rb
CHANGED
@@ -1,10 +1,7 @@
|
|
1
|
-
require 'fb/
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
require 'fb/request'
|
6
|
-
# Ruby client to authenticate a Facebook user.
|
7
|
-
# @see http://www.rubydoc.info/gems/Fb/
|
1
|
+
require 'fb/support'
|
2
|
+
|
3
|
+
# An object-oriented Ruby client for the Facebook Graph API.
|
4
|
+
# @see http://www.rubydoc.info/gems/fb-core/
|
8
5
|
module Fb
|
9
6
|
# Provides methods to authenticate a user with the Facebook OAuth flow.
|
10
7
|
# @see https://developers.facebook.com/docs/facebook-login
|
@@ -19,44 +16,44 @@ module Fb
|
|
19
16
|
@code = options[:code]
|
20
17
|
end
|
21
18
|
|
22
|
-
# @return [String]
|
19
|
+
# @return [String] the url to authenticate as a Facebook user.
|
23
20
|
def url
|
24
|
-
|
21
|
+
HTTPRequest.new(url_options).url
|
25
22
|
end
|
26
23
|
|
27
|
-
# @return [String] the non-expiring access token of
|
24
|
+
# @return [String] the non-expiring access token of a Facebook user.
|
28
25
|
def access_token
|
29
|
-
|
30
|
-
params
|
31
|
-
|
26
|
+
params = {redirect_uri: @redirect_uri, code: @code}
|
27
|
+
temp_token = fetch_access_token_with params
|
28
|
+
|
29
|
+
params = {grant_type: :fb_exchange_token, fb_exchange_token: temp_token}
|
30
|
+
fetch_access_token_with params
|
32
31
|
end
|
33
32
|
|
34
33
|
private
|
35
34
|
|
36
|
-
def short_term_access_token
|
37
|
-
response_body = Fb::Request.new(path: '/oauth/access_token',
|
38
|
-
params: short_term_token_params).run
|
39
|
-
response_body["access_token"]
|
40
|
-
end
|
41
|
-
|
42
35
|
def url_options
|
43
|
-
url_params = {scope: 'email,manage_pages', redirect_uri: @redirect_uri}
|
44
36
|
{host: 'www.facebook.com', path: '/dialog/oauth', params: url_params}
|
45
37
|
end
|
46
38
|
|
47
|
-
def
|
39
|
+
def url_params
|
48
40
|
{}.tap do |params|
|
49
|
-
params[:
|
41
|
+
params[:client_id] = Fb.configuration.client_id
|
50
42
|
params[:redirect_uri] = @redirect_uri
|
51
|
-
params[:
|
43
|
+
params[:scope] = 'email,manage_pages,read_insights'
|
52
44
|
end
|
53
45
|
end
|
54
46
|
|
55
|
-
def
|
47
|
+
def fetch_access_token_with(params)
|
48
|
+
params = params.merge access_token_params
|
49
|
+
request = HTTPRequest.new path: '/oauth/access_token', params: params
|
50
|
+
request.run.body['access_token']
|
51
|
+
end
|
52
|
+
|
53
|
+
def access_token_params
|
56
54
|
{}.tap do |params|
|
55
|
+
params[:client_id] = Fb.configuration.client_id
|
57
56
|
params[:client_secret] = Fb.configuration.client_secret
|
58
|
-
params[:grant_type] = :fb_exchange_token
|
59
|
-
params[:fb_exchange_token] = short_term_access_token
|
60
57
|
end
|
61
58
|
end
|
62
59
|
end
|
data/lib/fb/auth/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fb-auth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0.alpha1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Dao
|
@@ -9,8 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2017-07-
|
12
|
+
date: 2017-07-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: fb-support
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 1.0.0.alpha1
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 1.0.0.alpha1
|
14
28
|
- !ruby/object:Gem::Dependency
|
15
29
|
name: bundler
|
16
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -105,12 +119,8 @@ files:
|
|
105
119
|
- fb-auth.gemspec
|
106
120
|
- lib/fb/auth.rb
|
107
121
|
- lib/fb/auth/version.rb
|
108
|
-
- lib/fb/configuration.rb
|
109
122
|
- lib/fb/error.rb
|
110
|
-
- lib/fb/metric.rb
|
111
|
-
- lib/fb/page.rb
|
112
123
|
- lib/fb/request.rb
|
113
|
-
- lib/fb/user.rb
|
114
124
|
homepage: https://github.com/Fullscreen/fb-auth
|
115
125
|
licenses:
|
116
126
|
- MIT
|
@@ -126,9 +136,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
126
136
|
version: '0'
|
127
137
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
138
|
requirements:
|
129
|
-
- - "
|
139
|
+
- - ">"
|
130
140
|
- !ruby/object:Gem::Version
|
131
|
-
version:
|
141
|
+
version: 1.3.1
|
132
142
|
requirements: []
|
133
143
|
rubyforge_project:
|
134
144
|
rubygems_version: 2.6.11
|
data/lib/fb/configuration.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
module Fb
|
2
|
-
# Setter for shared global objects
|
3
|
-
# @private
|
4
|
-
class Configuration
|
5
|
-
attr_accessor :client_id, :client_secret
|
6
|
-
|
7
|
-
def initialize
|
8
|
-
@client_id = ENV['FB_CLIENT_ID']
|
9
|
-
@client_secret = ENV['FB_CLIENT_SECRET']
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
# Returns the global [Configuration](Fb/Configuration) object. While
|
14
|
-
# you can use this method to access the configuration, the more common
|
15
|
-
# convention is to use [Fb.configure].
|
16
|
-
#
|
17
|
-
# @example
|
18
|
-
# Fb.configuration.client_id = 1234
|
19
|
-
def self.configuration
|
20
|
-
@configuration ||= Configuration.new
|
21
|
-
end
|
22
|
-
|
23
|
-
# Yields the global configuration to a block.
|
24
|
-
# @yield [Configuration] global configuration
|
25
|
-
#
|
26
|
-
# @example
|
27
|
-
# Fb.configure do |config|
|
28
|
-
# config.client_id = 1234
|
29
|
-
# end
|
30
|
-
def self.configure
|
31
|
-
yield(configuration) if block_given?
|
32
|
-
end
|
33
|
-
end
|
data/lib/fb/metric.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
# Ruby client to authenticate a Facebook user.
|
2
|
-
# @see http://www.rubydoc.info/gems/Fb/
|
3
|
-
module Fb
|
4
|
-
# Fb::Metric reprensents a Facebook page's metric.
|
5
|
-
# For a list of all available metrics, refer to:
|
6
|
-
# @see https://developers.facebook.com/docs/graph-api/reference/v2.9/insights
|
7
|
-
# Provides methods to read name, description, and value.
|
8
|
-
class Metric
|
9
|
-
|
10
|
-
# @return [String] the name of the metric (e.g. page_fans).
|
11
|
-
attr_reader :name
|
12
|
-
|
13
|
-
# @return [String] a description for the metric.
|
14
|
-
attr_reader :description
|
15
|
-
|
16
|
-
# @return [Integer] the value of the metric when last requested.
|
17
|
-
attr_reader :value
|
18
|
-
|
19
|
-
# @param [Hash] options the options to initialize an instance of Fb::Metric.
|
20
|
-
# @option [String] :name of the metric.
|
21
|
-
# @option [String] :title of the metric.
|
22
|
-
# @option [String] :description of the metric.
|
23
|
-
# @option [Integer] :value of this metric when last requested.
|
24
|
-
def initialize(options = {})
|
25
|
-
@name = options["name"]
|
26
|
-
@description = options["description"]
|
27
|
-
@value = options["values"].first["value"]
|
28
|
-
end
|
29
|
-
|
30
|
-
# @return [String] the representation of the metric.
|
31
|
-
def to_s
|
32
|
-
"#<#{self.class.name} name=#{@name}, description=#{@description}, value=#{@value}>"
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
data/lib/fb/page.rb
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
# Ruby client to authenticate a Facebook user.
|
2
|
-
# @see http://www.rubydoc.info/gems/Fb/
|
3
|
-
module Fb
|
4
|
-
# Fb::Page reprensents a Facebook page.
|
5
|
-
# Provides methods to get/set a page's name and id.
|
6
|
-
class Page
|
7
|
-
# @return [String] the name of the page.
|
8
|
-
attr_reader :name
|
9
|
-
|
10
|
-
#@return [String] the unique id of the page.
|
11
|
-
attr_reader :id
|
12
|
-
|
13
|
-
# @param [Hash] options the options to initialize an instance of Fb::Page.
|
14
|
-
# @option [String] :name The name of the page.
|
15
|
-
# @option [String] :id The unique id of the page.
|
16
|
-
def initialize(options = {})
|
17
|
-
@name = options["name"]
|
18
|
-
@id = options["id"]
|
19
|
-
@user = options["user"]
|
20
|
-
end
|
21
|
-
|
22
|
-
# @param [Hash] options to customize the insights returned from the API.
|
23
|
-
# @option [String] :since The lower bound of the time range to consider.
|
24
|
-
# @option [String] :period The aggregation period (must be available to all
|
25
|
-
# given metrics).
|
26
|
-
# @option [Array<String, Symbol>] :metric A list of metrics.
|
27
|
-
# @return [Hash] a collection of metrics with metric name as key
|
28
|
-
# and metric object as value.
|
29
|
-
# @example
|
30
|
-
# page = Fb::User.new('token').pages.first
|
31
|
-
# page.insights(options)
|
32
|
-
# => {"page_fan_adds_unique"=>#<Fb::Metric:0x123abc
|
33
|
-
# @name="page_fans", @description="Weekly: The
|
34
|
-
# number of new people who have liked your Page (Unique Users)",
|
35
|
-
# @value=123>,..}
|
36
|
-
def insights(options = {})
|
37
|
-
fetch_insights(options)["data"].map do |metric_data|
|
38
|
-
[metric_data["name"], Fb::Metric.new(metric_data)]
|
39
|
-
end.to_h
|
40
|
-
end
|
41
|
-
|
42
|
-
# @return [String] the representation of the page.
|
43
|
-
def to_s
|
44
|
-
"#<#{self.class.name} id=#{@id}, name=#{@name}>"
|
45
|
-
end
|
46
|
-
|
47
|
-
private
|
48
|
-
|
49
|
-
def fetch_insights(options)
|
50
|
-
unless options[:metric]
|
51
|
-
raise Fb::Error, 'Missing required parameter: metric'
|
52
|
-
end
|
53
|
-
insights_params = options.merge(metric: options[:metric].join(","),
|
54
|
-
access_token: @user.send(:access_token))
|
55
|
-
Fb::Request.new(
|
56
|
-
path: "/v2.9/#{@id}/insights",
|
57
|
-
params: insights_params
|
58
|
-
).run
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
data/lib/fb/user.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
module Fb
|
2
|
-
# Provides methods to get a collection of pages that an access token is
|
3
|
-
# allowed to manage and get page insights on those pages.
|
4
|
-
class User
|
5
|
-
# @access_token The access token returned by Facebook's OAuth flow.
|
6
|
-
def initialize(access_token)
|
7
|
-
@access_token = access_token
|
8
|
-
end
|
9
|
-
|
10
|
-
# @return [String] the email of the Facebook user.
|
11
|
-
def email
|
12
|
-
@email ||= begin
|
13
|
-
response_body = Fb::Request.new(path: '/me',
|
14
|
-
params: {fields: :email, access_token: @access_token}).run
|
15
|
-
response_body["email"]
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
# @return [Array] a collection of pages available to the given access token.
|
20
|
-
def pages
|
21
|
-
@pages ||= begin
|
22
|
-
response_body = Fb::Request.new(path: '/me/accounts',
|
23
|
-
params: {access_token: @access_token}).run
|
24
|
-
response_body["data"].map do |page_data|
|
25
|
-
Fb::Page.new page_data.merge('user' => self)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
attr_reader :access_token
|
32
|
-
end
|
33
|
-
end
|