Module: Requests

Extended by:
Sugar
Defined in:
lib/requests.rb,
lib/requests/sugar.rb

Defined Under Namespace

Modules: Sugar Classes: Error, Response

Constant Summary collapse

CA_FILE =
ENV.fetch('REQUESTS_CA_FILE',
File.expand_path('../cacert.pem', __FILE__))

Class Method Summary collapse

Methods included from Sugar

delete, get, head, options, patch, post, put, trace

Class Method Details

.request(method, url, headers: {}, data: nil, params: nil, auth: nil, proxy: nil, options: {}) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/requests.rb', line 20

def self.request(method, url,
  headers: {},
  data: nil,
  params: nil,
  auth: nil,
  proxy: nil,
  options: {})

  uri = URI.parse(url)
  uri.query = encode_www_form(params) if params

  body = process_params(headers: headers, data: data) if data

  basic_auth(headers, *auth) if auth

  proxy = proxy.to_h.values_at(:host, :port, :user, :password)
  response = Net::HTTP.start(uri.host, uri.port, *proxy, opts(uri, options)) do |http|
    http.send_request(method, uri, body, headers)
  end

  if response.is_a?(Net::HTTPSuccess)
    Response.new(response.code, response.to_hash, response.body)
  else
    raise Error, response
  end
end