Skip to content

Commit

Permalink
Merge pull request #21 from Fullscreen/focus-on-auth
Browse files Browse the repository at this point in the history
Extract non-auth methods into separate libraries
  • Loading branch information
claudiofullscreen authored Jul 24, 2017
2 parents 2c13f07 + 09f93a6 commit f8300c2
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 431 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ For more information about changelogs, check
[Keep a Changelog](http://keepachangelog.com) and
[Vandamme](http://tech-angels.github.io/vandamme).

## 1.0.0 - 2017/07/24

This library was originally intended to provide methods to authenticate but
has grown to include other methods to configure, get posts, insights etc.
Most of those methods have been extracted into separate libraries.

* [REMOVAL] Moved Fb::Page to fb-core gem (without insights)
* [REMOVAL] Moved Fb::User to fb-core gem
* [REMOVAL] Moved Fb::Configuration to fb-support gem

## 0.1.3 - 2017/07/20

* [BUGFIX] Fixed `require` statement not loading for some classes.
Expand Down
68 changes: 4 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ By default, Fb::Auth will look for the environment variables called `FB_CLIENT_I
## Usage

Fb::Auth#url
---------------------
------------

The `url` method helps you obtain a URL where to redirect users who need to
authenticate with their Facebook account in order to use your application:
Expand All @@ -40,6 +40,9 @@ Fb::Auth.new(redirect_uri: redirect_uri).url
# => https://www.facebook.com/dialog/oauth?client_id=...&scope=manage_pages&redirect_uri=https%3A%2F%2Fexample.com%2Fauth
```

Note that access is always requested with permission to access email,
manage pages and read insights.

Fb::Auth#access_token
---------------------

Expand All @@ -56,69 +59,6 @@ Fb::Auth.new(redirect_uri: redirect_uri, code: code).access_token
# => "kefjej49s82hFS@2333233222FDh66"
```

Fb::User#pages
---------------------

Once you have successfully obtain an access token, you can fetch the pages managed
by that access token. Calling `Fb::User.new(access_token).pages` will return an
array of type Fb::Page, each with an id and name.

```ruby
access_token = Fb::Auth.new(redirect_uri: redirect_uri, code: code).access_token
Fb::User.new(access_token).pages
# => [#<Fb::Page: id="1234", name="sample1">, #<Fb::Page: id="5678", name="sample2">]
```

Fb::User#email
---------------------

Similarly, you can get the email of the user by calling `Fb::User.new(access_token).email`.

```ruby
access_token = Fb::Auth.new(redirect_uri: redirect_uri, code: code).access_token
Fb::User.new(access_token).email
# => "[email protected]"
```

Fb::Page#insights
---------------------

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:

[String] :since The lower bound of the time range to consider.
[String] :period The aggregation period (must be available to all
given metrics).
[Array] :metric A list of metrics.

Insights will return a hash with the name of each metric as the key and the metric object as the value.

```ruby
options = {
metric: %i(page_fan_adds_unique page_engaged_users page_views_total),
period: :week,
since: '2017-06-09' # sample date format
}
page = Fb::User.new('token').pages.first
page.insights(options)
# => {"page_fan_adds_unique"=>#<Fb::Metric:0x123abc
# @name="page_fan_adds_unique", @description="Weekly: The
# number of new people who have liked your Page (Unique Users)",
# @value=123>,..}
```

Fb::Error
-------------

`Fb::Error` will be raised when an issue occurs during the Facebook authentication process.
The message of the error will include the details:

```ruby
redirect_uri = 'https://example.com/auth' # REPLACE WITH REAL ONE
code = 'invalid-code'
Fb::Auth.new(redirect_uri: redirect_uri, code: code).access_token
# => Fb::Error: Invalid verification code format.
```

## Development

After checking out the repo, run `bin/setup` to install dependencies.
Expand Down
24 changes: 13 additions & 11 deletions fb-auth.gemspec
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
# coding: utf-8
lib = File.expand_path("../lib", __FILE__)
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "fb/auth/version"
require 'fb/auth/version'

Gem::Specification.new do |spec|
spec.name = "fb-auth"
spec.name = 'fb-auth'
spec.version = Fb::Auth::VERSION
spec.authors = ['Aaron Dao', 'Claudio Baccigalupo']
spec.email = ['[email protected]', '[email protected]']

spec.summary = %q{Ruby client to authenticate a Facebook user.}
spec.description = %q{Fb::Auth provides methods to obtain an access token to
manage pages of a Facebook user.}
spec.homepage = "https://github.com/Fullscreen/fb-auth"
spec.license = "MIT"
spec.homepage = 'https://github.com/Fullscreen/fb-auth'
spec.license = 'MIT'

spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.bindir = "exe"
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.require_paths = ['lib']

spec.add_development_dependency "bundler", "~> 1.15"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "yard", "~> 0.9.9"
spec.add_dependency 'fb-support', '~> 1.0.0.alpha1'

spec.add_development_dependency 'bundler', '~> 1.15'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'yard', '~> 0.9.9'
spec.add_development_dependency 'coveralls'
end
49 changes: 23 additions & 26 deletions lib/fb/auth.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
require 'fb/configuration'
require 'fb/user'
require 'fb/page'
require 'fb/metric'
require 'fb/request'
# Ruby client to authenticate a Facebook user.
# @see http://www.rubydoc.info/gems/Fb/
require 'fb/support'

# An object-oriented Ruby client for the Facebook Graph API.
# @see http://www.rubydoc.info/gems/fb-core/
module Fb
# Provides methods to authenticate a user with the Facebook OAuth flow.
# @see https://developers.facebook.com/docs/facebook-login
Expand All @@ -19,44 +16,44 @@ def initialize(options = {})
@code = options[:code]
end

# @return [String] a url to Facebook's account authentication.
# @return [String] the url to authenticate as a Facebook user.
def url
Fb::Request.new(url_options).url
HTTPRequest.new(url_options).url
end

# @return [String] the non-expiring access token of an authenticated Facebook account.
# @return [String] the non-expiring access token of a Facebook user.
def access_token
response_body = Fb::Request.new(path: '/oauth/access_token',
params: long_term_token_params).run
response_body["access_token"]
params = {redirect_uri: @redirect_uri, code: @code}
temp_token = fetch_access_token_with params

params = {grant_type: :fb_exchange_token, fb_exchange_token: temp_token}
fetch_access_token_with params
end

private

def short_term_access_token
response_body = Fb::Request.new(path: '/oauth/access_token',
params: short_term_token_params).run
response_body["access_token"]
end

def url_options
url_params = {scope: 'email,manage_pages', redirect_uri: @redirect_uri}
{host: 'www.facebook.com', path: '/dialog/oauth', params: url_params}
end

def short_term_token_params
def url_params
{}.tap do |params|
params[:client_secret] = Fb.configuration.client_secret
params[:client_id] = Fb.configuration.client_id
params[:redirect_uri] = @redirect_uri
params[:code] = @code
params[:scope] = 'email,manage_pages,read_insights'
end
end

def long_term_token_params
def fetch_access_token_with(params)
params = params.merge access_token_params
request = HTTPRequest.new path: '/oauth/access_token', params: params
request.run.body['access_token']
end

def access_token_params
{}.tap do |params|
params[:client_id] = Fb.configuration.client_id
params[:client_secret] = Fb.configuration.client_secret
params[:grant_type] = :fb_exchange_token
params[:fb_exchange_token] = short_term_access_token
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fb/auth/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module Fb
class Auth
# @return [String] the SemVer-compatible gem version.
# @see http://semver.org
VERSION = "0.1.3"
VERSION = '1.0.0.alpha1'
end
end
33 changes: 0 additions & 33 deletions lib/fb/configuration.rb

This file was deleted.

35 changes: 0 additions & 35 deletions lib/fb/metric.rb

This file was deleted.

61 changes: 0 additions & 61 deletions lib/fb/page.rb

This file was deleted.

Loading

0 comments on commit f8300c2

Please sign in to comment.