Class: Elementary::Transport::HTTP

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

Constant Summary collapse

CONTENT_TYPE_HEADER =
'Content-Type'.freeze
CONTENT_TYPE =
'application/x-protobuf'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(hosts, opts = {}) ⇒ HTTP

Create a HTTP transport object for sending protobuf objects to the service host names enumerated in hosts

Parameters:

  • hosts (Array)

    A collection of host declarations (=> ”, :port => 0, :prefix => ‘/’)

  • opts (Hash) (defaults to: {})

    Options to be passed directly into Faraday.



20
21
22
23
24
25
# File 'lib/elementary/transport/http.rb', line 20

def initialize(hosts, opts={})
  @hosts = hosts
  @options = Hashie::Mash.new({:logging => true, :logger => nil}).merge(opts)
  # Create connection here to avoid threading issues later. See Issue #43
  client
end

Instance Method Details

#call(service, rpc_method, *params) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/elementary/transport/http.rb', line 27

def call(service, rpc_method, *params)

  begin
    response = client.post do |h|
      path = "#{CGI.escape(service.name)}/#{rpc_method.method}"
      h.url(path)
      h.headers[CONTENT_TYPE_HEADER] = CONTENT_TYPE
      h.body = params[0].encode
    end

    return rpc_method[:response_type].decode(response.body)
  rescue StandardError => e
    if e.respond_to?(:exception)
      raise e.exception("#{service.name}##{rpc_method.method}: #{e.message}")
    else
      # java.lang.Exceptions don't implement #exception
      raise e.class.new("#{service.name}##{rpc_method.method}: #{e.message}")
    end
  end
end