class Activite::HTTP::Request

Public Class Methods

new(access_token, headers) click to toggle source
# File lib/activite/http/request.rb, line 10
def initialize(access_token, headers)
  @access_token = access_token
  @headers = headers
end

Public Instance Methods

get(path, options = {}) click to toggle source
# File lib/activite/http/request.rb, line 15
def get(path, options = {})
  request(:get, path, options)
end
post(path, options = {}) click to toggle source
# File lib/activite/http/request.rb, line 19
def post(path, options = {})
  request(:post, path, options)
end

Protected Instance Methods

hash_to_query(hash) click to toggle source
# File lib/activite/http/request.rb, line 25
def hash_to_query(hash)
  return URI.encode(hash.map{|k,v| "#{k}=#{v}"}.join("&"))
end
request(method, path, options = {}) click to toggle source
# File lib/activite/http/request.rb, line 40
def request(method, path, options = {})
  if [:post, :put].include? method
    response = request_with_body(method, path, options)
  else
    response = request_with_query_string(method, path, options)
  end

  if response.code.to_i < 200 or response.code.to_i >= 400
    raise Activite::Error::ClientConfigurationError, response.body
  end

  body = JSON.parse(response.body)
  if body['status'].to_i != 0
    raise Activite::Error::InvalidResponseError, "#{body['status']} - #{body['error']}"
  end

  body['body'] ||= body
  body['body']
end
request_with_body(method, path, options = {}) click to toggle source
# File lib/activite/http/request.rb, line 29
def request_with_body(method, path, options = {})
  body = hash_to_query(options)
  uri  = "#{BASE_URI}#{path}"
  @access_token.send(method, uri, body, @headers)
end
request_with_query_string(method, path, options = {}) click to toggle source
# File lib/activite/http/request.rb, line 35
def request_with_query_string(method, path, options = {})
  uri = "#{BASE_URI}#{path}?#{hash_to_query(options)}"
  @access_token.send(method, uri, @headers)
end