Module: Koala::TyphoeusService

Includes:
HTTPService, Typhoeus
Defined in:
lib/koala/http_services/typhoeus_service.rb

Class Method Summary collapse

Methods included from HTTPService

included

Class Method Details

.make_request(path, args, verb, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/koala/http_services/typhoeus_service.rb', line 9

def self.make_request(path, args, verb, options = {})
  # if the verb isn't get or post, send it as a post argument
  args.merge!({:method => verb}) && verb = "post" if verb != "get" && verb != "post"

  # switch any UploadableIOs to the files Typhoeus expects
  args.each_pair {|key, value| args[key] = value.to_file if value.is_a?(UploadableIO)}

  # you can pass arguments directly to Typhoeus using the :typhoeus_options key
  typhoeus_options = {:params => args}.merge(options[:typhoeus_options] || {})

  # if proxy/timeout options aren't passed, check if defaults are set
  typhoeus_options[:proxy] ||= proxy
  typhoeus_options[:timeout] ||= timeout

  # by default, we use SSL only for private requests (e.g. with access token)
  # this makes public requests faster
  prefix = (args["access_token"] || @always_use_ssl || options[:use_ssl]) ? "https" : "http"

  response = Typhoeus::Request.send(verb, "#{prefix}://#{server(options)}#{path}", typhoeus_options)
  Koala::Response.new(response.code, response.body, response.headers_hash)
end