fobos 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +64 -0
- data/Rakefile +2 -0
- data/fobos.gemspec +25 -0
- data/lib/fobos.rb +5 -0
- data/lib/fobos/graph_api.rb +7 -0
- data/lib/fobos/graph_api/o_auth.rb +91 -0
- data/lib/fobos/graph_api/pages.rb +58 -0
- data/lib/fobos/graph_api/users.rb +49 -0
- data/lib/fobos/version.rb +3 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 80db1bacf6dad8f1052ece5a78d87915bc1541e0
|
4
|
+
data.tar.gz: bff2eb2103c9c469f458cb85e4fd6797313f577a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 929cd0c4f8c94f985edfcd3f02a0be9148cab0b1819ee522ae9ae24ef045f08e1e8fa21600af0e443deff09ac3163ecae7c6056821e68adce743e3f77793c758
|
7
|
+
data.tar.gz: 51f3285424929faeda857505c7618f5cdc31934b525b239f8f117d777842106de71838285426d99832e8aa229a68275c3f341dfd4ca96f1612aaba211c9fe40b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Max Goncharov
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Fobos
|
2
|
+
|
3
|
+
Fobos is a easy to use, based on HTTParty gem for working with Facebook Graph and REST API.
|
4
|
+
|
5
|
+
|
6
|
+
It's Ruby based, so it compatible with any frameworks like Rails, Sinatra, etc.
|
7
|
+
|
8
|
+
In current version it works only with basic Facebook Graph API features. In next versions REST API and new features for Graph API will be added.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'fobos'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install fobos
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
At first you need to initialize OAuth.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
oauth = Fobos::GraphAPI::OAuth.new(app_id, app_secret, oauth_callback_url)
|
31
|
+
```
|
32
|
+
|
33
|
+
Then call method which generate URL for getting access code.
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
access_code_url = oauth.get_user_access_code_url
|
37
|
+
```
|
38
|
+
|
39
|
+
**OR**, you can make HTTP request.
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
oauth.get_user_access_code
|
43
|
+
```
|
44
|
+
|
45
|
+
Now you can get access token from access code.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
oauth.get_user_access_token
|
49
|
+
```
|
50
|
+
|
51
|
+
On oauth_callback_url facebook will send token in params.
|
52
|
+
|
53
|
+
|
54
|
+
With access token you can make requests to Facebook Graph API.
|
55
|
+
|
56
|
+
Use Fobos::GraphAPI::Users for working with user's data and Fobos::GraphAPI::Pages for working with Facebook Pages.
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
1. Fork it ( https://github.com/[my-github-username]/fobos/fork )
|
61
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
62
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
63
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
64
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/fobos.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fobos/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fobos"
|
8
|
+
spec.version = Fobos::VERSION
|
9
|
+
spec.authors = ["Max Goncharov"]
|
10
|
+
spec.email = ["mxgoncahrov@gmail.com"]
|
11
|
+
spec.summary = %q{Fobos is based on HTTParty gem for using Facebook Graph and REST API.}
|
12
|
+
spec.description = %q{}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
|
24
|
+
spec.add_dependency "httparty", "~> 0.13"
|
25
|
+
end
|
data/lib/fobos.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
module Fobos
|
2
|
+
module GraphAPI
|
3
|
+
# Provides generating of access codes and access tokens for working with Facebook API.
|
4
|
+
#
|
5
|
+
#
|
6
|
+
# Need HTTParty for making HTTP calls.
|
7
|
+
# More about HTTParty here: https://github.com/jnunemaker/httparty
|
8
|
+
|
9
|
+
|
10
|
+
class OAuth
|
11
|
+
require 'cgi'
|
12
|
+
include HTTParty
|
13
|
+
|
14
|
+
|
15
|
+
# ID of your Facebook App. You can get in your app's manage page.
|
16
|
+
attr_accessor :app_id
|
17
|
+
# Secret key of your Facebook App. You can get in your app's manage page.
|
18
|
+
attr_accessor :app_secret
|
19
|
+
# URL for redirecting after FB action on FB server-side. <b>NOTE:</b> callback_url must be the same for get_oauth_url and get_user_access_token_url
|
20
|
+
attr_accessor :oauth_callback_url
|
21
|
+
# Store long-live access token (set it manually).
|
22
|
+
attr_accessor :access_token
|
23
|
+
|
24
|
+
# Store Facebook default URL
|
25
|
+
FB_URI = 'https://www.facebook.com'
|
26
|
+
# Store Facebook Graph API URL
|
27
|
+
GRAPH_URI = 'https://graph.facebook.com'
|
28
|
+
|
29
|
+
# Initialize new object for Facebook OAuth actions.
|
30
|
+
def initialize(app_id, app_secret, oauth_callback_url)
|
31
|
+
@app_id = app_id
|
32
|
+
@app_secret = app_secret
|
33
|
+
@oauth_callback_url = oauth_callback_url
|
34
|
+
end
|
35
|
+
|
36
|
+
# Generate link for getting user's "access code" (<b>NOT ACCESS TOKEN</b>). After you make call of this link FB redirect you to @oauth_callback_url with "code" in params.
|
37
|
+
#
|
38
|
+
# <b>Options</b> you can see here https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/v2.1#login
|
39
|
+
def get_user_access_code_url(oauth_callback_url = @oauth_callback_url, options = {})
|
40
|
+
options_part = ''
|
41
|
+
options_part = "&" + options.map {|k,v| "#{k}=#{v.kind_of?(Array) ? v.join(',') : v}" }.join('&') unless options.empty?
|
42
|
+
|
43
|
+
query = "/dialog/oauth?client_id=#{@app_id}&redirect_uri=#{oauth_callback_url}#{options_part}"
|
44
|
+
|
45
|
+
(FB_URI.to_s + query.to_s).to_s
|
46
|
+
end
|
47
|
+
|
48
|
+
# Provide call of link what is result of get_user_access_code_url
|
49
|
+
def get_user_access_code(oauth_callback_url = @oauth_callback_url, options = {})
|
50
|
+
uri = get_user_access_code_url(oauth_callback_url, options)
|
51
|
+
self.class.get(uri)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Generate link for getting user's <b>ACCESS TOKEN</b> by code given with get_user_access_code
|
55
|
+
def get_user_access_token_url(oauth_callback_url = @oauth_callback_url, code)
|
56
|
+
query = "/oauth/access_token?client_id=#{@app_id}&client_secret=#{@app_secret}&code=#{code}&redirect_uri=#{oauth_callback_url}"
|
57
|
+
|
58
|
+
GRAPH_URI.to_s + query.to_s
|
59
|
+
end
|
60
|
+
|
61
|
+
# Provide call of link what is result of get_user_access_token_url. Returned parsed access_token.
|
62
|
+
def get_user_access_token(oauth_callback_url = @oauth_callback_url, code)
|
63
|
+
response = self.class.get(get_user_access_token_url(oauth_callback_url, code))
|
64
|
+
parsed_params = CGI::parse(response.parsed_response)
|
65
|
+
parsed_params["access_token"].first
|
66
|
+
end
|
67
|
+
|
68
|
+
# Return users access code from params which returned by Facebook
|
69
|
+
def get_user_access_code_from_params(params)
|
70
|
+
code = params[:code]
|
71
|
+
if code.nil?
|
72
|
+
raise 'An error has occurred. You code is nil. Please check is your actions is valid.'
|
73
|
+
else
|
74
|
+
code
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Generate link for getting long-live token (~60 days) instead short-live token (~1-2 hours)
|
79
|
+
def get_user_long_live_access_token_url(access_token)
|
80
|
+
query = "/oauth/access_token?grant_type=fb_exchange_token&client_id=#{app_id}&client_secret=#{app_secret}&fb_exchange_token=#{access_token}"
|
81
|
+
|
82
|
+
GRAPH_URI.to_s + query.to_s
|
83
|
+
end
|
84
|
+
|
85
|
+
# Provide call of link what is result of get_user_long_live_access_token_url
|
86
|
+
def get_user_long_live_access_token(access_token)
|
87
|
+
self.class.get(get_user_long_live_access_token_url(access_token))
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Fobos
|
2
|
+
module GraphAPI
|
3
|
+
class Pages
|
4
|
+
include HTTParty
|
5
|
+
|
6
|
+
# You can get access token with Fobos::GraphAPI::Oauth.
|
7
|
+
attr_accessor :access_token
|
8
|
+
|
9
|
+
# Store Facebook default URL
|
10
|
+
FB_URI = 'https://www.facebook.com'
|
11
|
+
# Store Facebook Graph API URL
|
12
|
+
GRAPH_URI = 'https://graph.facebook.com'
|
13
|
+
|
14
|
+
# Need access token for making calls.
|
15
|
+
def initialize(access_token)
|
16
|
+
@access_token = access_token
|
17
|
+
end
|
18
|
+
|
19
|
+
# Return list of user's accounts.
|
20
|
+
#
|
21
|
+
# Use access_token from Fobos::GraphAPI::Pages.new
|
22
|
+
def get_accounts
|
23
|
+
user = Fobos::GraphAPI::Users.new(@access_token)
|
24
|
+
user.get_request(fields: 'accounts')
|
25
|
+
end
|
26
|
+
|
27
|
+
# Publishing post to the page wall.
|
28
|
+
#
|
29
|
+
# If you call it with <b>USER_ACCESS_TOKEN</b> it will publish post from user!
|
30
|
+
#
|
31
|
+
# If you want publish something from Page you need call it with <b>PAGE_ACCESS_TOKEN</b>. You can get it from list of user's account returned by Fobos::GraphAPI::Pages.get_accounts.
|
32
|
+
def post_to_page(page_id, page_access_token, options = {})
|
33
|
+
options_part = ''
|
34
|
+
options_part = options.map {|k,v| "#{k}=#{v.kind_of?(Array) ? v.join(',') : v}" }.join('&') unless options.empty?
|
35
|
+
|
36
|
+
query = GRAPH_URI.to_s + "/#{page_id}" + "/feed?#{options_part}&access_token=#{page_access_token}"
|
37
|
+
|
38
|
+
encoded_url = URI.encode(query)
|
39
|
+
|
40
|
+
puts encoded_url
|
41
|
+
|
42
|
+
self.class.post(URI.parse(encoded_url))
|
43
|
+
end
|
44
|
+
|
45
|
+
# Return feed list of page.
|
46
|
+
#
|
47
|
+
# Use access_token from Fobos::GraphAPI::Pages.new
|
48
|
+
def get_feed(page_id, options = {})
|
49
|
+
options_part = ''
|
50
|
+
options_part = options.map {|k,v| "#{k}=#{v.kind_of?(Array) ? v.join(',') : v}" }.join('&') unless options.empty?
|
51
|
+
|
52
|
+
query = GRAPH_URI.to_s + "/#{page_id}" + "/feed?#{options_part}&access_token=#{@access_token}"
|
53
|
+
|
54
|
+
self.class.get(query)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Fobos
|
2
|
+
module GraphAPI
|
3
|
+
class Users
|
4
|
+
include HTTParty
|
5
|
+
|
6
|
+
# You can get access token with Fobos::GraphAPI::Oauth.
|
7
|
+
attr_accessor :access_token
|
8
|
+
|
9
|
+
# Store Facebook default URL
|
10
|
+
FB_URI = 'https://www.facebook.com'
|
11
|
+
# Store Facebook Graph API URL
|
12
|
+
GRAPH_URI = 'https://graph.facebook.com'
|
13
|
+
|
14
|
+
# Need access token for making calls.
|
15
|
+
def initialize(access_token)
|
16
|
+
@access_token = access_token
|
17
|
+
end
|
18
|
+
|
19
|
+
# Provides getting user's data with options.
|
20
|
+
# Options must be a hash. You can provide value as String or Array.
|
21
|
+
#
|
22
|
+
# E.g. Fobos::GraphAPI::Users.new(access_token).get_request(fields: 'id,first_name,last_name') will return ID, FIRST NAME and LAST NAME of user.
|
23
|
+
def get_request(options = {})
|
24
|
+
options_part = ''
|
25
|
+
options_part = options.map {|k,v| "#{k}=#{v.kind_of?(Array) ? v.join(',') : v}" }.join('&') unless options.empty?
|
26
|
+
|
27
|
+
query = GRAPH_URI.to_s + '/v2.1' '/me?' + options_part + "&access_token=#{@access_token}"
|
28
|
+
|
29
|
+
self.class.get(query)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Provides sending post request (publishing) for user.
|
33
|
+
# Options must be a hash. You can provide value as String or Array.
|
34
|
+
#
|
35
|
+
# E.g. Fobos::GraphAPI::Users.new(access_token).post_request(message: 'This is test message') will post "This is test message" to user's feed.
|
36
|
+
def post_request(options = {})
|
37
|
+
options_part = ''
|
38
|
+
options_part = options.map {|k,v| "#{k}=#{v.kind_of?(Array) ? v.join(',') : v}" }.join('&') unless options.empty?
|
39
|
+
|
40
|
+
query = GRAPH_URI.to_s + '/v2.1' '/me/feed?' + options_part + "&access_token=#{@access_token}"
|
41
|
+
|
42
|
+
encoded_url = URI.encode(query)
|
43
|
+
|
44
|
+
self.class.post(URI.parse(encoded_url))
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fobos
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Max Goncharov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: httparty
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.13'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.13'
|
55
|
+
description: ''
|
56
|
+
email:
|
57
|
+
- mxgoncahrov@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- fobos.gemspec
|
68
|
+
- lib/fobos.rb
|
69
|
+
- lib/fobos/graph_api.rb
|
70
|
+
- lib/fobos/graph_api/o_auth.rb
|
71
|
+
- lib/fobos/graph_api/pages.rb
|
72
|
+
- lib/fobos/graph_api/users.rb
|
73
|
+
- lib/fobos/version.rb
|
74
|
+
homepage: ''
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.2.2
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Fobos is based on HTTParty gem for using Facebook Graph and REST API.
|
98
|
+
test_files: []
|