Class: Pickpoint::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/pickpoint/http.rb

Overview

Thin Net::HTTP wrapper. Optional adapter responds to call(method, url, headers, body) -> [status, body_string].

Instance Method Summary collapse

Constructor Details

#initialize(timeout:, adapter: nil) ⇒ Http

Returns a new instance of Http.



10
11
12
13
# File 'lib/pickpoint/http.rb', line 10

def initialize(timeout:, adapter: nil)
  @timeout = timeout
  @adapter = adapter
end

Instance Method Details

#request(method, url, headers: {}, body: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pickpoint/http.rb', line 15

def request(method, url, headers: {}, body: nil)
  return @adapter.call(method, url, headers, body) if @adapter

  uri = URI(url)
  req = build_request(method, uri, body)
  headers.each { |k, v| req[k] = v }

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.open_timeout = @timeout
  http.read_timeout = @timeout
  res = http.request(req)
  [res.code.to_i, res.body.to_s]
end