class Promoter::Request

Public Class Methods

delete(url) click to toggle source
# File lib/promoter/request.rb, line 11
def self.delete(url)
  response = HTTParty.delete(url, headers: auth_header)
  parse_response(response)
end
get(url) click to toggle source
# File lib/promoter/request.rb, line 16
def self.get(url)
  response = HTTParty.get(url, headers: auth_header)
  parse_response(response)
end
post(url, params) click to toggle source
# File lib/promoter/request.rb, line 27
def self.post(url, params)
  response_format = params.delete(:response_format) || :json
  response = HTTParty.post(url, body: params.to_json, headers: auth_header)
  parse_response(response, response_format)
end
put(url, params) click to toggle source
# File lib/promoter/request.rb, line 21
def self.put(url, params)
  response_format = params.delete(:response_format) || :json
  response = HTTParty.put(url, body: params.to_json, headers: auth_header)
  parse_response(response, response_format)
end

Private Class Methods

auth_header() click to toggle source
# File lib/promoter/request.rb, line 54
def self.auth_header
  if Promoter.api_key.nil?
    raise Errors::Unauthorized.new("You need to set your promoter api key. You can register for a Promoter API key with a Promoter.io Account.")
  end
  { "Authorization" => "Token #{Promoter.api_key}",
    'Content-Type' => 'application/json' }
end
display_debug(response) click to toggle source
# File lib/promoter/request.rb, line 46
def self.display_debug(response)
  if Promoter.debug
    puts "-" * 20 + " DEBUG " + "-" * 20
    puts response
    puts "-" * 18 + " END DEBUG " + "-" * 18
  end
end
parse_response(response, response_format=:json) click to toggle source
# File lib/promoter/request.rb, line 35
def self.parse_response(response, response_format=:json)
  display_debug(response.body)
  response_body = begin
                    JSON.parse(response.body.to_s)
                  rescue
                    response.body.to_s
                  end
  check_for_error(response.response.code, response_body)
  response_body
end