fbk 1.0.0 → 1.1.0

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -17
  3. data/lib/fbk.rb +32 -13
  4. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ba8be13d4f86aa5b5ab9be02af0bc32174272352
4
- data.tar.gz: 99f4aec85639c0ad08389054f271474ff200eda1
3
+ metadata.gz: 8be4d53c3664aa08a1456e7576dd54cd28738815
4
+ data.tar.gz: 0784b66698e16e4e5205a8034c3933060bf2eb3f
5
5
  SHA512:
6
- metadata.gz: b98818dd423486383145be375647171d8dee114df185b88925044a4d5fe83b71f15aca2da1a189b0ad7a720fee1aad7b3873063e2170014cdfa64998de8190ea
7
- data.tar.gz: 331b149bf07373ae9bae89e2b44594071fc4922a352e740da7cdd1c1fdb779d20934c2a254776ecb467fc1709bb8395bedf5c3670fb73393bc20df09a3347b84
6
+ metadata.gz: 4c7c5f7b0ce66b40d2cf631d5daf6c8d9ed0fea52d1493fab63228fed6ebb688c3fe155b641d4a44c2d94df3537f5c4fe48099f7e81571c316260e7f0fc874a2
7
+ data.tar.gz: 3e78182652ac7ab705bb3affa50605baa4c86dd53a2bed3d5108eefd6ad9c0e91e5aa0d37f4cb3205a2e5470fc400d3b202a45e1cede1bf4cdb7c3857862de35
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # FBK
2
2
 
3
- This probably isn't what you're looking for.
3
+ You can use this library to implement Facebook login in your Ruby application.
4
4
 
5
5
  We use this at [Chicisimo](https://github.com/chicisimo).
6
6
 
@@ -10,25 +10,22 @@ We use this at [Chicisimo](https://github.com/chicisimo).
10
10
  require "sinatra"
11
11
  require "fbk"
12
12
 
13
- get "/login/fb" do
14
- client_id = "xxxx"
15
- redirect_uri = "https://something.com/login/fb/token"
13
+ REDIRECT_URI = "https://something.com/login/fb/token"
14
+
15
+ FBK.configure do |config|
16
+ config.client_id = "xxx"
17
+ config.client_secret = "xxx"
18
+ end
16
19
 
17
- redirect to("https://www.facebook.com/v2.0/dialog/oauth?client_id=#{client_id}&redirect_uri=#{redirect_uri}")
20
+ get "/login/fb" do
21
+ redirect to(FBK.authorize_uri(redirect_uri: REDIRECT_URI, scope: %w{public_profile}))
18
22
  end
19
23
 
20
24
  get "/login/fb/token" do
21
- client_id = "xxxx"
22
- client_secret = "xxxx"
23
- redirect_uri = "https://something.com/login/fb/token"
24
- code = params[:code]
25
-
26
- token = FBK.get_access_token(
27
- client_id: client_id,
28
- client_secret: client_secret,
29
- redirect_uri: redirect_uri,
30
- code: code) # => "H3UUnvsoymXtinqyofZtHqpqmTNkbJ7x5bpQFeFDGcmAy2THf9pfug2Eyx2ZSTnrqfMcDrUguzaytn4OZHCOyC6RsjEEM2MwHGWGVMwpzuSjWcjE1aS0dVPCTRrNm4bf6YmgVDS7JuUDb3Lxt0AoI0ucTmPdvnbrnkmunszoa0Y9KMJXyh2Nkf4PFgOo2bmjo6rVKzTY"
31
-
32
- user = FBK.get_user_info(token) # => {:id=>"xxxx", :first_name=>"Pedro", :gender=>"male", :last_name=>"Giménez", :link=>"https://www.facebook.com/pedrotgimenez", :locale=>"en_US", :name=>"Pedro Giménez", :timezone=>1, :updated_time=>"1955-02-24T00:00:00+0000", :username=>"pedrotgimenez", :verified=>true}
25
+ code = params[:code]
26
+
27
+ access_token = FBK.get_access_token(redirect_uri: REDIRECT_URI, code: code) # => "H3UUnvsoymXtinqyofZtHqpqmTNkbJ7x5bpQFeFDGcmAy2THf9pfug2Eyx2ZSTnrqfMcDrUguzaytn4OZHCOyC6RsjEEM2MwHGWGVMwpzuSjWcjE1aS0dVPCTRrNm4bf6YmgVDS7JuUDb3Lxt0AoI0ucTmPdvnbrnkmunszoa0Y9KMJXyh2Nkf4PFgOo2bmjo6rVKzTY"
28
+
29
+ user = FBK.get_user_info(access_token) # => {:id=>"xxxx", :first_name=>"Pedro", :gender=>"male", :last_name=>"Giménez", :link=>"https://www.facebook.com/pedrotgimenez", :locale=>"en_US", :name=>"Pedro Giménez", :timezone=>1, :updated_time=>"1955-02-24T00:00:00+0000", :username=>"pedrotgimenez", :verified=>true}
33
30
  end
34
31
  ```
data/lib/fbk.rb CHANGED
@@ -1,35 +1,54 @@
1
1
  require "json"
2
2
  require "nestful"
3
3
 
4
- class FBK
5
- FACEBOOK_URL = "https://graph.facebook.com"
4
+ module FBK
5
+ extend self
6
6
 
7
- def self.get_access_token(params)
8
- query = parse_params(params)
7
+ FACEBOOK_URL = "https://facebook.com"
8
+ FACEBOOK_GRAPH_URL = "https://graph.facebook.com/v2.0"
9
9
 
10
- response = Nestful.get("#{FACEBOOK_URL}/oauth/access_token?#{query}").body
10
+ attr_writer :client_id, :client_secret
11
+
12
+ def configure
13
+ yield self
14
+ end
15
+
16
+ def authorize_uri(params)
17
+ params[:client_id] = @client_id
18
+ params[:scope] = params[:scope].join(",")
19
+
20
+ query_string = params_to_query_string(params)
21
+
22
+ "#{FACEBOOK_URL}/dialog/oauth?#{query_string}"
23
+ end
24
+
25
+ def get_access_token(params)
26
+ params[:client_id] = @client_id
27
+ params[:client_secret] = @client_secret
28
+
29
+ query_string = params_to_query_string(params)
30
+
31
+ response = Nestful.get("#{FACEBOOK_GRAPH_URL}/oauth/access_token?#{query_string}").body
11
32
  response = parse_response(response)
12
33
 
13
34
  response[:access_token]
14
35
  end
15
36
 
16
- def self.get_user_info(token)
17
- response = Nestful.get("#{FACEBOOK_URL}/me?access_token=#{token}")
18
- JSON.parse(response.body, symbolize_names: true)
37
+ def get_user_info(token)
38
+ response = Nestful.get("#{FACEBOOK_GRAPH_URL}/me?access_token=#{token}").body
39
+ JSON.parse(response, symbolize_names: true)
19
40
  end
20
41
 
21
42
  private
22
43
 
23
- def self.parse_params(params)
24
- params.each_with_object([]) { |(k,v), query| query << "#{k}=#{v}" }.join("&")
44
+ def params_to_query_string(params)
45
+ params.map { |k,v| "#{k}=#{v}" }.join("&")
25
46
  end
26
47
 
27
- def self.parse_response(response)
48
+ def parse_response(response)
28
49
  response.split("&").each_with_object({}) do |parameter, hash|
29
50
  key, value = parameter.split("=")
30
51
  hash[key.to_sym] = value
31
52
  end
32
53
  end
33
-
34
- private_class_method :parse_params, :parse_response
35
54
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fbk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pedro Gimenez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-09 00:00:00.000000000 Z
11
+ date: 2015-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nestful
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.0'
41
- description: Interacts with some parts of the FB Graph API.
41
+ description: Interacts with Facebook's Graph API.
42
42
  email:
43
43
  - pedro@chicisimo.com
44
44
  executables: []
@@ -71,5 +71,5 @@ rubyforge_project:
71
71
  rubygems_version: 2.2.0
72
72
  signing_key:
73
73
  specification_version: 4
74
- summary: Interacts with some parts of the FB Graph API.
74
+ summary: Interacts with Facebook's Graph API.
75
75
  test_files: []