tyler_koala 1.2.0beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/.autotest +12 -0
  2. data/.gitignore +5 -0
  3. data/.travis.yml +9 -0
  4. data/CHANGELOG +185 -0
  5. data/Gemfile +11 -0
  6. data/LICENSE +22 -0
  7. data/Manifest +39 -0
  8. data/Rakefile +16 -0
  9. data/autotest/discover.rb +1 -0
  10. data/koala.gemspec +50 -0
  11. data/lib/koala.rb +119 -0
  12. data/lib/koala/batch_operation.rb +74 -0
  13. data/lib/koala/graph_api.rb +281 -0
  14. data/lib/koala/graph_batch_api.rb +87 -0
  15. data/lib/koala/graph_collection.rb +54 -0
  16. data/lib/koala/http_service.rb +161 -0
  17. data/lib/koala/oauth.rb +181 -0
  18. data/lib/koala/realtime_updates.rb +89 -0
  19. data/lib/koala/rest_api.rb +95 -0
  20. data/lib/koala/test_users.rb +102 -0
  21. data/lib/koala/uploadable_io.rb +180 -0
  22. data/lib/koala/utils.rb +7 -0
  23. data/readme.md +160 -0
  24. data/spec/cases/api_base_spec.rb +101 -0
  25. data/spec/cases/error_spec.rb +30 -0
  26. data/spec/cases/graph_and_rest_api_spec.rb +48 -0
  27. data/spec/cases/graph_api_batch_spec.rb +600 -0
  28. data/spec/cases/graph_api_spec.rb +42 -0
  29. data/spec/cases/http_service_spec.rb +420 -0
  30. data/spec/cases/koala_spec.rb +21 -0
  31. data/spec/cases/oauth_spec.rb +428 -0
  32. data/spec/cases/realtime_updates_spec.rb +198 -0
  33. data/spec/cases/rest_api_spec.rb +41 -0
  34. data/spec/cases/test_users_spec.rb +281 -0
  35. data/spec/cases/uploadable_io_spec.rb +206 -0
  36. data/spec/cases/utils_spec.rb +8 -0
  37. data/spec/fixtures/beach.jpg +0 -0
  38. data/spec/fixtures/cat.m4v +0 -0
  39. data/spec/fixtures/facebook_data.yml +61 -0
  40. data/spec/fixtures/mock_facebook_responses.yml +439 -0
  41. data/spec/spec_helper.rb +43 -0
  42. data/spec/support/graph_api_shared_examples.rb +502 -0
  43. data/spec/support/json_testing_fix.rb +42 -0
  44. data/spec/support/koala_test.rb +163 -0
  45. data/spec/support/mock_http_service.rb +98 -0
  46. data/spec/support/ordered_hash.rb +205 -0
  47. data/spec/support/rest_api_shared_examples.rb +285 -0
  48. data/spec/support/uploadable_io_shared_examples.rb +70 -0
  49. metadata +221 -0
@@ -0,0 +1,285 @@
1
+ shared_examples_for "Koala RestAPI" do
2
+ # REST_CALL
3
+ describe "when making a rest request" do
4
+ it "should use the proper path" do
5
+ method = stub('methodName')
6
+ @api.should_receive(:api).with(
7
+ "method/#{method}",
8
+ anything,
9
+ anything,
10
+ anything
11
+ )
12
+
13
+ @api.rest_call(method)
14
+ end
15
+
16
+ it "should always use the rest api" do
17
+ @api.should_receive(:api).with(
18
+ anything,
19
+ anything,
20
+ anything,
21
+ hash_including(:rest_api => true)
22
+ )
23
+
24
+ @api.rest_call('anything')
25
+ end
26
+
27
+ it "should set the read_only option to true if the method is listed in the read-only list" do
28
+ method = Koala::Facebook::RestAPI::READ_ONLY_METHODS.first
29
+
30
+ @api.should_receive(:api).with(
31
+ anything,
32
+ anything,
33
+ anything,
34
+ hash_including(:read_only => true)
35
+ )
36
+
37
+ @api.rest_call(method)
38
+ end
39
+
40
+ it "should set the read_only option to false if the method is not inthe read-only list" do
41
+ method = "I'm not a read-only method"
42
+
43
+ @api.should_receive(:api).with(
44
+ anything,
45
+ anything,
46
+ anything,
47
+ hash_including(:read_only => false)
48
+ )
49
+
50
+ @api.rest_call(method)
51
+ end
52
+
53
+
54
+ it "should take an optional hash of arguments" do
55
+ args = {:arg1 => 'arg1'}
56
+
57
+ @api.should_receive(:api).with(
58
+ anything,
59
+ hash_including(args),
60
+ anything,
61
+ anything
62
+ )
63
+
64
+ @api.rest_call('anything', args)
65
+ end
66
+
67
+ it "should always ask for JSON" do
68
+ @api.should_receive(:api).with(
69
+ anything,
70
+ hash_including('format' => 'json'),
71
+ anything,
72
+ anything
73
+ )
74
+
75
+ @api.rest_call('anything')
76
+ end
77
+
78
+ it "should pass any options provided to the API" do
79
+ options = {:a => 2}
80
+
81
+ @api.should_receive(:api).with(
82
+ anything,
83
+ hash_including('format' => 'json'),
84
+ anything,
85
+ hash_including(options)
86
+ )
87
+
88
+ @api.rest_call('anything', {}, options)
89
+ end
90
+
91
+ it "uses get by default" do
92
+ @api.should_receive(:api).with(
93
+ anything,
94
+ anything,
95
+ "get",
96
+ anything
97
+ )
98
+
99
+ @api.rest_call('anything')
100
+ end
101
+
102
+ it "allows you to specify other http methods as the last argument" do
103
+ method = 'bar'
104
+ @api.should_receive(:api).with(
105
+ anything,
106
+ anything,
107
+ method,
108
+ anything
109
+ )
110
+
111
+ @api.rest_call('anything', {}, {}, method)
112
+ end
113
+
114
+ it "should throw an APIError if the result hash has an error key" do
115
+ Koala.stub(:make_request).and_return(Koala::Response.new(500, {"error_code" => "An error occurred!"}, {}))
116
+ lambda { @api.rest_call("koppel", {}) }.should raise_exception(Koala::Facebook::APIError)
117
+ end
118
+
119
+ describe "when making a FQL request" do
120
+ it "should call fql.query method" do
121
+ @api.should_receive(:rest_call).with(
122
+ "fql.query", anything, anything
123
+ ).and_return(Koala::Response.new(200, "2", {}))
124
+
125
+ @api.fql_query stub('query string')
126
+ end
127
+
128
+ it "should pass a query argument" do
129
+ query = stub('query string')
130
+
131
+ @api.should_receive(:rest_call).with(
132
+ anything, hash_including(:query => query), anything
133
+ )
134
+
135
+ @api.fql_query(query)
136
+ end
137
+
138
+ it "should pass on any other arguments provided" do
139
+ args = {:a => 2}
140
+ @api.should_receive(:rest_call).with(anything, hash_including(args), anything)
141
+ @api.fql_query("a query", args)
142
+ end
143
+
144
+ it "should pass on any http options provided" do
145
+ opts = {:a => 2}
146
+ @api.should_receive(:rest_call).with(anything, anything, hash_including(opts))
147
+ @api.fql_query("a query", {}, opts)
148
+ end
149
+ end
150
+
151
+ describe "when making a FQL-multiquery request" do
152
+ it "should call fql.multiquery method" do
153
+ @api.should_receive(:rest_call).with(
154
+ "fql.multiquery", anything, anything
155
+ ).and_return({})
156
+
157
+ @api.fql_multiquery 'query string'
158
+ end
159
+
160
+ it "should pass a queries argument" do
161
+ queries = stub('query string')
162
+ queries_json = "some JSON"
163
+ MultiJson.stub(:encode).with(queries).and_return(queries_json)
164
+
165
+ @api.should_receive(:rest_call).with(
166
+ anything,
167
+ hash_including(:queries => queries_json),
168
+ anything
169
+ )
170
+
171
+ @api.fql_multiquery(queries)
172
+ end
173
+
174
+ it "simplifies the response format" do
175
+ raw_results = [
176
+ {"name" => "query1", "fql_result_set" => [1, 2, 3]},
177
+ {"name" => "query2", "fql_result_set" => [:a, :b, :c]}
178
+ ]
179
+ expected_results = {
180
+ "query1" => [1, 2, 3],
181
+ "query2" => [:a, :b, :c]
182
+ }
183
+
184
+ @api.stub(:rest_call).and_return(raw_results)
185
+ results = @api.fql_multiquery({:query => true})
186
+ results.should == expected_results
187
+ end
188
+
189
+ it "should pass on any other arguments provided" do
190
+ args = {:a => 2}
191
+ @api.should_receive(:rest_call).with(anything, hash_including(args), anything)
192
+ @api.fql_multiquery("a query", args)
193
+ end
194
+
195
+ it "should pass on any http options provided" do
196
+ opts = {:a => 2}
197
+ @api.should_receive(:rest_call).with(anything, anything, hash_including(opts))
198
+ @api.fql_multiquery("a query", {}, opts)
199
+ end
200
+ end
201
+ end
202
+ end
203
+
204
+ shared_examples_for "Koala RestAPI with an access token" do
205
+ # FQL
206
+ it "should be able to access public information via FQL" do
207
+ result = @api.fql_query("select first_name from user where uid = #{KoalaTest.user2_id}")
208
+ result.size.should == 1
209
+ result.first['first_name'].should == KoalaTest.user2_name
210
+ end
211
+
212
+ it "should be able to access public information via FQL.multiquery" do
213
+ result = @api.fql_multiquery(
214
+ :query1 => "select first_name from user where uid = #{KoalaTest.user2_id}",
215
+ :query2 => "select first_name from user where uid = #{KoalaTest.user1_id}"
216
+ )
217
+ result.size.should == 2
218
+ result["query1"].first['first_name'].should == KoalaTest.user2_name
219
+ result["query2"].first['first_name'].should == KoalaTest.user1_name
220
+ end
221
+
222
+ it "should be able to access protected information via FQL" do
223
+ # Tests agains the permissions fql table
224
+
225
+ # get the current user's ID
226
+ # we're sneakily using the Graph API, which should be okay since it has its own tests
227
+ g = Koala::Facebook::API.new(@token)
228
+ id = g.get_object("me", :fields => "id")["id"]
229
+
230
+ # now send a query about your permissions
231
+ result = @api.fql_query("select read_stream from permissions where uid = #{id}")
232
+
233
+ result.size.should == 1
234
+ # we've verified that you have read_stream permissions, so we can test against that
235
+ result.first["read_stream"].should == 1
236
+ end
237
+
238
+
239
+ it "should be able to access protected information via FQL.multiquery" do
240
+ result = @api.fql_multiquery(
241
+ :query1 => "select post_id from stream where source_id = me()",
242
+ :query2 => "select fromid from comment where post_id in (select post_id from #query1)",
243
+ :query3 => "select uid, name from user where uid in (select fromid from #query2)"
244
+ )
245
+ result.size.should == 3
246
+ result.keys.should include("query1", "query2", "query3")
247
+ end
248
+
249
+ end
250
+
251
+
252
+ shared_examples_for "Koala RestAPI without an access token" do
253
+ # FQL_QUERY
254
+ describe "when making a FQL request" do
255
+ it "should be able to access public information via FQL" do
256
+ result = @api.fql_query("select first_name from user where uid = #{KoalaTest.user2_id}")
257
+ result.size.should == 1
258
+ result.first['first_name'].should == KoalaTest.user2_name
259
+ end
260
+
261
+ it "should be able to access public information via FQL.multiquery" do
262
+ result = @api.fql_multiquery(
263
+ :query1 => "select first_name from user where uid = #{KoalaTest.user2_id}",
264
+ :query2 => "select first_name from user where uid = #{KoalaTest.user1_id}"
265
+ )
266
+ result.size.should == 2
267
+ result["query1"].first['first_name'].should == KoalaTest.user2_name
268
+ result["query2"].first['first_name'].should == KoalaTest.user1_name
269
+ end
270
+
271
+ it "should not be able to access protected information via FQL" do
272
+ lambda { @api.fql_query("select read_stream from permissions where uid = #{KoalaTest.user2_id}") }.should raise_error(Koala::Facebook::APIError)
273
+ end
274
+
275
+ it "should not be able to access protected information via FQL.multiquery" do
276
+ lambda {
277
+ @api.fql_multiquery(
278
+ :query1 => "select post_id from stream where source_id = me()",
279
+ :query2 => "select fromid from comment where post_id in (select post_id from #query1)",
280
+ :query3 => "select uid, name from user where uid in (select fromid from #query2)"
281
+ )
282
+ }.should raise_error(Koala::Facebook::APIError)
283
+ end
284
+ end
285
+ end
@@ -0,0 +1,70 @@
1
+ shared_examples_for "MIME::Types can't return results" do
2
+ {
3
+ "jpg" => "image/jpeg",
4
+ "jpeg" => "image/jpeg",
5
+ "png" => "image/png",
6
+ "gif" => "image/gif"
7
+ }.each_pair do |extension, mime_type|
8
+ it "should properly get content types for #{extension} using basic analysis" do
9
+ path = "filename.#{extension}"
10
+ if @koala_io_params[0].is_a?(File)
11
+ @koala_io_params[0].stub!(:path).and_return(path)
12
+ else
13
+ @koala_io_params[0] = path
14
+ end
15
+ Koala::UploadableIO.new(*@koala_io_params).content_type.should == mime_type
16
+ end
17
+
18
+ it "should get content types for #{extension} using basic analysis with file names with more than one dot" do
19
+ path = "file.name.#{extension}"
20
+ if @koala_io_params[0].is_a?(File)
21
+ @koala_io_params[0].stub!(:path).and_return(path)
22
+ else
23
+ @koala_io_params[0] = path
24
+ end
25
+ Koala::UploadableIO.new(*@koala_io_params).content_type.should == mime_type
26
+ end
27
+ end
28
+
29
+ describe "if the MIME type can't be determined" do
30
+ before :each do
31
+ path = "badfile.badextension"
32
+ if @koala_io_params[0].is_a?(File)
33
+ @koala_io_params[0].stub!(:path).and_return(path)
34
+ else
35
+ @koala_io_params[0] = path
36
+ end
37
+ end
38
+
39
+ it "should throw an exception" do
40
+ lambda { Koala::UploadableIO.new(*@koala_io_params) }.should raise_exception(Koala::KoalaError)
41
+ end
42
+ end
43
+ end
44
+
45
+ shared_examples_for "determining a mime type" do
46
+ describe "if MIME::Types is available" do
47
+ it "should return an UploadIO with MIME::Types-determined type if the type exists" do
48
+ type_result = ["type"]
49
+ Koala::MIME::Types.stub(:type_for).and_return(type_result)
50
+ Koala::UploadableIO.new(*@koala_io_params).content_type.should == type_result.first
51
+ end
52
+ end
53
+
54
+ describe "if MIME::Types is unavailable" do
55
+ before :each do
56
+ # fake that MIME::Types doesn't exist
57
+ Koala::MIME::Types.stub(:type_for).and_raise(NameError)
58
+ end
59
+ it_should_behave_like "MIME::Types can't return results"
60
+ end
61
+
62
+ describe "if MIME::Types can't find the result" do
63
+ before :each do
64
+ # fake that MIME::Types doesn't exist
65
+ Koala::MIME::Types.stub(:type_for).and_return([])
66
+ end
67
+
68
+ it_should_behave_like "MIME::Types can't return results"
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,221 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tyler_koala
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31098177
5
+ prerelease: 5
6
+ segments:
7
+ - 1
8
+ - 2
9
+ - 0
10
+ - beta
11
+ version: 1.2.0beta
12
+ platform: ruby
13
+ authors:
14
+ - Alex Koppel, Chris Baclig, Rafi Jacoby, Context Optional
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-07-16 00:00:00 -07:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: multi_json
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ hash: 15
31
+ segments:
32
+ - 1
33
+ - 0
34
+ version: "1.0"
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: faraday
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 11
46
+ segments:
47
+ - 0
48
+ - 7
49
+ - 4
50
+ version: 0.7.4
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: faraday-stack
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ hash: 29
62
+ segments:
63
+ - 0
64
+ - 1
65
+ - 3
66
+ version: 0.1.3
67
+ type: :runtime
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ hash: 9
78
+ segments:
79
+ - 2
80
+ - 5
81
+ version: "2.5"
82
+ type: :development
83
+ version_requirements: *id004
84
+ - !ruby/object:Gem::Dependency
85
+ name: rake
86
+ prerelease: false
87
+ requirement: &id005 !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ~>
91
+ - !ruby/object:Gem::Version
92
+ hash: 49
93
+ segments:
94
+ - 0
95
+ - 8
96
+ - 7
97
+ version: 0.8.7
98
+ type: :development
99
+ version_requirements: *id005
100
+ description: Koala is a lightweight, flexible Ruby SDK for Facebook. It allows read/write access to the social graph via the Graph and REST APIs, as well as support for realtime updates and OAuth and Facebook Connect authentication. Koala is fully tested and supports Net::HTTP and Typhoeus connections out of the box and can accept custom modules for other services.
101
+ email: alex@alexkoppel.com
102
+ executables: []
103
+
104
+ extensions: []
105
+
106
+ extra_rdoc_files:
107
+ - readme.md
108
+ - CHANGELOG
109
+ files:
110
+ - .autotest
111
+ - .gitignore
112
+ - .travis.yml
113
+ - CHANGELOG
114
+ - Gemfile
115
+ - LICENSE
116
+ - Manifest
117
+ - Rakefile
118
+ - autotest/discover.rb
119
+ - koala.gemspec
120
+ - lib/koala.rb
121
+ - lib/koala/batch_operation.rb
122
+ - lib/koala/graph_api.rb
123
+ - lib/koala/graph_batch_api.rb
124
+ - lib/koala/graph_collection.rb
125
+ - lib/koala/http_service.rb
126
+ - lib/koala/oauth.rb
127
+ - lib/koala/realtime_updates.rb
128
+ - lib/koala/rest_api.rb
129
+ - lib/koala/test_users.rb
130
+ - lib/koala/uploadable_io.rb
131
+ - lib/koala/utils.rb
132
+ - readme.md
133
+ - spec/cases/api_base_spec.rb
134
+ - spec/cases/error_spec.rb
135
+ - spec/cases/graph_and_rest_api_spec.rb
136
+ - spec/cases/graph_api_batch_spec.rb
137
+ - spec/cases/graph_api_spec.rb
138
+ - spec/cases/http_service_spec.rb
139
+ - spec/cases/koala_spec.rb
140
+ - spec/cases/oauth_spec.rb
141
+ - spec/cases/realtime_updates_spec.rb
142
+ - spec/cases/rest_api_spec.rb
143
+ - spec/cases/test_users_spec.rb
144
+ - spec/cases/uploadable_io_spec.rb
145
+ - spec/cases/utils_spec.rb
146
+ - spec/fixtures/beach.jpg
147
+ - spec/fixtures/cat.m4v
148
+ - spec/fixtures/facebook_data.yml
149
+ - spec/fixtures/mock_facebook_responses.yml
150
+ - spec/spec_helper.rb
151
+ - spec/support/graph_api_shared_examples.rb
152
+ - spec/support/json_testing_fix.rb
153
+ - spec/support/koala_test.rb
154
+ - spec/support/mock_http_service.rb
155
+ - spec/support/ordered_hash.rb
156
+ - spec/support/rest_api_shared_examples.rb
157
+ - spec/support/uploadable_io_shared_examples.rb
158
+ has_rdoc: true
159
+ homepage: http://github.com/arsduo/koala
160
+ licenses: []
161
+
162
+ post_install_message:
163
+ rdoc_options:
164
+ - --line-numbers
165
+ - --inline-source
166
+ - --title
167
+ - Koala
168
+ require_paths:
169
+ - lib
170
+ required_ruby_version: !ruby/object:Gem::Requirement
171
+ none: false
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ hash: 3
176
+ segments:
177
+ - 0
178
+ version: "0"
179
+ required_rubygems_version: !ruby/object:Gem::Requirement
180
+ none: false
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ hash: 11
185
+ segments:
186
+ - 1
187
+ - 2
188
+ version: "1.2"
189
+ requirements: []
190
+
191
+ rubyforge_project:
192
+ rubygems_version: 1.6.2
193
+ signing_key:
194
+ specification_version: 3
195
+ summary: A lightweight, flexible library for Facebook with support for the Graph API, the REST API, realtime updates, and OAuth authentication.
196
+ test_files:
197
+ - spec/cases/api_base_spec.rb
198
+ - spec/cases/error_spec.rb
199
+ - spec/cases/graph_and_rest_api_spec.rb
200
+ - spec/cases/graph_api_batch_spec.rb
201
+ - spec/cases/graph_api_spec.rb
202
+ - spec/cases/http_service_spec.rb
203
+ - spec/cases/koala_spec.rb
204
+ - spec/cases/oauth_spec.rb
205
+ - spec/cases/realtime_updates_spec.rb
206
+ - spec/cases/rest_api_spec.rb
207
+ - spec/cases/test_users_spec.rb
208
+ - spec/cases/uploadable_io_spec.rb
209
+ - spec/cases/utils_spec.rb
210
+ - spec/fixtures/beach.jpg
211
+ - spec/fixtures/cat.m4v
212
+ - spec/fixtures/facebook_data.yml
213
+ - spec/fixtures/mock_facebook_responses.yml
214
+ - spec/spec_helper.rb
215
+ - spec/support/graph_api_shared_examples.rb
216
+ - spec/support/json_testing_fix.rb
217
+ - spec/support/koala_test.rb
218
+ - spec/support/mock_http_service.rb
219
+ - spec/support/ordered_hash.rb
220
+ - spec/support/rest_api_shared_examples.rb
221
+ - spec/support/uploadable_io_shared_examples.rb