namxam-rfgraph 0.4

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2010 Conor Hunt <conor.hunt@gmail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ Ruby Facebook Graph API Wrapper
2
+ ===============================
3
+
4
+ A simple Ruby wrapper to help use the Facebook Graph API.
5
+
6
+ Documentation on the Graph API is available here:
7
+ [http://developers.facebook.com/docs/](http://developers.facebook.com/docs/)
8
+
9
+ This code is based off of the Python SDK - [http://github.com/facebook/python-sdk/](http://github.com/facebook/python-sdk/)
10
+
11
+ Install
12
+ -------
13
+
14
+ gem install rfgraph
15
+
16
+ Example
17
+ -------
18
+
19
+ Without auth token
20
+
21
+ require 'rfgraph'
22
+ req = RFGraph::Request.new
23
+ req.get_object("19292868552")
24
+ req.get_object("99394368305/photos")
25
+ req.get_object("331218348435", :metadata => 1)
26
+
27
+ With auth token
28
+
29
+ require 'rfgraph'
30
+ fauth = RFGraph::Auth.new(APP_ID, APP_SECRET)
31
+
32
+ # Get the URL to redirect your user to in their web browser.
33
+ # For options see: http://developers.facebook.com/docs/authentication/
34
+ # For scope permissions see: http://developers.facebook.com/docs/authentication/permissions
35
+ auth_url = fauth.authorize_url("http://yourwebsite.com/callback", :display => :popup, :scope => [])
36
+
37
+ # Get the code that facebook returns after the user auths your app and turn that into a auth token
38
+ # Make sure that the callback url is the same as in the previous request, otherwise the request will fail
39
+ auth_token = fauth.authorize("http://yourwebsite.com/callback", FACEBOOK_CODE)
40
+
41
+ # Make some requests using the auth token
42
+ request = RFGraph::Request.new(auth_token)
43
+ request.get_object("me")
44
+ request.get_object("me/friends")
45
+
46
+ request.put_wall_post("Awesome message!")
47
+
48
+ COPYRIGHT
49
+ ---------
50
+
51
+ Copyright (c) 2010 Conor Hunt <conor.hunt@gmail.com>
52
+ Released under the MIT license
data/lib/rfgraph.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'rfgraph/request'
2
+ require 'rfgraph/auth'
3
+
4
+ module RFGraph
5
+ BASE_URL = "https://graph.facebook.com"
6
+
7
+ def self.get_user_from_cookie(cookies, app_id, app_secret)
8
+ # TODO
9
+ end
10
+
11
+ class RFGraphError < RuntimeError
12
+ attr_accessor :code
13
+
14
+ def initialize(code, message)
15
+ @code = code
16
+ super(message)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,45 @@
1
+ require 'open-uri'
2
+ require 'cgi'
3
+
4
+ module RFGraph
5
+ class Auth
6
+ attr_accessor :app_id
7
+ attr_accessor :app_secret
8
+ attr_accessor :access_token
9
+ attr_accessor :expires
10
+
11
+ def initialize(app_id, app_secret)
12
+ @app_id = app_id
13
+ @app_secret = app_secret
14
+ end
15
+
16
+ # Options:
17
+ # * :display - page (default), popup, wap, touch. See: http://developers.facebook.com/docs/authentication/.
18
+ # * :scope - either a String "user_photos,user_videos,stream_publish" or an
19
+ # Array like [:user_photos, :user_videos, :stream_publish]
20
+ # For more permission scopes, see http://developers.facebook.com/docs/authentication/permissions
21
+ #
22
+ # All other options in the options Hash will be put in the authorize_url.
23
+ def authorize_url(callback_url, options = {})
24
+ url = "#{BASE_URL}/oauth/authorize?client_id=#{app_id}&redirect_uri=#{callback_url}"
25
+ scope = options.delete(:scope)
26
+ url += "&scope=#{scope.join(',')}" unless scope.blank?
27
+ url += "&#{options.to_query}" unless options.blank? # Add other options. FIXME: to_query method requires Rails?!
28
+ return url
29
+ end
30
+
31
+ # Take the oauth2 request token and turn it into an access token
32
+ # which can be used to access private data
33
+ def authorize(callback_url, code)
34
+ data = open("#{BASE_URL}/oauth/access_token?client_id=#{app_id}&redirect_uri=#{CGI.escape callback_url}&client_secret=#{app_secret}&code=#{CGI.escape code}").read
35
+
36
+ # The expiration date is not necessarily set, as the app might have
37
+ # requested offline_access to the data
38
+ match_data = data.match(/expires=([^&]+)/)
39
+ @expires = match_data && match_data[1] || nil
40
+
41
+ # Extract the access token
42
+ @access_token = data.match(/access_token=([^&]+)/)[1]
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,82 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'net/https'
4
+
5
+ module RFGraph
6
+ class Request
7
+ attr_accessor :access_token
8
+
9
+ def initialize(access_token = nil)
10
+ @access_token = access_token
11
+ end
12
+
13
+ def get_object(id, args = {})
14
+ request(id, args)
15
+ end
16
+
17
+ def get_objects(ids, args = {})
18
+ args["ids"] = ids.join(",")
19
+ return request("", args)
20
+ end
21
+
22
+ def get_connections(id, connection_name, args = {})
23
+ return request(id + "/" + connection_name, args)
24
+ end
25
+
26
+ def put_object(parent_object, connection_name, data = {})
27
+ raise RFGraphError.new(-1, "Access token required for put requests") if access_token.nil?
28
+ post_request(parent_object + "/" + connection_name, data)
29
+ end
30
+
31
+ def put_wall_post(message, attachment = {}, profile_id = "me")
32
+ put_object(profile_id, "feed", attachment.merge({'message' => message}))
33
+ end
34
+
35
+ def put_comment(object_id, message)
36
+ put_object(object_id, "comments", {"message" => message})
37
+ end
38
+
39
+ def put_like(object_id)
40
+ put_object(object_id, "likes")
41
+ end
42
+
43
+ def delete_object(id)
44
+ post_request(id, {"method"=> "delete"})
45
+ end
46
+
47
+ def post_request(path, post_args = {})
48
+ request(path, {}, post_args)
49
+ end
50
+
51
+ def request(path, url_args = {}, post_args = nil)
52
+ if access_token
53
+ if post_args
54
+ post_args["access_token"] = access_token
55
+ else
56
+ url_args["access_token"] = access_token
57
+ end
58
+ end
59
+
60
+ encoded_url_args = url_args.collect {|k,v| "#{CGI.escape k.to_s}=#{CGI.escape v.to_s}" }.join("&")
61
+ url = URI.parse("#{BASE_URL}/#{path}?#{encoded_url_args}")
62
+ http = Net::HTTP.new(url.host, url.port)
63
+ http.use_ssl = true
64
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
65
+
66
+ response = if post_args
67
+ encoded_post_args = post_args.collect {|k,v| "#{CGI.escape k.to_s}=#{CGI.escape v.to_s}" }.join("&")
68
+ http.post("#{url.path}?#{url.query}", encoded_post_args)
69
+ else
70
+ http.get("#{url.path}?#{url.query}")
71
+ end
72
+
73
+ response = JSON.parse(response.body)
74
+
75
+ if response["error"]
76
+ raise RFGraphError.new(response["error"]["code"], response["error"]["message"])
77
+ end
78
+
79
+ response
80
+ end
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: namxam-rfgraph
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 4
8
+ version: "0.4"
9
+ platform: ruby
10
+ authors:
11
+ - Conor Hunt
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-04-30 00:00:00 +02:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: json
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ segments:
27
+ - 1
28
+ - 2
29
+ - 4
30
+ version: 1.2.4
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: Ruby wrapper for Facebook Graph API
34
+ email: conor.hunt@gmail.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ files:
42
+ - README.md
43
+ - LICENSE
44
+ - lib/rfgraph/auth.rb
45
+ - lib/rfgraph/request.rb
46
+ - lib/rfgraph.rb
47
+ has_rdoc: true
48
+ homepage: http://github.com/conorh/rfgraph
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.3.6
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Ruby wrapper for Facebook Graph API
77
+ test_files: []
78
+