Module: Koala::HTTPService

Defined in:
lib/koala/http_service.rb

Constant Summary collapse

DEFAULT_MIDDLEWARE =
Proc.new do |builder|
  builder.use Koala::MultipartRequest
  builder.request :url_encoded
  builder.adapter Faraday.default_adapter
end

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.faraday_middlewareObject

Returns the value of attribute faraday_middleware.



17
18
19
# File 'lib/koala/http_service.rb', line 17

def faraday_middleware
  @faraday_middleware
end

.http_optionsObject

Returns the value of attribute http_options.



17
18
19
# File 'lib/koala/http_service.rb', line 17

def http_options
  @http_options
end

Class Method Details

.always_use_sslObject

deprecations not elegant or compact code, but temporary



67
68
69
70
# File 'lib/koala/http_service.rb', line 67

def self.always_use_ssl
  Koala::Utils.deprecate("HTTPService.always_use_ssl is now HTTPService.http_options[:use_ssl]; always_use_ssl will be removed in a future version.")
  http_options[:use_ssl]
end

.always_use_ssl=(value) ⇒ Object



72
73
74
75
# File 'lib/koala/http_service.rb', line 72

def self.always_use_ssl=(value)
  Koala::Utils.deprecate("HTTPService.always_use_ssl is now HTTPService.http_options[:use_ssl]; always_use_ssl will be removed in a future version.")
  http_options[:use_ssl] = value
end

.ca_fileObject



117
118
119
120
# File 'lib/koala/http_service.rb', line 117

def self.ca_file
  Koala::Utils.deprecate("HTTPService.ca_file is now (HTTPService.http_options[:ssl] ||= {})[:ca_file]; .ca_file will be removed in a future version.")
  (http_options[:ssl] || {})[:ca_file]
end

.ca_file=(value) ⇒ Object



122
123
124
125
# File 'lib/koala/http_service.rb', line 122

def self.ca_file=(value)
  Koala::Utils.deprecate("HTTPService.ca_file is now (HTTPService.http_options[:ssl] ||= {})[:ca_file]; .ca_file will be removed in a future version.")
  (http_options[:ssl] ||= {})[:ca_file] = value
end

.ca_pathObject



107
108
109
110
# File 'lib/koala/http_service.rb', line 107

def self.ca_path
  Koala::Utils.deprecate("HTTPService.ca_path is now (HTTPService.http_options[:ssl] ||= {})[:ca_path]; .ca_path will be removed in a future version.")
  (http_options[:ssl] || {})[:ca_path]
end

.ca_path=(value) ⇒ Object



112
113
114
115
# File 'lib/koala/http_service.rb', line 112

def self.ca_path=(value)
  Koala::Utils.deprecate("HTTPService.ca_path is now (HTTPService.http_options[:ssl] ||= {})[:ca_path]; .ca_path will be removed in a future version.")
  (http_options[:ssl] ||= {})[:ca_path] = value
end

.encode_params(param_hash) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/koala/http_service.rb', line 54

def self.encode_params(param_hash)
  # unfortunately, we can't use to_query because that's Rails, not Ruby
  # if no hash (e.g. no auth token) return empty string
  # this is used mainly by the Batch API nowadays
  ((param_hash || {}).collect do |key_and_value|
    key_and_value[1] = MultiJson.encode(key_and_value[1]) unless key_and_value[1].is_a? String
    "#{key_and_value[0].to_s}=#{CGI.escape key_and_value[1]}"
  end).join("&")
end

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/koala/http_service.rb', line 35

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"

  # turn all the keys to strings (Faraday has issues with symbols under 1.8.7) and resolve UploadableIOs
  params = args.inject({}) {|hash, kv| hash[kv.first.to_s] = kv.last.is_a?(UploadableIO) ? kv.last.to_upload_io : kv.last; hash}

  # figure out our options for this request   
  request_options = {:params => (verb == "get" ? params : {})}.merge(http_options || {}).merge(process_options(options))
  request_options[:use_ssl] = true if args["access_token"] # require http if there's a token

  # set up our Faraday connection
  # we have to manually assign params to the URL or the
  conn = Faraday.new(server(request_options), request_options, &(faraday_middleware || DEFAULT_MIDDLEWARE))

  response = conn.send(verb, path, (verb == "post" ? params : {}))
  Koala::Response.new(response.status.to_i, response.body, response.headers)
end

.process_options(options) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/koala/http_service.rb', line 137

def self.process_options(options)
  if typhoeus_options = options.delete(:typhoeus_options)
    Koala::Utils.deprecate("typhoeus_options should now be included directly in the http_options hash.  Support for this key will be removed in a future version.")
    options = options.merge(typhoeus_options)
  end
  
  if ca_file = options.delete(:ca_file)
    Koala::Utils.deprecate("http_options[:ca_file] should now be passed inside (http_options[:ssl] = {}) -- that is, http_options[:ssl][:ca_file].  Support for this key will be removed in a future version.")
    (options[:ssl] ||= {})[:ca_file] = ca_file
  end

  if ca_path = options.delete(:ca_path)
    Koala::Utils.deprecate("http_options[:ca_path] should now be passed inside (http_options[:ssl] = {}) -- that is, http_options[:ssl][:ca_path].  Support for this key will be removed in a future version.")
    (options[:ssl] ||= {})[:ca_path] = ca_path
  end

  if verify_mode = options.delete(:verify_mode)
    Koala::Utils.deprecate("http_options[:verify_mode] should now be passed inside (http_options[:ssl] = {}) -- that is, http_options[:ssl][:verify_mode].  Support for this key will be removed in a future version.")
    (options[:ssl] ||= {})[:verify_mode] = verify_mode
  end
  
  options
end

.proxyObject



97
98
99
100
# File 'lib/koala/http_service.rb', line 97

def self.proxy
  Koala::Utils.deprecate("HTTPService.proxy is now HTTPService.http_options[:proxy]; .proxy will be removed in a future version.")
  http_options[:proxy]
end

.proxy=(value) ⇒ Object



102
103
104
105
# File 'lib/koala/http_service.rb', line 102

def self.proxy=(value)
  Koala::Utils.deprecate("HTTPService.proxy is now HTTPService.http_options[:proxy]; .proxy will be removed in a future version.")
  http_options[:proxy] = value
end

.server(options = {}) ⇒ Object



28
29
30
31
32
33
# File 'lib/koala/http_service.rb', line 28

def self.server(options = {})
  server = "#{options[:rest_api] ? Facebook::REST_SERVER : Facebook::GRAPH_SERVER}"
  server.gsub!(/\.facebook/, "-video.facebook") if options[:video]
  server.gsub!(/\.facebook/, ".beta.facebook") if options[:beta]
  "#{options[:use_ssl] ? "https" : "http"}://#{server}"
end

.timeoutObject



77
78
79
80
# File 'lib/koala/http_service.rb', line 77

def self.timeout
  Koala::Utils.deprecate("HTTPService.timeout is now HTTPService.http_options[:timeout]; .timeout will be removed in a future version.")
  http_options[:timeout]
end

.timeout=(value) ⇒ Object



82
83
84
85
# File 'lib/koala/http_service.rb', line 82

def self.timeout=(value)
  Koala::Utils.deprecate("HTTPService.timeout is now HTTPService.http_options[:timeout]; .timeout will be removed in a future version.")
  http_options[:timeout] = value
end

.verify_modeObject



127
128
129
130
# File 'lib/koala/http_service.rb', line 127

def self.verify_mode
  Koala::Utils.deprecate("HTTPService.verify_mode is now (HTTPService.http_options[:ssl] ||= {})[:verify_mode]; .verify_mode will be removed in a future version.")
  (http_options[:ssl] || {})[:verify_mode]
end

.verify_mode=(value) ⇒ Object



132
133
134
135
# File 'lib/koala/http_service.rb', line 132

def self.verify_mode=(value)
  Koala::Utils.deprecate("HTTPService.verify_mode is now (HTTPService.http_options[:ssl] ||= {})[:verify_mode]; .verify_mode will be removed in a future version.")
  (http_options[:ssl] ||= {})[:verify_mode] = value
end