Class: Hoth::Transport::Http

Inherits:
Base
  • Object
show all
Defined in:
lib/hoth/transport/http.rb

Direct Known Subclasses

HttpHmac, Https

Instance Attribute Summary

Attributes inherited from Base

#encoder

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Hoth::Transport::Base

Instance Method Details

#call_remote_with(*params) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/hoth/transport/http.rb', line 6

def call_remote_with(*params)
  begin
    handle_response post_payload(params)
  rescue Exception => e
    raise TransportError.wrap(e)
  end
end

#handle_response(response) ⇒ Object



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

def handle_response(response)
  case response
  when Net::HTTPSuccess
    Hoth::Logger.debug "response.body: #{response.body}"
    encoder.decode(response.body)["result"]
  when Net::HTTPServerError
    begin
      Hoth::Logger.debug "response.body: #{response.body}"
      raise encoder.decode(response.body)["error"]
    rescue JSON::ParserError => jpe
      raise TransportError.wrap(jpe)
    end
  when Net::HTTPRedirection, Net::HTTPClientError, Net::HTTPInformation, Net::HTTPUnknownResponse
    raise NotImplementedError, "code: #{response.code}, message: #{response.body}"
  end
end

#post_payload(payload) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/hoth/transport/http.rb', line 31

def post_payload(payload)
  uri = URI.parse(self.endpoint.to_url)
  return Net::HTTP.post_form(uri,
    'name'   => self.name.to_s,
    'params' => encoder.encode(payload)
  )
end