Class: A4Tools::HTTPTransporter

Inherits:
Transporter show all
Defined in:
lib/acres_client.rb

Instance Method Summary collapse

Methods inherited from Transporter

#connect, #disconnect

Methods included from EventGenerator

#on, #passthrough, #signal

Constructor Details

#initialize(uri) ⇒ HTTPTransporter

Returns a new instance of HTTPTransporter.



48
49
50
51
52
53
54
# File 'lib/acres_client.rb', line 48

def initialize(uri)
  super()
  @uri = uri
  @http = Net::HTTP.new(@uri.host, @uri.port)
  @http.use_ssl = @uri.scheme == 'https'
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

Instance Method Details

#response_body(result) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/acres_client.rb', line 56

def response_body(result)
  return nil unless result.code.to_i >= 200 and result.code.to_i < 300

  jsonrpc = symbolify JSON.parse(result.body)
  return nil unless jsonrpc[:jsonrpc] == "2.0"
  return nil unless jsonrpc[:error].nil?
  return nil if jsonrpc[:result].nil?

  return jsonrpc[:result][:body] unless jsonrpc[:result][:body].nil?

  return jsonrpc[:result]
end

#send_message(msg) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/acres_client.rb', line 69

def send_message(msg)
  req = Net::HTTP::Post.new(@uri.to_s,
    initheader = { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
    )
  req.body = msg
  signal(:sent, msg)
  response = @http.request(req)
  signal(:message, response.body)

  response
end