Class: Wire::HttpTransport

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

Instance Method Summary collapse

Constructor Details

#initialize(host, port = nil) ⇒ HttpTransport

Returns a new instance of HttpTransport.



155
156
157
158
159
160
161
162
# 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

Instance Method Details

#call(service, method, args = nil, result_type = nil) ⇒ Object

call a service method



197
198
199
200
201
202
203
204
205
206
207
208
# 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) ⇒ Object

return array with the type of each argument



211
212
213
214
215
216
217
# 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) ⇒ Object



219
220
221
# 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) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# 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