Class: Columbo::APIClient

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

Constant Summary collapse

API_URI =
"http://localhost:15080".freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_uri) ⇒ APIClient

Returns a new instance of APIClient.



9
10
11
12
# File 'lib/columbo/api_client.rb', line 9

def initialize(api_key, api_uri)
  @api_key = api_key
  @api_uri = api_uri || API_URI
end

Instance Method Details

#handshakeObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/columbo/api_client.rb', line 14

def handshake
  Thread.new do
    headers = { "Content-Type" => "application/json; charset=utf-8" }

    uri = URI(@api_uri + '/' + @api_key + '/handshake')
    Net::HTTP.new(uri.host, uri.port).start do |http|
      response = http.get(uri.request_uri, headers)
      json = JSON.parse(response.body)
      json.each { |k, v| yield k, v }
    end
  end
end

#post_data(hash) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/columbo/api_client.rb', line 27

def post_data(hash)
  headers = {
      "Accept-Encoding" => "gzip, deflate",
      "Content-Encoding" => "deflate",
      "Content-Type" => "application/json; charset=utf-8"
  }

  zlib = Columbo::Compressor
  json = hash.merge({api_key: @api_key}).to_json
  payload = zlib.deflate(json)
  uri = URI(@api_uri + '/' + @api_key + '/capture')

  start = Time.now
  Net::HTTP.new(uri.host, uri.port).start do |http|
    response = http.post(uri.request_uri, payload, headers)
    stop = Time.now
    duration = ((stop-start) * 1000).round(3)
    zlib.unzip(response.body, response['Content-Encoding']) + ' - Time: ' + duration.to_s + 'ms'
  end
end