Class: TuggApi::Client

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

Constant Summary collapse

TUGG_API =
def initialize(api_key, api_version = 1)
  @api_key = api_key
  @api_version = api_version
end

Instance Method Summary collapse

Instance Method Details

#get(options = {}) ⇒ Object

api_type: ‘titles’ or ‘events’ id of event or title format, defaults to json debug = true, prints to stdout



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tugg_api/client.rb', line 17

def get(options={})
  api_type = options[:api_type]
  id = options[:id]
  format = options[:format] || 'json'
  debug = options[:debug] || false

  response = nil # return value

  EventMachine.run do
    conn_options = {
      connect_timeout: 5,        # default connection setup timeout
      inactivity_timeout: 10    # default connection inactivity (post-setup) timeout
    }
    req_options = {
      redirects: 1,             # in case we're redirected by changes to Tugg API
      path: "api/#{api_type}/#{id}.#{format}",
      head: {'TUGG-API-KEY' => @api_key, 'TUGG-API-VERSION' => @api_version}
    }
    http = EventMachine::HttpRequest.new('https://www.tugg.com/', conn_options).get req_options

    http.errback do
      response = http.response
      p response if debug
      EM.stop
    end
    http.callback do
      response = http.response
      if debug
        p http.response_header.status
        p http.response_header
        p response
      end
      EM.stop
    end
  end

  return response
end