facebook-graph 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +19 -0
- data/README +11 -0
- data/lib/facebook-graph.rb +31 -0
- data/lib/facebook-graph/base/authentication.rb +31 -0
- data/lib/facebook-graph/base/client.rb +29 -0
- data/lib/facebook-graph/base/graph_node.rb +39 -0
- data/lib/facebook-graph/helpers/helper.rb +42 -0
- data/lib/facebook-graph/helpers/http_helper.rb +19 -0
- metadata +88 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010 Pedro Fracarolli
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module FacebookGraph
|
2
|
+
|
3
|
+
autoload :HTTPHelper, 'facebook-graph/helpers/http_helper.rb'
|
4
|
+
autoload :Helper, 'facebook-graph/helpers/helper.rb'
|
5
|
+
autoload :Client, 'facebook-graph/base/client.rb'
|
6
|
+
autoload :Authentication, 'facebook-graph/base/authentication.rb'
|
7
|
+
autoload :GraphNode, 'facebook-graph/base/graph_node.rb'
|
8
|
+
|
9
|
+
|
10
|
+
@@configuration = {
|
11
|
+
:base_uri => 'https://graph.facebook.com',
|
12
|
+
:search_uri => '/search',
|
13
|
+
:authorize_uri => '/oauth/authorize',
|
14
|
+
:access_token_uri => '/oauth/access_token'
|
15
|
+
}
|
16
|
+
|
17
|
+
class << self
|
18
|
+
|
19
|
+
# Enables custom url and parameter configuration
|
20
|
+
def configure(configuration={})
|
21
|
+
@@configuration.merge!(configuration)
|
22
|
+
end
|
23
|
+
|
24
|
+
def configuration
|
25
|
+
@@configuration
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class FacebookGraph::Authentication
|
2
|
+
include FacebookGraph::Helper
|
3
|
+
|
4
|
+
def initialize(client_id, client_secret, redirect_uri=nil)
|
5
|
+
@client_id, @client_secret = client_id, client_secret
|
6
|
+
@redirect_uri = redirect_uri
|
7
|
+
end
|
8
|
+
|
9
|
+
def client_id_and_secret
|
10
|
+
[client_id, client_secret]
|
11
|
+
end
|
12
|
+
|
13
|
+
def user_authorization_uri(options={})
|
14
|
+
config = FacebookGraph.configuration
|
15
|
+
uri = "#{config[:base_uri] + config[:authorize_uri]}?client_id=#{@client_id}"
|
16
|
+
uri += "&redirect_uri=#{CGI.escape(@redirect_uri)}" if @redirect_uri
|
17
|
+
uri += "&hash_to_querystring(options)" unless options.nil? || options.empty?
|
18
|
+
uri
|
19
|
+
end
|
20
|
+
|
21
|
+
def access_token code
|
22
|
+
config = FacebookGraph.configuration
|
23
|
+
parse_form_data("#{config[:base_uri] + config[:access_token_uri]}?client_id=#{@client_id}" +
|
24
|
+
"&client_secret=#{@client_secret}&code=#{CGI.escape(code)}" +
|
25
|
+
"&redirect_uri=#{CGI.escape(@redirect_uri)}")['access_token'][0]
|
26
|
+
end
|
27
|
+
|
28
|
+
attr_reader :client_id, :client_secret
|
29
|
+
attr_accessor :redirect_uri
|
30
|
+
end
|
31
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class FacebookGraph::Client
|
2
|
+
include FacebookGraph::Helper
|
3
|
+
|
4
|
+
def initialize(access_token=nil)
|
5
|
+
@access_token = access_token
|
6
|
+
end
|
7
|
+
|
8
|
+
def get_node(element_id, options={})
|
9
|
+
config = FacebookGraph.configuration
|
10
|
+
uri = "#{config[:base_uri]}/#{element_id}"
|
11
|
+
uri += "#{querystring_separator(uri)}access_token=#{CGI.escape(@access_token)}" if @access_token
|
12
|
+
uri += "#{querystring_separator(uri) + hash_to_querystring(options)}" unless options.nil? || options.empty?
|
13
|
+
FacebookGraph::GraphNode.new(parse_json(uri))
|
14
|
+
end
|
15
|
+
|
16
|
+
def current_user(options={})
|
17
|
+
get_node('me', options)
|
18
|
+
end
|
19
|
+
|
20
|
+
def search(query, options={})
|
21
|
+
config = FacebookGraph.configuration
|
22
|
+
uri = "#{config[:base_uri] + config[:search_uri]}?q=#{CGI.escape(query)}"
|
23
|
+
uri += "&access_token=#{CGI.escape(@access_token)}" if @access_token
|
24
|
+
uri += "&#{hash_to_querystring(options)}" unless options.nil? || options.empty?
|
25
|
+
FacebookGraph::GraphNode.from_array(parse_json(uri))
|
26
|
+
end
|
27
|
+
|
28
|
+
attr_accessor :access_token
|
29
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class FacebookGraph::GraphNode
|
2
|
+
include FacebookGraph::Helper
|
3
|
+
|
4
|
+
def initialize(properties={})
|
5
|
+
config = FacebookGraph.configuration
|
6
|
+
@properties = symbolize_keys(properties)
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing(mname, *args)
|
10
|
+
prop = @properties[mname]
|
11
|
+
return prop unless prop.nil?
|
12
|
+
prop = connections_defined? && @properties[:metadata]['connections'][mname.to_s]
|
13
|
+
super and return unless prop
|
14
|
+
options = args[0]
|
15
|
+
prop += "#{querystring_separator(prop) + hash_to_querystring(options)}" if options && options.kind_of?(Hash)
|
16
|
+
mname == :picture ? prop : FacebookGraph::GraphNode.from_array(parse_json(prop))
|
17
|
+
end
|
18
|
+
|
19
|
+
def respond_to? mname
|
20
|
+
is_metadata = connections_defined? && @properties[:metadata]['connections'][mname.to_s]
|
21
|
+
!!is_metadata || !!@properties[mname] || super(mname)
|
22
|
+
end
|
23
|
+
|
24
|
+
def node_id
|
25
|
+
@properties[:id]
|
26
|
+
end
|
27
|
+
|
28
|
+
def connections_defined?
|
29
|
+
!!(@properties[:metadata] && @properties[:metadata]['connections'])
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.from_array(array)
|
33
|
+
array['data'].map{|el| self.new(el)} if array['data']
|
34
|
+
end
|
35
|
+
|
36
|
+
attr_reader :properties
|
37
|
+
private :connections_defined?
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module FacebookGraph::Helper
|
5
|
+
include FacebookGraph::HTTPHelper
|
6
|
+
|
7
|
+
protected
|
8
|
+
|
9
|
+
def symbolize_keys h
|
10
|
+
result = {}
|
11
|
+
h.each_pair do |k,v|
|
12
|
+
result[k.to_sym] = v
|
13
|
+
end
|
14
|
+
result
|
15
|
+
end
|
16
|
+
|
17
|
+
def parse_json(uri, options={:http_method=>:get})
|
18
|
+
result = JSON.parse(send(options.delete(:http_method),uri)) rescue nil
|
19
|
+
if result && result['error']
|
20
|
+
begin
|
21
|
+
raise FacebookGraph.const_get(result['error']['type']), result['error']['message']
|
22
|
+
rescue NameError
|
23
|
+
FacebookGraph.const_set(result['error']['type'], Class.new(Exception))
|
24
|
+
retry
|
25
|
+
end
|
26
|
+
end
|
27
|
+
result
|
28
|
+
end
|
29
|
+
|
30
|
+
def querystring_separator(uri)
|
31
|
+
uri =~ /\=/ ? '&' : '?'
|
32
|
+
end
|
33
|
+
|
34
|
+
def hash_to_querystring(hash)
|
35
|
+
hash.map{|k,v| "#{k}=#{v}"}.join('&')
|
36
|
+
end
|
37
|
+
|
38
|
+
def parse_form_data(uri, options={:http_method=>:get})
|
39
|
+
CGI.parse(send(options.delete(:http_method),uri))
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'net/https'
|
2
|
+
|
3
|
+
module FacebookGraph::HTTPHelper
|
4
|
+
def initialize_http host, port
|
5
|
+
http = Net::HTTP.new(host, port)
|
6
|
+
http.use_ssl = true
|
7
|
+
http
|
8
|
+
end
|
9
|
+
|
10
|
+
def get uri
|
11
|
+
uri = URI.parse(uri)
|
12
|
+
http = initialize_http(uri.host, uri.port)
|
13
|
+
request = Net::HTTP::Get.new uri.request_uri
|
14
|
+
#request['user-agent'] = "facebook-graph gem"
|
15
|
+
http.request(request).body
|
16
|
+
end
|
17
|
+
|
18
|
+
private :initialize_http
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: facebook-graph
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 9
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: "0.1"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Pedro A. Vicentini Fracarolli
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-08-18 00:00:00 -03:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: json_pure
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 31
|
29
|
+
segments:
|
30
|
+
- 1
|
31
|
+
- 2
|
32
|
+
- 0
|
33
|
+
version: 1.2.0
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
description: Simple Ruby wrapper for the FB Graph API. Still in its early stages.
|
37
|
+
email: pedro.fracarolli@coffeebeantech.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
files:
|
45
|
+
- README
|
46
|
+
- MIT-LICENSE
|
47
|
+
- lib/facebook-graph.rb
|
48
|
+
- lib/facebook-graph/base/client.rb
|
49
|
+
- lib/facebook-graph/base/authentication.rb
|
50
|
+
- lib/facebook-graph/base/graph_node.rb
|
51
|
+
- lib/facebook-graph/helpers/helper.rb
|
52
|
+
- lib/facebook-graph/helpers/http_helper.rb
|
53
|
+
has_rdoc: true
|
54
|
+
homepage: http://github.com/pfracarolli/facebook-graph
|
55
|
+
licenses: []
|
56
|
+
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: 3
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirements:
|
81
|
+
- json_pure v1.2.0 or greater
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.3.7
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Ruby wrapper for the Facebook Graph API
|
87
|
+
test_files: []
|
88
|
+
|