Class: Hermetic::Transport::Http
- Inherits:
-
Object
- Object
- Hermetic::Transport::Http
- Defined in:
- lib/hermetic/transport/http.rb
Overview
Sends a backend-built request spec ({ method:, url:, headers:, body: }) over net/http. The connection factory is injectable so unit tests never open a socket — fakes return canned responses or raise socket errors.
Defined Under Namespace
Classes: Response
Constant Summary collapse
- REQUESTS =
{ "GET" => Net::HTTP::Get, "POST" => Net::HTTP::Post }.freeze
Instance Method Summary collapse
- #call(spec) ⇒ Object
-
#initialize(open_timeout: 5, read_timeout: 65, connector: nil) ⇒ Http
constructor
A new instance of Http.
Constructor Details
#initialize(open_timeout: 5, read_timeout: 65, connector: nil) ⇒ Http
Returns a new instance of Http.
19 20 21 22 23 |
# File 'lib/hermetic/transport/http.rb', line 19 def initialize(open_timeout: 5, read_timeout: 65, connector: nil) @open_timeout = open_timeout @read_timeout = read_timeout @connector = connector || method(:default_connector) end |
Instance Method Details
#call(spec) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/hermetic/transport/http.rb', line 25 def call(spec) method = spec.fetch(:method) { raise ConfigError, "http spec needs :method" }.to_s.upcase uri = URI(spec.fetch(:url) { raise ConfigError, "http spec needs :url" }) klass = REQUESTS.fetch(method) { raise ConfigError, "unsupported http method #{method.inspect}" } request = klass.new(uri) (spec[:headers] || {}).each { |name, value| request[name.to_s] = value.to_s } request.body = spec[:body] if spec[:body] raw = @connector.call(uri).request(request) Response.new(status: raw.code.to_i, body: raw.body.to_s) rescue SocketError, SystemCallError, IOError, EOFError, Net::OpenTimeout, Net::ReadTimeout, OpenSSL::SSL::SSLError => e raise TransportError, "#{method} #{uri}: #{e.class}: #{e.}" end |