Class: Xshellz::NetHttpAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/xshellz/client.rb

Overview

Default HTTP adapter: one Net::HTTP request per call.

Any object responding to call(method, url, headers, body) and returning something with status (Integer) and body (String) can replace it - specs inject a fake so no test ever touches the network.

Instance Method Summary collapse

Constructor Details

#initialize(open_timeout: 10, read_timeout: 120) ⇒ NetHttpAdapter

Returns a new instance of NetHttpAdapter.



14
15
16
17
# File 'lib/xshellz/client.rb', line 14

def initialize(open_timeout: 10, read_timeout: 120)
  @open_timeout = open_timeout
  @read_timeout = read_timeout
end

Instance Method Details

#call(method, url, headers, body) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/xshellz/client.rb', line 19

def call(method, url, headers, body)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.open_timeout = @open_timeout
  http.read_timeout = @read_timeout

  request = Net::HTTP.const_get(method.capitalize).new(uri.request_uri, headers)
  request.body = body unless body.nil?

  response = http.request(request)
  Client::Response.new(status: response.code.to_i, body: response.body.to_s)
end