Class: Elementary::Connection

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

Constant Summary collapse

DEFAULT_HOSTS =
[{:host => 'localhost', :port => 8000}].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, opts = {}) ⇒ Connection

Initialize a connection to the Protobuf::Rpc::Service

Parameters:

  • service (Protobuf::Rpc::Service)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :transport_options (Hash)

    A Hash of request options that will be passed down to the transport layer. This will depend on what options are available by the underlying transport

  • :future_options (Hash)

    A Hash of options to use when constructing the Concurrent::Future to run the RPC. In particular, it allows specifying the :executor for the Concurrent::Future.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/elementary/connection.rb', line 28

def initialize(service, opts={})
  opts = Hashie::Mash.new(opts)

  if service.nil? || service.superclass != Protobuf::Rpc::Service
    raise ArgumentError,
      "Cannot construct an Elementary::Connection with `#{service}` (#{service.class})"
  end

  @service = service
  @transport = opts[:transport]
  @hosts = opts[:hosts] || DEFAULT_HOSTS
  @transport_opts = opts[:transport_options] || {}
  @future_opts = opts[:future_options] || {}
  # Create exectutor here to avoid threading issues later. See Issue #43
  rpc
end

Instance Attribute Details

#raise_on_errorObject (readonly)

Returns the value of attribute raise_on_error.



10
11
12
# File 'lib/elementary/connection.rb', line 10

def raise_on_error
  @raise_on_error
end

#serviceObject (readonly)

Returns the value of attribute service.



10
11
12
# File 'lib/elementary/connection.rb', line 10

def service
  @service
end

Instance Method Details

#rpcObject



45
46
47
# File 'lib/elementary/connection.rb', line 45

def rpc
  @rpc ||= Elementary::Executor.new(@service, select_transport, @future_opts)
end

#select_transportObject



49
50
51
# File 'lib/elementary/connection.rb', line 49

def select_transport
  Elementary::Transport::HTTP.new(@hosts, @transport_opts)
end