module InvisibleCollector::DefaultHandlers

Public Class Methods

new(options = {}) click to toggle source
# 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

Public Instance Methods

execute() { |connection| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/invisible_collector/resources/default_handlers.rb, line 36
def handle(code, &block)
  handles[code] = block
end
handles() click to toggle source
# File lib/invisible_collector/resources/default_handlers.rb, line 32
def handles
  @handles ||= {}
end