Module: InvisibleCollector::DefaultHandlers

Instance Method Summary collapse

Instance Method Details

#executeObject



10
11
12
13
14
# File 'lib/invisible_collector/resources/default_handlers.rb', line 10

def execute
  response = yield(@connection)
  handles[response.status].call(response) if handles.key?(response.status)
  response
end

#execute_get(endpoint, params) ⇒ Object



16
17
18
19
20
# File 'lib/invisible_collector/resources/default_handlers.rb', line 16

def execute_get(endpoint, params)
  execute do |connection|
    connection.get(endpoint, params)
  end
end

#execute_post(endpoint, body) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/invisible_collector/resources/default_handlers.rb', line 22

def execute_post(endpoint, body)
  execute do |connection|
    connection.post do |req|
      req.url endpoint
      req.headers['Content-Type'] = 'application/json'
      req.body = (body.is_a?(String) ? body : body.to_json)
    end
  end
end

#handle(code, &block) ⇒ Object



36
37
38
# File 'lib/invisible_collector/resources/default_handlers.rb', line 36

def handle(code, &block)
  handles[code] = block
end

#handlesObject



32
33
34
# File 'lib/invisible_collector/resources/default_handlers.rb', line 32

def handles
  @handles ||= {}
end

#initialize(options = {}) ⇒ Object



5
6
7
8
# File 'lib/invisible_collector/resources/default_handlers.rb', line 5

def initialize(options = {})
  @connection = options[:connection]
  handle(401) { |response| raise InvisibleCollector::Unauthorized.from_json(response.body) }
end