class Wire::HttpTransport

Public Class Methods

new(host, port = nil) click to toggle source
# File lib/wire.rb, line 155
def initialize (host, port = nil)
  @type_signatures = {'Fixnum'.freeze => 'I', 'String'.freeze => 'G', 'Array'.freeze => '!Enumerable', 'Hash'.freeze => '!Dictionary'}

  @host = host
  if port != nil
    @host += ':' + port
  end
end

Public Instance Methods

call(service, method, args = nil, result_type = nil) click to toggle source

call a service method

# File lib/wire.rb, line 197
def call(service, method, args = nil, result_type = nil)
  function = Function.new(method, (args == nil) ? nil : get_signature(args), result_type)
  contexts = [
      {'auth'.freeze => {'user'.freeze => {'id'.freeze => '1'}, 'organization'.freeze => {'id'.freeze => '1'}, 'proxy'.freeze => {'id'.freeze => '-1'}}},
      {'core'.freeze => {'transaction'.freeze => {'id'.freeze => UUID.new.generate.to_s}, 'stream'.freeze => {'id'.freeze => UUID.new.generate.to_s}}}
  ]

  #  puts 'Calling ' + service + '.' + method #+ signature.to_s
  message = self.transmit(service, '1', InvocationSignal.new(function, args, contexts), 60)
  result = message.result
  return result['body']
end
get_signature(args) click to toggle source

return array with the type of each argument

# File lib/wire.rb, line 211
def get_signature(args)
  arg_types=[]
  args.each do |arg|
    arg_types.push(get_type(arg))
  end
  return arg_types
end
get_type(arg) click to toggle source
# File lib/wire.rb, line 219
def get_type (arg)
  return @type_signatures[arg.class.to_s]
end
transmit(service_name, version, invocation_signal, timeout_seconds) click to toggle source
# File lib/wire.rb, line 164
def transmit (service_name, version, invocation_signal, timeout_seconds)
  message = Message.new

  begin
    uri = URI("#{@host}/api/message")
    headers = {'Content-Type' => 'application/json'}
    http = Net::HTTP.new(uri.host, uri.port)
    http.read_timeout = timeout_seconds

    invocation_signal.function.service = service_name
    invocation_signal.function.version = version

    context_hash = {}
    invocation_signal.contexts.each { |context| context.each { |key, value| context_hash[key] = value } }
    invocation_signal.contexts = context_hash

    res = http.post(uri.path, invocation_signal.to_json, headers)

    case res
      when Net::HTTPSuccess
        response = JSON.parse res.body
        message.complete(response)
      else
        res.error!
    end
  rescue Timeout::Error
    message.timeout
  end

  message
end