class Stargate::Client::Protocol

Constants

DEFAULT_TIMEOUT
UnsupportedProtocolError

Attributes

codec[R]

Public: Codec used to serialize/deserialize payload.

options[R]

Public: Transport configuration.

safe_uri[R]

Public: URL without credentials part.

uri[R]

Public: Remote URL of the endpoint.

Public Class Methods

[](protocol) click to toggle source

Public: Finds implementation for given protocol.

Returns transport if found. Raises UnsupportedProtocolError if not found.

# File lib/stargate/client/protocol.rb, line 27
def self.[](protocol)
  transports[protocol.downcase] or raise UnsupportedProtocolError, "Unsupported protocol: #{protocol}"
end
new(codec, uri) click to toggle source

Public: Constructor.

# File lib/stargate/client/protocol.rb, line 44
def initialize(codec, uri)
  @codec        = codec
  @uri          = uri
  @safe_uri     = uri.dup.tap { |safe_uri| safe_uri.user, safe_uri.password = nil, nil }
  @options      = default_options.merge(parse_options_from_uri_fragment)
  @unmarshaller = Unmarshaller.new
end
register(protocols, transport) click to toggle source

Public: Registers new implementation for given protocols.

# File lib/stargate/client/protocol.rb, line 19
def self.register(protocols, transport)
  protocols.each { |protocol| transports[protocol] = transport }
end
transports() click to toggle source

Public: List of registered transports (protocol implementations).

# File lib/stargate/client/protocol.rb, line 14
def self.transports
  @@transports
end

Public Instance Methods

call(klass, method, *args) click to toggle source

Public: Shall execute class method with given arguments. Executes method remotely of course.

# File lib/stargate/client/protocol.rb, line 58
def call(klass, method, *args)
  raise NotImplementedError
end
fetch_definitions() click to toggle source

Public: Shall load all definitions from remote server.

# File lib/stargate/client/protocol.rb, line 53
def fetch_definitions
  raise NotImplementedError
end

Protected Instance Methods

default_options() click to toggle source
# File lib/stargate/client/protocol.rb, line 64
def default_options
  { timeout: DEFAULT_TIMEOUT }
end
parse_options_from_uri_fragment() click to toggle source

Internal: Parses options from URI fragment. Example http+json://example.com/v1#timeout=0.3

Returns hash with options.

# File lib/stargate/client/protocol.rb, line 81
def parse_options_from_uri_fragment
  uri.fragment.to_s.split(';').inject({}) do |options, entry|
    k, v = entry.split('=')
    options[k.to_sym] = v
    options
  end
end
unpack_definitions(definitions) click to toggle source

Internal: A helper to unpack remote definitions.

# File lib/stargate/client/protocol.rb, line 74
def unpack_definitions(definitions)
  definitions.map { |metadata| ::Stargate::Metadata.from_hash(metadata) }
end
unpack_payload(data) click to toggle source

Internal: A helper to unpack loaded data.

# File lib/stargate/client/protocol.rb, line 69
def unpack_payload(data)
  @unmarshaller.unmarshal(data)
end