Class: Wire::HttpTransport

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

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ HttpTransport

Returns a new instance of HttpTransport.



155
156
157
158
# File 'lib/wire.rb', line 155

def initialize (host, port)
  @host = host
  @port = port
end

Instance Method Details

#transmit(service_name, version, invocation_signal, timeout_seconds) ⇒ Object



160
161
162
163
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
# File 'lib/wire.rb', line 160

def transmit (service_name, version, invocation_signal, timeout_seconds)
  message = Message.new

  begin
    uri = URI("#{@host}:#{@port}/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