class Dovico::ApiClient

Constants

API_URL
API_VERSION

Attributes

client_token[RW]
user_token[RW]

Public Class Methods

delete(path, params: {}, body: nil) click to toggle source
# File lib/dovico/api_client.rb, line 27
def delete(path, params: {}, body: nil)
  perform!(:delete, path, params: params, body: body)
end
get(path, params: {}) click to toggle source
# File lib/dovico/api_client.rb, line 15
def get(path, params: {})
  perform!(:get, path, params: params)
end
initialize!(client_token, user_token) click to toggle source
# File lib/dovico/api_client.rb, line 10
def initialize!(client_token, user_token)
  @client_token = client_token
  @user_token = user_token
end
post(path, params: {}, body: nil) click to toggle source
# File lib/dovico/api_client.rb, line 19
def post(path, params: {}, body: nil)
  perform!(:post, path, params: params, body: body)
end
put(path, params: {}, body: nil) click to toggle source
# File lib/dovico/api_client.rb, line 23
def put(path, params: {}, body: nil)
  perform!(:put, path, params: params, body: body)
end

Private Class Methods

authorization_token() click to toggle source
# File lib/dovico/api_client.rb, line 35
def authorization_token
  "WRAP access_token=\"client=#{client_token}&user_token=#{user_token}\""
end
perform!(method, path, params: {}, body: nil) click to toggle source
# File lib/dovico/api_client.rb, line 47
def perform!(method, path, params: {}, body: nil)
  request = Typhoeus::Request.new(
    "#{API_URL}#{path}",
    method: method,
    params: params.merge(version: API_VERSION),
    headers: request_headers,
    body: body,
  )

  response = request.run

  if response.code != 200
    response = JSON.parse(response.body)
    puts "== Error during HTTP request =="
    puts "Status:      #{response["Status"]}"
    puts "Description: #{response["Description"]}"
    puts ""
    raise "Error during HTTP request"
  end

  if response.body.length > 0
    JSON.parse(response.body)
  else
    nil
  end
end
request_headers() click to toggle source
# File lib/dovico/api_client.rb, line 39
def request_headers
  {
    "Accept"        => "application/json",
    "Content-Type"  => "application/json",
    "Authorization" => authorization_token,
  }
end