koala 1.0.0.beta2.1 → 1.0.0.rc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/.gitignore +3 -0
  2. data/CHANGELOG +6 -1
  3. data/Gemfile +3 -0
  4. data/Rakefile +13 -14
  5. data/koala.gemspec +35 -20
  6. data/lib/koala.rb +8 -17
  7. data/lib/koala/graph_api.rb +2 -2
  8. data/lib/koala/http_services.rb +32 -27
  9. data/lib/koala/test_users.rb +4 -4
  10. data/lib/koala/uploadable_io.rb +1 -1
  11. data/spec/cases/api_base_spec.rb +101 -0
  12. data/spec/cases/graph_and_rest_api_spec.rb +31 -0
  13. data/spec/cases/graph_api_spec.rb +25 -0
  14. data/spec/{koala/http_services/http_service_tests.rb → cases/http_services/http_service_spec.rb} +8 -5
  15. data/spec/cases/http_services/net_http_service_spec.rb +350 -0
  16. data/spec/cases/http_services/typhoeus_service_spec.rb +144 -0
  17. data/spec/cases/oauth_spec.rb +374 -0
  18. data/spec/cases/realtime_updates_spec.rb +184 -0
  19. data/spec/cases/rest_api_spec.rb +25 -0
  20. data/spec/{koala/test_users/test_users_tests.rb → cases/test_users_spec.rb} +34 -29
  21. data/spec/cases/uploadable_io_spec.rb +151 -0
  22. data/spec/{koala/assets → fixtures}/beach.jpg +0 -0
  23. data/spec/{facebook_data.yml → fixtures/facebook_data.yml} +5 -5
  24. data/spec/{mock_facebook_responses.yml → fixtures/mock_facebook_responses.yml} +311 -311
  25. data/spec/spec_helper.rb +18 -0
  26. data/spec/support/graph_api_shared_examples.rb +424 -0
  27. data/spec/{koala → support}/live_testing_data_helper.rb +39 -42
  28. data/spec/{mock_http_service.rb → support/mock_http_service.rb} +94 -94
  29. data/spec/{koala/rest_api/rest_api_tests.rb → support/rest_api_shared_examples.rb} +43 -0
  30. data/spec/support/setup_mocks_or_live.rb +52 -0
  31. data/spec/support/uploadable_io_shared_examples.rb +76 -0
  32. metadata +109 -53
  33. data/init.rb +0 -2
  34. data/spec/koala/api_base_tests.rb +0 -102
  35. data/spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb +0 -14
  36. data/spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb +0 -16
  37. data/spec/koala/graph_api/graph_api_no_access_token_tests.rb +0 -65
  38. data/spec/koala/graph_api/graph_api_tests.rb +0 -85
  39. data/spec/koala/graph_api/graph_api_with_access_token_tests.rb +0 -194
  40. data/spec/koala/graph_api/graph_collection_tests.rb +0 -104
  41. data/spec/koala/http_services/net_http_service_tests.rb +0 -339
  42. data/spec/koala/http_services/typhoeus_service_tests.rb +0 -162
  43. data/spec/koala/oauth/oauth_tests.rb +0 -372
  44. data/spec/koala/realtime_updates/realtime_updates_tests.rb +0 -187
  45. data/spec/koala/rest_api/rest_api_no_access_token_tests.rb +0 -25
  46. data/spec/koala/rest_api/rest_api_with_access_token_tests.rb +0 -38
  47. data/spec/koala/uploadable_io/uploadable_io_tests.rb +0 -246
  48. data/spec/koala_spec.rb +0 -18
  49. data/spec/koala_spec_helper.rb +0 -74
  50. data/spec/koala_spec_without_mocks.rb +0 -19
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg
2
+ .project
3
+ Gemfile.lock
data/CHANGELOG CHANGED
@@ -1,11 +1,15 @@
1
1
  v1.0
2
2
  New methods:
3
3
  -- Photo and file upload now supported through #put_picture
4
+ -- RC: now supported natively through Typhoeus
4
5
  -- Beta 2: support for Rails 3, Sinatra, File objects, and paths (see documentation)
5
6
  -- Beta 2: Added UploadableIO class to manage file uploads
6
7
  -- Added a delete_like method (thanks to wassem)
7
8
  -- Beta 2: Added put_connection and delete_connection convenience methods
8
9
  Updated methods:
10
+ -- RC: url_for_oauth_code can now take a :display option (thanks, netbe!)
11
+ -- RC: Net::HTTP can now accept :timeout and :proxy options (thanks, gilles!)
12
+ -- RC: search can now search places, checkins, etc. (thanks, rickyc!)
9
13
  -- Beta 2: TestUser#befriend now requires user info hashes (id and access token) due to Facebook API changes (thanks to pulsd/kbighorse)
10
14
  -- Beta 2: all methods now accept an http_options hash as their optional last parameter
11
15
  -- Beta 2: You can now pass :beta => true in the http options to use Facebook's beta tier.
@@ -16,11 +20,12 @@ Internal improvements:
16
20
  -- Replaced parse_signed_request with a version from Facebook that also supports the new signed params proposal
17
21
  -- Note: invalid requests will now raise exceptions rather than return nil, in keeping with other SDKs
18
22
  -- delete_object will now raise an error if there's no access token (like put_object and delete_like)
23
+ -- Beta 2.1: Updated parse_signed_request to match Facebook's current implementation (thanks, imajes!)
19
24
  -- Beta 2: photo and file uploads now accept symbols as well as strings overall
20
25
  -- Beta 2: APIError is now < StandardError, not Exception
21
26
  -- Beta 2: Added KoalaError for non-API errors
22
- -- Beta 3: Updated parse_signed_request to match Facebook's current implementation (thanks, imajes!)
23
27
  Test improvements:
28
+ -- RC: Incorporated joshk's awesome rewrite of the entire Koala test suite (thanks, joshk!)
24
29
  -- Expanded HTTP service tests (added Typhoeus test suite and additional Net::HTTP test cases)
25
30
  -- Live tests now verify that the access token has the necessary permissions before starting
26
31
  -- Replaced the 50-person network test, which often took 15+ minutes to run live, with a 5-person test
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/Rakefile CHANGED
@@ -1,17 +1,16 @@
1
- # Rakefile
2
- require 'rubygems'
3
1
  require 'rake'
4
- require 'echoe'
5
2
 
6
- # gem management
7
- Echoe.new('koala', '1.0.0.beta2.1') do |p|
8
- p.summary = "A lightweight, flexible library for Facebook with support for the Graph API, the old REST API, realtime updates, and OAuth validation."
9
- p.description = "Koala is a lightweight, flexible Ruby SDK for Facebook. It allows read/write access to the social graph via the Graph API and the older REST API, 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."
10
- p.url = "http://github.com/arsduo/koala"
11
- p.author = ["Alex Koppel", "Chris Baclig", "Rafi Jacoby", "Context Optional"]
12
- p.email = "alex@alexkoppel.com"
13
- p.ignore_pattern = ["tmp/*", "script/*", "pkg/*"]
14
- p.runtime_dependencies = ["json >=1.0", "multipart-post >=1.0"]
15
- p.development_dependencies = []
16
- p.retain_gemspec = true
3
+ begin
4
+ require 'bundler/setup'
5
+ Bundler::GemHelper.install_tasks
6
+ rescue LoadError
7
+ puts 'although not required, bundler is recommened for running the tests'
8
+ end
9
+
10
+
11
+ task :default => :spec
12
+
13
+ require 'rspec/core/rake_task'
14
+ RSpec::Core::RakeTask.new do |t|
15
+ t.rspec_opts = ["--color", '--format doc']
17
16
  end
data/koala.gemspec CHANGED
@@ -1,35 +1,50 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  Gem::Specification.new do |s|
4
- s.name = %q{koala}
5
- s.version = "1.0.0.beta2.1"
4
+ s.name = %q{koala}
5
+ s.version = "1.0.0.rc"
6
+ s.date = %q{2011-04-18}
7
+
8
+ s.summary = %q{A lightweight, flexible library for Facebook with support for the Graph API, the REST API, realtime updates, and OAuth authentication.}
9
+ s.description = %q{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.}
10
+ s.homepage = %q{http://github.com/arsduo/koala}
6
11
 
7
- s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
12
  s.authors = ["Alex Koppel, Chris Baclig, Rafi Jacoby, Context Optional"]
9
- s.date = %q{2011-04-06}
10
- s.description = %q{Koala is a lightweight, flexible Ruby SDK for Facebook. It allows read/write access to the social graph via the Graph API and the older REST API, 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.}
11
- s.email = %q{alex@alexkoppel.com}
12
- s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "lib/koala.rb", "lib/koala/graph_api.rb", "lib/koala/http_services.rb", "lib/koala/realtime_updates.rb", "lib/koala/rest_api.rb", "lib/koala/test_users.rb", "lib/koala/uploadable_io.rb"]
13
- s.files = ["CHANGELOG", "LICENSE", "Manifest", "Rakefile", "init.rb", "koala.gemspec", "lib/koala.rb", "lib/koala/graph_api.rb", "lib/koala/http_services.rb", "lib/koala/realtime_updates.rb", "lib/koala/rest_api.rb", "lib/koala/test_users.rb", "lib/koala/uploadable_io.rb", "readme.md", "spec/facebook_data.yml", "spec/koala/api_base_tests.rb", "spec/koala/assets/beach.jpg", "spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb", "spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb", "spec/koala/graph_api/graph_api_no_access_token_tests.rb", "spec/koala/graph_api/graph_api_tests.rb", "spec/koala/graph_api/graph_api_with_access_token_tests.rb", "spec/koala/graph_api/graph_collection_tests.rb", "spec/koala/http_services/http_service_tests.rb", "spec/koala/http_services/net_http_service_tests.rb", "spec/koala/http_services/typhoeus_service_tests.rb", "spec/koala/live_testing_data_helper.rb", "spec/koala/oauth/oauth_tests.rb", "spec/koala/realtime_updates/realtime_updates_tests.rb", "spec/koala/rest_api/rest_api_no_access_token_tests.rb", "spec/koala/rest_api/rest_api_tests.rb", "spec/koala/rest_api/rest_api_with_access_token_tests.rb", "spec/koala/test_users/test_users_tests.rb", "spec/koala/uploadable_io/uploadable_io_tests.rb", "spec/koala_spec.rb", "spec/koala_spec_helper.rb", "spec/koala_spec_without_mocks.rb", "spec/mock_facebook_responses.yml", "spec/mock_http_service.rb"]
14
- s.homepage = %q{http://github.com/arsduo/koala}
15
- s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Koala"]
16
- s.require_paths = ["lib"]
17
- s.rubyforge_project = %q{koala}
18
- s.rubygems_version = %q{1.4.2}
19
- s.summary = %q{A lightweight, flexible library for Facebook with support for the Graph API, the old REST API, realtime updates, and OAuth validation.}
13
+ s.email = %q{alex@alexkoppel.com}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+
18
+ s.extra_rdoc_files = ["readme.md", "CHANGELOG"]
19
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Koala"]
20
+
21
+ s.require_paths = ["lib"]
22
+
23
+ s.rubygems_version = %q{1.4.2}
24
+
25
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
20
26
 
21
27
  if s.respond_to? :specification_version then
22
28
  s.specification_version = 3
23
29
 
24
30
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
- s.add_runtime_dependency(%q<json>, [">= 1.0"])
26
- s.add_runtime_dependency(%q<multipart-post>, [">= 1.0"])
31
+ s.add_runtime_dependency(%q<json>, ["~> 1.0"])
32
+ s.add_runtime_dependency(%q<multipart-post>, ["~> 1.0"])
33
+ s.add_development_dependency(%q<rspec>, ["~> 2.5.0"])
34
+ s.add_development_dependency(%q<rake>, ["~> 0.8.7"])
35
+ s.add_development_dependency(%q<typhoeus>, ["~> 0.2.4"])
27
36
  else
28
- s.add_dependency(%q<json>, [">= 1.0"])
29
- s.add_dependency(%q<multipart-post>, [">= 1.0"])
37
+ s.add_dependency(%q<json>, ["~> 1.0"])
38
+ s.add_dependency(%q<multipart-post>, ["~> 1.0"])
39
+ s.add_dependency(%q<rspec>, ["~> 2.5.0"])
40
+ s.add_dependency(%q<rake>, ["~> 0.8.7"])
41
+ s.add_dependency(%q<typhoeus>, ["~> 0.2.4"])
30
42
  end
31
43
  else
32
- s.add_dependency(%q<json>, [">= 1.0"])
33
- s.add_dependency(%q<multipart-post>, [">= 1.0"])
44
+ s.add_dependency(%q<json>, ["~> 1.0"])
45
+ s.add_dependency(%q<multipart-post>, ["~> 1.0"])
46
+ s.add_dependency(%q<rspec>, ["~> 2.5.0"])
47
+ s.add_dependency(%q<rake>, ["~> 0.8.7"])
48
+ s.add_dependency(%q<typhoeus>, ["~> 0.2.4"])
34
49
  end
35
50
  end
data/lib/koala.rb CHANGED
@@ -1,27 +1,17 @@
1
1
  require 'cgi'
2
2
  require 'digest/md5'
3
3
 
4
- # rubygems is required to support json, how facebook returns data
5
- require 'rubygems'
6
4
  require 'json'
7
5
 
8
6
  # OpenSSL and Base64 are required to support signed_request
9
7
  require 'openssl'
10
8
  require 'base64'
11
9
 
12
- # include default http services
10
+ # include koala modules
13
11
  require 'koala/http_services'
14
-
15
- # add Graph API methods
16
12
  require 'koala/graph_api'
17
-
18
- # add REST API methods
19
13
  require 'koala/rest_api'
20
-
21
- # add realtime update methods
22
14
  require 'koala/realtime_updates'
23
-
24
- # add test user methods
25
15
  require 'koala/test_users'
26
16
 
27
17
  # add KoalaIO class
@@ -52,7 +42,7 @@ module Koala
52
42
  # JavaScript SDK at http://github.com/facebook/connect-js/.
53
43
 
54
44
  class API
55
- # initialize with an access token
45
+ # initialize with an access token
56
46
  def initialize(access_token = nil)
57
47
  @access_token = access_token
58
48
  end
@@ -61,7 +51,7 @@ module Koala
61
51
  def api(path, args = {}, verb = "get", options = {}, &error_checking_block)
62
52
  # Fetches the given path in the Graph API.
63
53
  args["access_token"] = @access_token || @app_access_token if @access_token || @app_access_token
64
-
54
+
65
55
  # add a leading /
66
56
  path = "/#{path}" unless path =~ /^\//
67
57
 
@@ -173,12 +163,13 @@ module Koala
173
163
  # for permissions, see http://developers.facebook.com/docs/authentication/permissions
174
164
  permissions = options[:permissions]
175
165
  scope = permissions ? "&scope=#{permissions.is_a?(Array) ? permissions.join(",") : permissions}" : ""
176
-
166
+ display = options.has_key?(:display) ? "&display=#{options[:display]}" : ""
167
+
177
168
  callback = options[:callback] || @oauth_callback_url
178
169
  raise ArgumentError, "url_for_oauth_code must get a callback either from the OAuth object or in the options!" unless callback
179
170
 
180
171
  # Creates the URL for oauth authorization for a given callback and optional set of permissions
181
- "https://#{GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{callback}#{scope}"
172
+ "https://#{GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{callback}#{scope}#{display}"
182
173
  end
183
174
 
184
175
  def url_for_access_token(code, options = {})
@@ -225,7 +216,7 @@ module Koala
225
216
  raise "SignedRequest: Unsupported algorithm #{envelope['algorithm']}" if envelope['algorithm'] != 'HMAC-SHA256'
226
217
 
227
218
  # now see if the signature is valid (digest, key, data)
228
- hmac = OpenSSL::HMAC.hexdigest('sha256', @app_secret, encoded_envelope.tr("-_", "+/"))
219
+ hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, @app_secret, encoded_envelope.tr("-_", "+/"))
229
220
  raise 'SignedRequest: Invalid signature' if (signature != hmac)
230
221
 
231
222
  return envelope
@@ -299,7 +290,7 @@ module Koala
299
290
  end
300
291
  end
301
292
  end
302
-
293
+
303
294
  class KoalaError< StandardError; end
304
295
 
305
296
  # finally, set up the http service Koala methods used to make requests
@@ -171,8 +171,8 @@ module Koala
171
171
  # Search
172
172
 
173
173
  def search(search_terms, args = {}, options = {})
174
- # Searches for a given term among posts visible to the current user (or public posts if no token)
175
- result = graph_call("search", args.merge(:q => search_terms), "get", options)
174
+ args.merge!({:q => search_terms}) unless search_terms.nil?
175
+ result = graph_call("search", args, "get", options)
176
176
  result ? GraphCollection.new(result, self) : nil # when facebook is down nil can be returned
177
177
  end
178
178
 
@@ -54,7 +54,7 @@ module Koala
54
54
  # if the verb isn't get or post, send it as a post argument
55
55
  args.merge!({:method => verb}) && verb = "post" if verb != "get" && verb != "post"
56
56
 
57
- http = Net::HTTP.new(server(options), private_request ? 443 : nil)
57
+ http = create_http(server(options), private_request, options)
58
58
  http.use_ssl = true if private_request
59
59
 
60
60
  result = http.start do |http|
@@ -87,6 +87,22 @@ module Koala
87
87
  [key, value.kind_of?(Koala::UploadableIO) ? value.to_upload_io : value]
88
88
  end.flatten]
89
89
  end
90
+
91
+ def self.create_http(server, private_request, options)
92
+ if options[:proxy]
93
+ proxy = URI.parse(options[:proxy])
94
+ http = Net::HTTP.new(server, private_request ? 443 : nil,
95
+ proxy.host, proxy.port, proxy.user, proxy.password)
96
+ else
97
+ http = Net::HTTP.new(server, private_request ? 443 : nil)
98
+ end
99
+ if options[:timeout]
100
+ http.open_timeout = options[:timeout]
101
+ http.read_timeout = options[:timeout]
102
+ end
103
+ http
104
+ end
105
+
90
106
  end
91
107
  end
92
108
  end
@@ -94,11 +110,6 @@ module Koala
94
110
  module TyphoeusService
95
111
  # this service uses Typhoeus to send requests to the graph
96
112
 
97
- # used for multipart file uploads (see below)
98
- class NetHTTPInterface
99
- include NetHTTPService
100
- end
101
-
102
113
  def self.included(base)
103
114
  base.class_eval do
104
115
  require "typhoeus" unless defined?(Typhoeus)
@@ -107,34 +118,28 @@ module Koala
107
118
  include Koala::HTTPService
108
119
 
109
120
  def self.make_request(path, args, verb, options = {})
110
- unless params_require_multipart?(args)
111
- # if the verb isn't get or post, send it as a post argument
112
- args.merge!({:method => verb}) && verb = "post" if verb != "get" && verb != "post"
121
+ # if the verb isn't get or post, send it as a post argument
122
+ args.merge!({:method => verb}) && verb = "post" if verb != "get" && verb != "post"
113
123
 
114
- # switch any UploadableIOs to the files Typhoeus expects
115
- # args.each_pair {|key, value| args[key] = value.to_file if value.is_a?(UploadableIO)}
124
+ # switch any UploadableIOs to the files Typhoeus expects
125
+ args.each_pair {|key, value| args[key] = value.to_file if value.is_a?(UploadableIO)}
116
126
 
117
- # you can pass arguments directly to Typhoeus using the :typhoeus_options key
118
- typhoeus_options = {:params => args}.merge(options[:typhoeus_options] || {})
127
+ # you can pass arguments directly to Typhoeus using the :typhoeus_options key
128
+ typhoeus_options = {:params => args}.merge(options[:typhoeus_options] || {})
119
129
 
120
- # by default, we use SSL only for private requests (e.g. with access token)
121
- # this makes public requests faster
122
- prefix = (args["access_token"] || @always_use_ssl || options[:use_ssl]) ? "https" : "http"
130
+ # by default, we use SSL only for private requests (e.g. with access token)
131
+ # this makes public requests faster
132
+ prefix = (args["access_token"] || @always_use_ssl || options[:use_ssl]) ? "https" : "http"
123
133
 
124
- response = self.send(verb, "#{prefix}://#{server(options)}#{path}", typhoeus_options)
125
- Koala::Response.new(response.code, response.body, response.headers_hash)
126
- else
127
- # we have to use NetHTTPService for multipart for file uploads
128
- # until Typhoeus integrates support
129
- Koala::TyphoeusService::NetHTTPInterface.make_request(path, args, verb, options)
130
- end
134
+ response = self.send(verb, "#{prefix}://#{server(options)}#{path}", typhoeus_options)
135
+ Koala::Response.new(response.code, response.body, response.headers_hash)
131
136
  end
132
137
 
133
138
  private
134
- # Typhoeus file uploads are not currently supported; this will be added when we can get them working
135
- #def self.multipart_requires_content_type?
136
- # false # Typhoeus handles multipart file types, we don't have to require it
137
- #end
139
+
140
+ def self.multipart_requires_content_type?
141
+ false # Typhoeus handles multipart file types, we don't have to require it
142
+ end
138
143
  end # class_eval
139
144
  end
140
145
  end
@@ -20,11 +20,11 @@ module Koala
20
20
  @graph_api = GraphAPI.new(@app_access_token)
21
21
  end
22
22
 
23
- def create(installed, permissions = nil)
23
+ def create(installed, permissions = nil, args = {}, options = {})
24
24
  # Creates and returns a test user
25
- args = {'installed' => installed}
25
+ args['installed'] = installed
26
26
  args['permissions'] = (permissions.is_a?(Array) ? permissions.join(",") : permissions) if installed
27
- result = @graph_api.graph_call(accounts_path, args, "post")
27
+ result = @graph_api.graph_call(accounts_path, args, "post", options)
28
28
  end
29
29
 
30
30
  def list
@@ -49,7 +49,7 @@ module Koala
49
49
  # we explicitly raise an error here to minimize the risk of confusing output
50
50
  # if you pass in a string (as was previously supported) no local exception would be raised
51
51
  # but the Facebook call would fail
52
- raise ArgumentError, "TestUsers#befriend requires hash arguments with id and access_token"
52
+ raise ArgumentError, "TestUsers#befriend requires hash arguments for both users with id and access_token"
53
53
  end
54
54
 
55
55
  u1_graph_api = GraphAPI.new(user1_token)
@@ -42,7 +42,7 @@ module Koala
42
42
 
43
43
  # Expects a parameter of type ActionDispatch::Http::UploadedFile
44
44
  def parse_rails_3_param(uploaded_file)
45
- if uploaded_file.respond_to?(:content_type) and uploaded_file.respond_to?(:tempfile) and uploaded_file.respond_to?(:path)
45
+ if uploaded_file.respond_to?(:content_type) and uploaded_file.respond_to?(:tempfile) and uploaded_file.tempfile.respond_to?(:path)
46
46
  @io_or_path = uploaded_file.tempfile.path
47
47
  @content_type = uploaded_file.content_type
48
48
  end
@@ -0,0 +1,101 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Koala::Facebook::API" do
4
+ before(:each) do
5
+ @service = Koala::Facebook::API.new
6
+ end
7
+
8
+ it "should not include an access token if none was given" do
9
+ Koala.should_receive(:make_request).with(
10
+ anything,
11
+ hash_not_including('access_token' => 1),
12
+ anything,
13
+ anything
14
+ ).and_return(Koala::Response.new(200, "", ""))
15
+
16
+ @service.api('anything')
17
+ end
18
+
19
+ it "should include an access token if given" do
20
+ token = 'adfadf'
21
+ service = Koala::Facebook::API.new token
22
+
23
+ Koala.should_receive(:make_request).with(
24
+ anything,
25
+ hash_including('access_token' => token),
26
+ anything,
27
+ anything
28
+ ).and_return(Koala::Response.new(200, "", ""))
29
+
30
+ service.api('anything')
31
+ end
32
+
33
+ it "should have an attr_reader for access token" do
34
+ token = 'adfadf'
35
+ service = Koala::Facebook::API.new token
36
+ service.access_token.should == token
37
+ end
38
+
39
+ it "should get the attribute of a Koala::Response given by the http_component parameter" do
40
+ http_component = :method_name
41
+
42
+ response = mock('Mock KoalaResponse', :body => '', :status => 200)
43
+ response.should_receive(http_component).and_return('')
44
+
45
+ Koala.stub(:make_request).and_return(response)
46
+
47
+ @service.api('anything', 'get', {}, :http_component => http_component)
48
+ end
49
+
50
+ it "should return the body of the request as JSON if no http_component is given" do
51
+ response = stub('response', :body => 'body', :status => 200)
52
+ Koala.stub(:make_request).and_return(response)
53
+
54
+ json_body = mock('JSON body')
55
+ JSON.stub(:parse).and_return([json_body])
56
+
57
+ @service.api('anything').should == json_body
58
+ end
59
+
60
+ it "should execute a block with the response body if passed one" do
61
+ body = '{}'
62
+ Koala.stub(:make_request).and_return(Koala::Response.new(200, body, {}))
63
+
64
+ yield_test = mock('Yield Tester')
65
+ yield_test.should_receive(:pass)
66
+
67
+ @service.api('anything') do |arg|
68
+ yield_test.pass
69
+ arg.should == JSON.parse(body)
70
+ end
71
+ end
72
+
73
+ it "should raise an API error if the HTTP response code is greater than or equal to 500" do
74
+ Koala.stub(:make_request).and_return(Koala::Response.new(500, 'response body', {}))
75
+
76
+ lambda { @service.api('anything') }.should raise_exception(Koala::Facebook::APIError)
77
+ end
78
+
79
+ it "should handle rogue true/false as responses" do
80
+ Koala.should_receive(:make_request).and_return(Koala::Response.new(200, 'true', {}))
81
+ @service.api('anything').should be_true
82
+
83
+ Koala.should_receive(:make_request).and_return(Koala::Response.new(200, 'false', {}))
84
+ @service.api('anything').should be_false
85
+ end
86
+
87
+ describe "with regard to leading slashes" do
88
+ it "should add a leading / to the path if not present" do
89
+ path = "anything"
90
+ Koala.should_receive(:make_request).with("/#{path}", anything, anything, anything).and_return(Koala::Response.new(200, 'true', {}))
91
+ @service.api(path)
92
+ end
93
+
94
+ it "shouldn't change the path if a leading / is present" do
95
+ path = "/anything"
96
+ Koala.should_receive(:make_request).with(path, anything, anything, anything).and_return(Koala::Response.new(200, 'true', {}))
97
+ @service.api(path)
98
+ end
99
+ end
100
+
101
+ end