hyper-graph 0.1.2 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -14,6 +14,21 @@ Be sure to require it properly, with an underscore not a hyphen:
14
14
 
15
15
  require 'hyper_graph'
16
16
 
17
+
18
+ Authorization
19
+ -----------------------------
20
+
21
+ The Facebook Graph API uses [OAuth 2.0](http://github.com/theRazorBlade/draft-ietf-oauth/raw/master/draft-ietf-oauth.txt) for authorization. You should be familiar with the authorization process as detailed in the [Facebook Authentication Guide](http://developers.facebook.com/docs/authentication/). HyperGraph has a couple of helpers to make the authorization process easy.
22
+
23
+ First, you need to redirect the user to the authorization url. You can generate that url like so:
24
+ irb > HyperGraph.authorize_url('CLIENT_ID', 'REDIRECT_URI', :scope => 'SCOPE1,SCOPE2', :display => 'popup')
25
+ => "https://graph.facebook.com/oauth/authorize?client_id=CLIENT_ID&display=popup&redirect_uri=REDIRECT_URI&scope=SCOPE1,SCOPE2"
26
+
27
+ After the user authorizes your application, they'll be redirected by Facebook to the redirect uri you specified along with one parameter, "code". You can use that code to retrieve an access token.
28
+ irb > HyperGraph.get_access_token('CLIENT_ID', 'CLIENT_SECRET', 'REDIRECT_URI', 'CODE')
29
+ => "your-access-token"
30
+ Your access token is tied to both your Facebook application and the redirect uri specified, so be sure pass the same uri and client information when retrieving your access token that you used when getting user authorization.
31
+
17
32
  Usage
18
33
  -----
19
34
 
data/lib/hyper_graph.rb CHANGED
@@ -15,9 +15,7 @@ class HyperGraph
15
15
  class << self
16
16
  # Request an object from the social graph
17
17
  def get(requested_object_id, options = {})
18
- http = Net::HTTP.new(API_URL, 443)
19
- http.use_ssl = true
20
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
18
+ http = initialize_http_connection
21
19
  request_path = "/#{requested_object_id}"
22
20
 
23
21
  query = build_query(options)
@@ -30,9 +28,7 @@ class HyperGraph
30
28
 
31
29
  # Post an object to the graph
32
30
  def post(requested_object_id, options = {})
33
- http = Net::HTTP.new(API_URL, 443)
34
- http.use_ssl = true
35
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
31
+ http = initialize_http_connection
36
32
  request_path = "/#{requested_object_id}"
37
33
  http_response = http.post(request_path, build_query(options))
38
34
  if http_response.body=='true'
@@ -47,7 +43,20 @@ class HyperGraph
47
43
  def delete(requested_object_id, options = {})
48
44
  post(requested_object_id, options.merge(:method => 'delete'))
49
45
  end
50
-
46
+
47
+ # Redirect users to this url to get authorization
48
+ def authorize_url(client_id, redirect_uri, options={})
49
+ "https://#{API_URL}/oauth/authorize?#{build_query(options.merge(:client_id => client_id, :redirect_uri => redirect_uri))}"
50
+ end
51
+
52
+ def get_access_token(client_id, client_secret, redirect_uri, code)
53
+ http = initialize_http_connection
54
+ request_path = "/oauth/access_token"
55
+ request_path << "?#{build_query(:client_id => client_id, :client_secret => client_secret, :redirect_uri => redirect_uri, :code => code)}"
56
+ http_response = http.get(request_path)
57
+ http_response.body.split('=')[1]
58
+ end
59
+
51
60
  protected
52
61
 
53
62
  def build_query(options)
@@ -67,6 +76,13 @@ class HyperGraph
67
76
 
68
77
  private
69
78
 
79
+ def initialize_http_connection
80
+ http = Net::HTTP.new(API_URL, 443)
81
+ http.use_ssl = true
82
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
83
+ http
84
+ end
85
+
70
86
  # Converts JSON-parsed hash keys and values into a Ruby-friendly format
71
87
  # Convert :id into integer and :updated_time into Time and all keys into symbols
72
88
  def normalize_hash(hash)
@@ -4,6 +4,7 @@ class HyperGraphTest < Test::Unit::TestCase
4
4
 
5
5
  def setup
6
6
  @mock_connection = mock('api-connection')
7
+ @mock_connection.stubs(:verify_mode=)
7
8
  Net::HTTP.stubs(:new).returns(@mock_connection)
8
9
  end
9
10
 
@@ -16,6 +17,7 @@ class HyperGraphTest < Test::Unit::TestCase
16
17
  :link => "http://www.facebook.com/chrisdinn"}
17
18
 
18
19
  mock_response = stub(:body => json_api_response)
20
+ @mock_connection.expects(:use_ssl=).with(true)
19
21
  @mock_connection.stubs(:get).with("/518018845").returns(mock_response)
20
22
 
21
23
  assert_equal expected_parsed_response, HyperGraph.get('518018845')
@@ -36,6 +38,7 @@ class HyperGraphTest < Test::Unit::TestCase
36
38
 
37
39
  access_token = "test-access-token"
38
40
  mock_response = stub(:body => json_api_response)
41
+ @mock_connection.expects(:use_ssl=).with(true)
39
42
  @mock_connection.stubs(:get).with("/518018845?access_token=#{access_token}").returns(mock_response)
40
43
 
41
44
  assert_equal expected_parsed_response, HyperGraph.get('518018845', :access_token => access_token)
@@ -47,6 +50,7 @@ class HyperGraphTest < Test::Unit::TestCase
47
50
 
48
51
  access_token = "test-access-token"
49
52
  mock_response = stub(:body => json_api_response)
53
+ @mock_connection.expects(:use_ssl=).with(true)
50
54
  @mock_connection.stubs(:get).with("/518018845/friends?access_token=#{access_token}").returns(mock_response)
51
55
 
52
56
  assert_equal expected_sorted_array, HyperGraph.get('518018845/friends', :access_token => access_token)
@@ -83,6 +87,7 @@ class HyperGraphTest < Test::Unit::TestCase
83
87
  access_token = "test-access-token"
84
88
  limit = 2
85
89
  mock_response = stub(:body => json_api_response)
90
+ @mock_connection.expects(:use_ssl=).with(true)
86
91
  @mock_connection.stubs(:get).with("/me/photos?access_token=#{access_token}&limit=#{limit}").returns(mock_response)
87
92
 
88
93
  assert_equal expected_sorted_array, HyperGraph.get('me/photos', :access_token => access_token, :limit => limit)
@@ -102,11 +107,10 @@ class HyperGraphTest < Test::Unit::TestCase
102
107
  :updated_time => Time.parse("2010-03-17T20:19:03+0000")}
103
108
  access_token = "test-access-token"
104
109
  mock_response = stub(:body => json_api_response)
110
+ @mock_connection.expects(:use_ssl=).with(true)
105
111
  @mock_connection.stubs(:get).with("/me?access_token=#{access_token}").returns(mock_response)
106
112
 
107
113
  graph = HyperGraph.new(access_token)
108
-
109
- assert_equal expected_parsed_response, HyperGraph.get('me', :access_token => access_token)
110
114
  assert_equal expected_parsed_response, graph.get('me')
111
115
  end
112
116
 
@@ -114,6 +118,7 @@ class HyperGraphTest < Test::Unit::TestCase
114
118
  json_api_response = 'true'
115
119
  access_token = "test-access-token"
116
120
  mock_response = stub(:body => json_api_response)
121
+ @mock_connection.expects(:use_ssl=).with(true)
117
122
  @mock_connection.stubs(:post).with("/115934485101003/maybe", "access_token=#{access_token}").returns(mock_response)
118
123
 
119
124
  graph = HyperGraph.new(access_token)
@@ -124,6 +129,7 @@ class HyperGraphTest < Test::Unit::TestCase
124
129
  json_api_response = '{"error":{"type":"Exception","message":"(#210) User not visible"}}'
125
130
  access_token = "test-access-token"
126
131
  mock_response = stub(:body => json_api_response)
132
+ @mock_connection.expects(:use_ssl=).with(true)
127
133
  @mock_connection.stubs(:post).with('/514569082_115714061789461/comments', "access_token=#{access_token}").returns(mock_response)
128
134
 
129
135
  graph = HyperGraph.new(access_token)
@@ -135,6 +141,7 @@ class HyperGraphTest < Test::Unit::TestCase
135
141
  def test_get_request_raising_error
136
142
  json_api_response = '{"error":{"type":"QueryParseException", "message":"An active access token must be used to query information about the current user."}}'
137
143
  mock_response = stub(:body => json_api_response)
144
+ @mock_connection.expects(:use_ssl=).with(true)
138
145
  @mock_connection.stubs(:get).with('/me/home').returns(mock_response)
139
146
 
140
147
  assert_raise FacebookError do
@@ -146,9 +153,42 @@ class HyperGraphTest < Test::Unit::TestCase
146
153
  json_api_response = 'true'
147
154
  access_token = "test-access-token"
148
155
  mock_response = stub(:body => json_api_response)
156
+ @mock_connection.expects(:use_ssl=).with(true)
149
157
  @mock_connection.stubs(:post).with("/514569082_115714061789461/likes", "access_token=#{access_token}&method=delete").returns(mock_response)
150
158
 
151
159
  graph = HyperGraph.new(access_token)
152
160
  assert_equal true, graph.delete('514569082_115714061789461/likes')
153
161
  end
162
+
163
+ def test_authorize_url
164
+ client_id = "your-client-id"
165
+ callback_url = "http://yoursite.com/callback"
166
+
167
+ expected_authorize_url = "https://graph.facebook.com/oauth/authorize?client_id=#{client_id}&redirect_uri=#{callback_url}"
168
+ assert_equal expected_authorize_url, HyperGraph.authorize_url(client_id, callback_url)
169
+ end
170
+
171
+ def test_authorize_url_with_scope_and_display
172
+ client_id = "your-client-id"
173
+ callback_url = "http://yoursite.com/callback"
174
+ scope = "user_photos,user_videos,stream_publish"
175
+ display = 'popup'
176
+
177
+ expected_authorize_url = "https://graph.facebook.com/oauth/authorize?client_id=#{client_id}&display=#{display}&redirect_uri=#{callback_url}&scope=#{scope}"
178
+ assert_equal expected_authorize_url, HyperGraph.authorize_url(client_id, callback_url, :scope => scope, :display => display)
179
+ end
180
+
181
+ def test_get_access_token
182
+ access_token = "your-token-here"
183
+ client_id = "your-client-id"
184
+ client_secret = "your-client-secret"
185
+ code = "facebook-oauth-code"
186
+ callback_url = "http://yoursite.com/callback"
187
+ api_response = "access_token=#{access_token}"
188
+
189
+ mock_response = stub(:body => api_response)
190
+ @mock_connection.expects(:use_ssl=).with(true)
191
+ @mock_connection.stubs(:get).with("/oauth/access_token?client_id=#{client_id}&client_secret=#{client_secret}&code=#{code}&redirect_uri=#{callback_url}").returns(mock_response)
192
+ assert_equal access_token, HyperGraph.get_access_token(client_id, client_secret, callback_url, code)
193
+ end
154
194
  end
metadata CHANGED
@@ -4,9 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
7
  - 2
9
- version: 0.1.2
8
+ version: "0.2"
10
9
  platform: ruby
11
10
  authors:
12
11
  - Chris Dinn