koala 1.1.0rc2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@ pkg
2
2
  .project
3
3
  Gemfile.lock
4
4
  .rvmrc
5
+ *.rbc
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ rvm:
2
+ - 1.8.7 # (current default)
3
+ - 1.9.2
4
+ - rbx
5
+ - rbx-2.0
6
+ - ree
7
+ - jruby
8
+ - ruby-head
data/CHANGELOG CHANGED
@@ -1,20 +1,25 @@
1
1
  v1.1
2
2
  New methods:
3
- -- Batch API support through Koala::Facebook::GraphAPI.batch (thanks, seejohnrun!)
3
+ -- Added Batch API support (thanks, seejohnrun and spiegela!)
4
4
  -- includes file uploads, error handling, and FQL
5
+ -- Added GraphAPI#put_video
5
6
  -- Added GraphAPI#get_comments_for_urls (thanks, amrnt!)
6
- -- Added RestAPI#fql_multiquery, which simplifies the results (thanks, amrnt!)
7
+ -- Added RestAPI#fql_multiquery, which simplifies the results (thanks, amrnt!)
8
+ -- HTTP services support global proxy and timeout settings (thanks, itchy!)
9
+ -- Net::HTTP supports global ca_path, ca_file, and verify_mode settings (thanks, spiegela!)
7
10
  Updated methods:
8
11
  -- RealtimeUpdates now uses a GraphAPI object instead of its own API
9
12
  -- RestAPI#rest_call now has an optional last argument for method, for calls requiring POST, DELETE, etc. (thanks, sshilo!)
10
13
  -- Filename can now be specified when uploading (e.g. for Ads API) (thanks, sshilo!)
11
14
  -- get_objects([]) returns [] instead of a Facebook error in non-batch mode (thanks, aselder!)
12
15
  Internal improvements:
16
+ -- Koala is now more compatible with other Rubies (JRuby, Rubinius, etc.)
13
17
  -- HTTP services are more modular and can be changed on the fly (thanks, chadk!)
14
18
  -- Includes support for uploading StringIOs and other non-files via Net::HTTP even when using TyphoeusService
15
- -- Support for global proxy and timeout settings (thanks, itchy!)
16
- -- Support for setting certificate path and file to address Net::HTTP errors under Ruby 1.9.2
19
+ -- Koala now uses multi_json to improve compatibility with Rubinius and other Ruby versions
17
20
  -- Koala now uses the modern Typhoeus API (thanks, aselder!)
21
+ -- Koala now uses the current modern Net::HTTP interface (thanks, romanbsd!)
22
+ -- Fixed bugs and typos (thanks, waynn, mokevnin, and tikh!)
18
23
 
19
24
  v1.0
20
25
  New methods:
@@ -24,13 +29,13 @@ New methods:
24
29
  -- Added put_connection and delete_connection convenience methods
25
30
  Updated methods:
26
31
  -- Search can now search places, checkins, etc. (thanks, rickyc!)
27
- -- You can now pass :beta => true in the http options to use Facebook's beta tier.
32
+ -- You can now pass :beta => true in the http options to use Facebook's beta tier
28
33
  -- TestUser#befriend now requires user info hashes (id and access token) due to Facebook API changes (thanks, pulsd and kbighorse!)
29
34
  -- All methods now accept an http_options hash as their optional last parameter (thanks, spiegela!)
30
35
  -- url_for_oauth_code can now take a :display option (thanks, netbe!)
31
36
  -- Net::HTTP can now accept :timeout and :proxy options (thanks, gilles!)
32
37
  -- Test users now supports using test accounts across multiple apps
33
- Internal improvements:
38
+ Internal improvements:
34
39
  -- For public requests, Koala now uses http by default (instead of https) to improve speed
35
40
  -- This can be overridden through Koala.always_use_ssl= or by passing :use_ssl => true in the options hash for an api call
36
41
  -- Read-only REST API requests now go through the faster api-read server
@@ -40,6 +45,7 @@ Internal improvements:
40
45
  -- Updated parse_signed_request to match Facebook's current implementation (thanks, imajes!)
41
46
  -- APIError is now < StandardError, not Exception
42
47
  -- Added KoalaError for non-API errors
48
+ -- Net::HTTP's SSL verification is no longer disabled by default
43
49
  Test improvements:
44
50
  -- Incorporated joshk's awesome rewrite of the entire Koala test suite (thanks, joshk!)
45
51
  -- Expanded HTTP service tests (added Typhoeus test suite and additional Net::HTTP test cases)
data/Gemfile CHANGED
@@ -1,3 +1,7 @@
1
1
  source :rubygems
2
2
 
3
3
  gemspec
4
+
5
+ if defined? JRUBY_VERSION
6
+ gem "jruby-openssl"
7
+ end
data/koala.gemspec CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{koala}
5
- s.version = "1.1.0rc2"
6
- s.date = %q{2011-06-06}
5
+ s.version = "1.1.0"
6
+ s.date = %q{2011-07-18}
7
7
 
8
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
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.}
@@ -28,20 +28,20 @@ Gem::Specification.new do |s|
28
28
  s.specification_version = 3
29
29
 
30
30
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
31
- s.add_runtime_dependency(%q<json>, ["~> 1.0"])
31
+ s.add_runtime_dependency(%q<multi_json>, ["~> 1.0"])
32
32
  s.add_runtime_dependency(%q<multipart-post>, ["~> 1.0"])
33
33
  s.add_development_dependency(%q<rspec>, ["~> 2.5"])
34
34
  s.add_development_dependency(%q<rake>, ["~> 0.8.7"])
35
35
  s.add_development_dependency(%q<typhoeus>, ["~> 0.2.4"])
36
36
  else
37
- s.add_dependency(%q<json>, ["~> 1.0"])
37
+ s.add_dependency(%q<multi_json>, ["~> 1.0"])
38
38
  s.add_dependency(%q<multipart-post>, ["~> 1.0"])
39
39
  s.add_dependency(%q<rspec>, ["~> 2.5"])
40
40
  s.add_dependency(%q<rake>, ["~> 0.8.7"])
41
41
  s.add_dependency(%q<typhoeus>, ["~> 0.2.4"])
42
42
  end
43
43
  else
44
- s.add_dependency(%q<json>, ["~> 1.0"])
44
+ s.add_dependency(%q<multi_json>, ["~> 1.0"])
45
45
  s.add_dependency(%q<multipart-post>, ["~> 1.0"])
46
46
  s.add_dependency(%q<rspec>, ["~> 2.5"])
47
47
  s.add_dependency(%q<rake>, ["~> 0.8.7"])
@@ -0,0 +1,74 @@
1
+ module Koala
2
+ module Facebook
3
+ class BatchOperation
4
+ attr_reader :access_token, :http_options, :post_processing, :files, :batch_api, :identifier
5
+
6
+ @identifier = 0
7
+
8
+ def self.next_identifier
9
+ @identifier += 1
10
+ end
11
+
12
+ def initialize(options = {})
13
+ @identifier = self.class.next_identifier
14
+ @args = (options[:args] || {}).dup # because we modify it below
15
+ @access_token = options[:access_token]
16
+ @http_options = (options[:http_options] || {}).dup # dup because we modify it below
17
+ @batch_args = @http_options.delete(:batch_args) || {}
18
+ @url = options[:url]
19
+ @method = options[:method].to_sym
20
+ @post_processing = options[:post_processing]
21
+
22
+ process_binary_args
23
+
24
+ raise Koala::KoalaError, "Batch operations require an access token, none provided." unless @access_token
25
+ end
26
+
27
+ def to_batch_params(main_access_token)
28
+ # set up the arguments
29
+ args_string = Koala.http_service.encode_params(@access_token == main_access_token ? @args : @args.merge(:access_token => @access_token))
30
+
31
+ response = {
32
+ :method => @method.to_s,
33
+ :relative_url => @url,
34
+ }
35
+
36
+ # handle batch-level arguments, such as name, depends_on, and attached_files
37
+ @batch_args[:attached_files] = @files.keys.join(",") if @files
38
+ response.merge!(@batch_args) if @batch_args
39
+
40
+ # for get and delete, we append args to the URL string
41
+ # otherwise, they go in the body
42
+ if args_string.length > 0
43
+ if args_in_url?
44
+ response[:relative_url] += (@url =~ /\?/ ? "&" : "?") + args_string if args_string.length > 0
45
+ else
46
+ response[:body] = args_string if args_string.length > 0
47
+ end
48
+ end
49
+
50
+ response
51
+ end
52
+
53
+ protected
54
+
55
+ def process_binary_args
56
+ # collect binary files
57
+ @args.each_pair do |key, value|
58
+ if UploadableIO.binary_content?(value)
59
+ @files ||= {}
60
+ # we use a class-level counter to ensure unique file identifiers across multiple batch operations
61
+ # (this is thread safe, since we just care about uniqueness)
62
+ # so remove the file from the original hash and add it to the file store
63
+ id = "op#{identifier}_file#{@files.keys.length}"
64
+ @files[id] = @args.delete(key).is_a?(UploadableIO) ? value : UploadableIO.new(value)
65
+ end
66
+ end
67
+ end
68
+
69
+ def args_in_url?
70
+ @method == :get || @method == :delete
71
+ end
72
+ end
73
+ end
74
+ end
@@ -29,19 +29,6 @@ module Koala
29
29
  # Koala::Facebook::OAuth.get_user_from_cookie() method below to get the OAuth access token
30
30
  # for the active user from the cookie saved by the SDK.
31
31
 
32
- def self.included(base)
33
- base.class_eval do
34
- def self.check_response(response)
35
- # check for Graph API-specific errors
36
- # this returns an error, which is immediately raised (non-batch)
37
- # or added to the list of batch results (batch)
38
- if response.is_a?(Hash) && error_details = response["error"]
39
- APIError.new(error_details)
40
- end
41
- end
42
- end
43
- end
44
-
45
32
  # Objects
46
33
 
47
34
  def get_object(id, args = {}, options = {})
@@ -109,9 +96,9 @@ module Koala
109
96
  graph_call("#{id}/#{connection_name}", args, "delete", options)
110
97
  end
111
98
 
112
- # Pictures
113
- # to delete pictures, use delete_object(photo_id)
114
- # note: you'll need the user_photos permission to actually access photos after uploading them
99
+ # Media (photos and videos)
100
+ # to delete photos or videos, use delete_object(object_id)
101
+ # note: you'll need the user_photos or user_videos permissions to actually access media after upload
115
102
 
116
103
  def get_picture(object, args = {}, options = {})
117
104
  # Gets a picture object, returning the URL (which Facebook sends as a header)
@@ -120,36 +107,30 @@ module Koala
120
107
  end
121
108
  end
122
109
 
110
+ # Can be called in multiple ways:
111
+ #
112
+ # put_picture(file, [content_type], ...)
113
+ # put_picture(path_to_file, [content_type], ...)
114
+ #
115
+ # You can pass in uploaded files directly from Rails or Sinatra.
116
+ # (See lib/koala/uploadable_io.rb for supported frameworks)
117
+ #
118
+ # Optional parameters can be added to the end of the argument list:
119
+ # - args: a hash of request parameters (default: {})
120
+ # - target_id: ID of the target where to post the picture (default: "me")
121
+ # - options: a hash of http options passed to the HTTPService module
122
+ #
123
+ # put_picture(file, content_type, {:message => "Message"}, 01234560)
124
+ # put_picture(params[:file], {:message => "Message"})
125
+
123
126
  def put_picture(*picture_args)
124
- # Can be called in multiple ways:
125
- #
126
- # put_picture(file, [content_type], ...)
127
- # put_picture(path_to_file, [content_type], ...)
128
- #
129
- # You can pass in uploaded files directly from Rails or Sinatra.
130
- # (See lib/koala/uploadable_io.rb for supported frameworks)
131
- #
132
- # Optional parameters can be added to the end of the argument list:
133
- # - args: a hash of request parameters (default: {})
134
- # - target_id: ID of the target where to post the picture (default: "me")
135
- # - options: a hash of http options passed to the HTTPService module
136
- #
137
- # put_picture(file, content_type, {:message => "Message"}, 01234560)
138
- # put_picture(params[:file], {:message => "Message"})
139
-
140
- raise KoalaError.new("Wrong number of arguments for put_picture") unless picture_args.size.between?(1, 5)
141
-
142
- args_offset = picture_args[1].kind_of?(Hash) || picture_args.size == 1 ? 0 : 1
143
-
144
- args = picture_args[1 + args_offset] || {}
145
- target_id = picture_args[2 + args_offset] || "me"
146
- options = picture_args[3 + args_offset] || {}
127
+ put_object(*parse_media_args(picture_args, "photos"))
128
+ end
147
129
 
148
- args["source"] = Koala::UploadableIO.new(*picture_args.slice(0, 1 + args_offset))
149
-
150
- options[:http_service] = Koala.base_http_service if args["source"].requires_base_http_service
151
-
152
- self.put_object(target_id, "photos", args, options)
130
+ def put_video(*video_args)
131
+ args = parse_media_args(video_args, "videos")
132
+ args.last[:video] = true
133
+ put_object(*args)
153
134
  end
154
135
 
155
136
  # Wall posts
@@ -202,34 +183,6 @@ module Koala
202
183
  end
203
184
  end
204
185
 
205
- # API access
206
-
207
- # Make a call which may or may not be batched
208
- def graph_call(path, args = {}, verb = "get", options = {}, &post_processing)
209
- # Direct access to the Facebook API
210
- # see any of the above methods for example invocations
211
- unless GraphAPI.batch_mode?
212
- result = api(path, args, verb, options) do |response|
213
- if error = GraphAPI.check_response(response)
214
- raise error
215
- end
216
- end
217
-
218
- # now process as appropriate (get picture header, make GraphCollection, etc.)
219
- post_processing ? post_processing.call(result) : result
220
- else
221
- # for batch APIs, we queue up the call details (incl. post-processing)
222
- GraphAPI.batch_calls << BatchOperation.new(
223
- :url => path,
224
- :args => args,
225
- :method => verb,
226
- :access_token => @access_token,
227
- :http_options => options,
228
- :post_processing => post_processing
229
- )
230
- nil # batch operations return nothing immediately
231
- end
232
- end
233
186
 
234
187
  # GraphCollection support
235
188
  def get_page(params)
@@ -240,58 +193,66 @@ module Koala
240
193
  end
241
194
  end
242
195
 
243
- end
244
-
245
-
246
- class GraphCollection < Array
247
- # This class is a light wrapper for collections returned
248
- # from the Graph API.
249
- #
250
- # It extends Array to allow direct access to the data colleciton
251
- # which should allow it to drop in seamlessly.
252
- #
253
- # It also allows access to paging information and the
254
- # ability to get the next/previous page in the collection
255
- # by calling next_page or previous_page.
256
- attr_reader :paging
257
- attr_reader :api
258
196
 
259
- def initialize(response, api)
260
- super response["data"]
261
- @paging = response["paging"]
262
- @api = api
263
- end
264
-
265
- # defines methods for NEXT and PREVIOUS pages
266
- %w{next previous}.each do |this|
267
-
268
- # def next_page
269
- # def previous_page
270
- define_method "#{this.to_sym}_page" do
271
- base, args = send("#{this}_page_params")
272
- base ? @api.get_page([base, args]) : nil
197
+ # Batch API
198
+ def batch(http_options = {}, &block)
199
+ batch_client = GraphBatchAPI.new(access_token)
200
+ if block
201
+ yield batch_client
202
+ batch_client.execute(http_options)
203
+ else
204
+ batch_client
273
205
  end
274
-
275
- # def next_page_params
276
- # def previous_page_params
277
- define_method "#{this.to_sym}_page_params" do
278
- return nil unless @paging and @paging[this]
279
- parse_page_url(@paging[this])
206
+ end
207
+
208
+ def self.included(base)
209
+ base.class_eval do
210
+ def self.batch
211
+ raise NoMethodError, "The BatchAPI signature has changed (the original implementation was not thread-safe). Please see https://github.com/arsduo/koala/wiki/Batch-requests. (This message will be removed in the final 1.1 release.)"
212
+ end
280
213
  end
281
214
  end
282
215
 
283
- def parse_page_url(url)
284
- match = url.match(/.com\/(.*)\?(.*)/)
285
- base = match[1]
286
- args = match[2]
287
- params = CGI.parse(args)
288
- new_params = {}
289
- params.each_pair do |key,value|
290
- new_params[key] = value.join ","
216
+ # Direct access to the Facebook API
217
+ # see any of the above methods for example invocations
218
+ def graph_call(path, args = {}, verb = "get", options = {}, &post_processing)
219
+ result = api(path, args, verb, options) do |response|
220
+ error = check_response(response)
221
+ raise error if error
291
222
  end
292
- [base,new_params]
223
+
224
+ # now process as appropriate (get picture header, make GraphCollection, etc.)
225
+ post_processing ? post_processing.call(result) : result
293
226
  end
294
227
 
228
+ def check_response(response)
229
+ # check for Graph API-specific errors
230
+ # this returns an error, which is immediately raised (non-batch)
231
+ # or added to the list of batch results (batch)
232
+ if response.is_a?(Hash) && error_details = response["error"]
233
+ APIError.new(error_details)
234
+ end
235
+ end
236
+
237
+ private
238
+
239
+ def parse_media_args(media_args, method)
240
+ # photo and video uploads can accept different types of arguments (see above)
241
+ # so here, we parse the arguments into a form directly usable in put_object
242
+ raise KoalaError.new("Wrong number of arguments for put_#{method == "photos" ? "picture" : "video"}") unless media_args.size.between?(1, 5)
243
+
244
+ args_offset = media_args[1].kind_of?(Hash) || media_args.size == 1 ? 0 : 1
245
+
246
+ args = media_args[1 + args_offset] || {}
247
+ target_id = media_args[2 + args_offset] || "me"
248
+ options = media_args[3 + args_offset] || {}
249
+
250
+ args["source"] = Koala::UploadableIO.new(*media_args.slice(0, 1 + args_offset))
251
+
252
+ options[:http_service] = Koala.base_http_service if args["source"].requires_base_http_service
253
+
254
+ [target_id, method, args, options]
255
+ end
295
256
  end
296
257
  end
297
- end
258
+ end
@@ -0,0 +1,87 @@
1
+ module Koala
2
+ module Facebook
3
+ module GraphBatchAPIMethods
4
+
5
+ def self.included(base)
6
+ base.class_eval do
7
+ alias_method :graph_call_outside_batch, :graph_call
8
+ alias_method :graph_call, :graph_call_in_batch
9
+
10
+ alias_method :check_graph_api_response, :check_response
11
+ alias_method :check_response, :check_graph_batch_api_response
12
+ end
13
+ end
14
+
15
+ def batch_calls
16
+ @batch_calls ||= []
17
+ end
18
+
19
+ def graph_call_in_batch(path, args = {}, verb = "get", options = {}, &post_processing)
20
+ # for batch APIs, we queue up the call details (incl. post-processing)
21
+ batch_calls << BatchOperation.new(
22
+ :url => path,
23
+ :args => args,
24
+ :method => verb,
25
+ :access_token => options['access_token'] || access_token,
26
+ :http_options => options,
27
+ :post_processing => post_processing
28
+ )
29
+ nil # batch operations return nothing immediately
30
+ end
31
+
32
+ def check_graph_batch_api_response(response)
33
+ if response.is_a?(Hash) && response["error"] && !response["error"].is_a?(Hash)
34
+ APIError.new("type" => "Error #{response["error"]}", "message" => response["error_description"])
35
+ else
36
+ check_graph_api_response(response)
37
+ end
38
+ end
39
+
40
+ def execute(http_options = {})
41
+ return [] unless batch_calls.length > 0
42
+ # Turn the call args collected into what facebook expects
43
+ args = {}
44
+ args["batch"] = MultiJson.encode(batch_calls.map { |batch_op|
45
+ args.merge!(batch_op.files) if batch_op.files
46
+ batch_op.to_batch_params(access_token)
47
+ })
48
+
49
+ graph_call_outside_batch('/', args, 'post', http_options) do |response|
50
+ # map the results with post-processing included
51
+ index = 0 # keep compat with ruby 1.8 - no with_index for map
52
+ response.map do |call_result|
53
+ # Get the options hash
54
+ batch_op = batch_calls[index]
55
+ index += 1
56
+
57
+ if call_result
58
+ # (see note in regular api method about JSON parsing)
59
+ body = MultiJson.decode("[#{call_result['body'].to_s}]")[0]
60
+
61
+ unless call_result["code"].to_i >= 500 || error = check_response(body)
62
+ # Get the HTTP component they want
63
+ data = case batch_op.http_options[:http_component]
64
+ when :status
65
+ call_result["code"].to_i
66
+ when :headers
67
+ # facebook returns the headers as an array of k/v pairs, but we want a regular hash
68
+ call_result['headers'].inject({}) { |headers, h| headers[h['name']] = h['value']; headers}
69
+ else
70
+ body
71
+ end
72
+
73
+ # process it if we are given a block to process with
74
+ batch_op.post_processing ? batch_op.post_processing.call(data) : data
75
+ else
76
+ error || APIError.new({"type" => "HTTP #{call_result["code"].to_s}", "message" => "Response body: #{body}"})
77
+ end
78
+ else
79
+ nil
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,54 @@
1
+ module Koala
2
+ module Facebook
3
+ class GraphCollection < Array
4
+ # This class is a light wrapper for collections returned
5
+ # from the Graph API.
6
+ #
7
+ # It extends Array to allow direct access to the data colleciton
8
+ # which should allow it to drop in seamlessly.
9
+ #
10
+ # It also allows access to paging information and the
11
+ # ability to get the next/previous page in the collection
12
+ # by calling next_page or previous_page.
13
+ attr_reader :paging
14
+ attr_reader :api
15
+
16
+ def initialize(response, api)
17
+ super response["data"]
18
+ @paging = response["paging"]
19
+ @api = api
20
+ end
21
+
22
+ # defines methods for NEXT and PREVIOUS pages
23
+ %w{next previous}.each do |this|
24
+
25
+ # def next_page
26
+ # def previous_page
27
+ define_method "#{this.to_sym}_page" do
28
+ base, args = send("#{this}_page_params")
29
+ base ? @api.get_page([base, args]) : nil
30
+ end
31
+
32
+ # def next_page_params
33
+ # def previous_page_params
34
+ define_method "#{this.to_sym}_page_params" do
35
+ return nil unless @paging and @paging[this]
36
+ parse_page_url(@paging[this])
37
+ end
38
+ end
39
+
40
+ def parse_page_url(url)
41
+ match = url.match(/.com\/(.*)\?(.*)/)
42
+ base = match[1]
43
+ args = match[2]
44
+ params = CGI.parse(args)
45
+ new_params = {}
46
+ params.each_pair do |key,value|
47
+ new_params[key] = value.join ","
48
+ end
49
+ [base,new_params]
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -7,6 +7,11 @@ module Koala
7
7
  # this service uses Net::HTTP to send requests to the graph
8
8
  include Koala::HTTPService
9
9
 
10
+ # Net::HTTP-specific values
11
+ class << self
12
+ attr_accessor :ca_file, :ca_path, :verify_mode
13
+ end
14
+
10
15
  def self.make_request(path, args, verb, options = {})
11
16
  # We translate args to a valid query string. If post is specified,
12
17
  # we send a POST request to the given path with the given arguments.
@@ -15,17 +20,13 @@ module Koala
15
20
  # this makes public requests faster
16
21
  private_request = args["access_token"] || @always_use_ssl || options[:use_ssl]
17
22
 
18
- # if proxy/timeout options aren't passed, check if defaults are set
19
- options[:proxy] ||= proxy
20
- options[:timeout] ||= timeout
21
-
22
23
  # if the verb isn't get or post, send it as a post argument
23
24
  args.merge!({:method => verb}) && verb = "post" if verb != "get" && verb != "post"
24
25
 
25
26
  http = create_http(server(options), private_request, options)
26
27
 
27
- result = http.start do |http|
28
- response, body = if verb == "post"
28
+ response = http.start do |http|
29
+ if verb == "post"
29
30
  if params_require_multipart? args
30
31
  http.request Net::HTTP::Post::Multipart.new path, encode_multipart_params(args)
31
32
  else
@@ -34,9 +35,9 @@ module Koala
34
35
  else
35
36
  http.get("#{path}?#{encode_params(args)}")
36
37
  end
37
-
38
- Koala::Response.new(response.code.to_i, body, response)
39
38
  end
39
+
40
+ Koala::Response.new(response.code.to_i, response.body, response)
40
41
  end
41
42
 
42
43
  protected
@@ -44,44 +45,48 @@ module Koala
44
45
  # unfortunately, we can't use to_query because that's Rails, not Ruby
45
46
  # if no hash (e.g. no auth token) return empty string
46
47
  ((param_hash || {}).collect do |key_and_value|
47
- key_and_value[1] = key_and_value[1].to_json if key_and_value[1].class != String
48
+ key_and_value[1] = MultiJson.encode(key_and_value[1]) if key_and_value[1].class != String
48
49
  "#{key_and_value[0].to_s}=#{CGI.escape key_and_value[1]}"
49
50
  end).join("&")
50
51
  end
51
-
52
+
52
53
  def self.encode_multipart_params(param_hash)
53
- Hash[*param_hash.collect do |key, value|
54
+ Hash[*param_hash.collect do |key, value|
54
55
  [key, value.kind_of?(Koala::UploadableIO) ? value.to_upload_io : value]
55
56
  end.flatten]
56
57
  end
57
58
 
58
59
  def self.create_http(server, private_request, options)
59
- if options[:proxy]
60
- proxy = URI.parse(options[:proxy])
60
+ if proxy_server = options[:proxy] || proxy
61
+ proxy = URI.parse(proxy_server)
61
62
  http = Net::HTTP.new(server, private_request ? 443 : nil,
62
63
  proxy.host, proxy.port, proxy.user, proxy.password)
63
64
  else
64
- http = Net::HTTP.new(server, private_request ? 443 : nil)
65
+ http = Net::HTTP.new(server, private_request ? 443 : nil)
65
66
  end
66
-
67
- if options[:timeout]
68
- http.open_timeout = options[:timeout]
69
- http.read_timeout = options[:timeout]
67
+
68
+ if timeout_value = options[:timeout] || timeout
69
+ http.open_timeout = timeout_value
70
+ http.read_timeout = timeout_value
70
71
  end
71
-
72
+
72
73
  # For HTTPS requests, set the proper CA certificates
73
74
  if private_request
74
75
  http.use_ssl = true
75
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
76
+ http.verify_mode = options[:verify_mode] || verify_mode || OpenSSL::SSL::VERIFY_PEER
76
77
 
77
- options[:ca_file] ||= ca_file
78
- http.ca_file = options[:ca_file] if options[:ca_file] && File.exists?(options[:ca_file])
78
+ if cert_file = options[:ca_file] || ca_file
79
+ raise Errno::ENOENT, "Certificate file #{cert_file.inspect} does not exist!" unless File.exists?(cert_file)
80
+ http.ca_file = cert_file
81
+ end
79
82
 
80
- options[:ca_path] ||= ca_path
81
- http.ca_path = options[:ca_path] if options[:ca_path] && Dir.exists?(options[:ca_path])
83
+ if cert_path = options[:ca_path] || ca_path
84
+ raise Errno::ENOENT, "Certificate path #{cert_path.inspect} does not exist!" unless File.directory?(cert_path)
85
+ http.ca_path = cert_path
86
+ end
82
87
  end
83
-
88
+
84
89
  http
85
90
  end
86
91
  end
87
- end
92
+ end