Class: Kasabi::BaseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/kasabi/api/base_client.rb

Overview

Base class for API clients

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, options = {}) ⇒ BaseClient

Initialize the client to work with a specific endpoint

The options hash can contain the following values:

  • :apikey: required. apikey authorized to use the API

  • :client: HTTPClient object instance



15
16
17
18
19
# File 'lib/kasabi/api/base_client.rb', line 15

def initialize(endpoint, options={})
  @endpoint = endpoint   
  @client = options[:client] || HTTPClient.new()
  @apikey = options[:apikey] || nil                    
end

Instance Attribute Details

#apikeyObject (readonly)

Returns the value of attribute apikey.



8
9
10
# File 'lib/kasabi/api/base_client.rb', line 8

def apikey
  @apikey
end

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/kasabi/api/base_client.rb', line 7

def client
  @client
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



6
7
8
# File 'lib/kasabi/api/base_client.rb', line 6

def endpoint
  @endpoint
end

Instance Method Details

#client_optionsObject



21
22
23
# File 'lib/kasabi/api/base_client.rb', line 21

def client_options
  {:apikey => @apikey, :client => @client}        
end

#get(uri, query = nil, headers = {}) ⇒ Object



25
26
27
28
# File 'lib/kasabi/api/base_client.rb', line 25

def get(uri, query=nil, headers={})
  headers["X_KASABI_APIKEY"] = @apikey
  return @client.get(uri, query, headers)
end

#post(uri, body = "", headers = {}) ⇒ Object



30
31
32
33
# File 'lib/kasabi/api/base_client.rb', line 30

def post(uri, body="", headers={})
  headers["X_KASABI_APIKEY"] = @apikey      
  return @client.post(uri, body, headers)
end

#validate_response(response) ⇒ Object



35
36
37
38
39
# File 'lib/kasabi/api/base_client.rb', line 35

def validate_response(response)
  if response.status != 200
    raise "Unable to perform request. Status: #{response.status}. Message: #{response.content}"
  end      
end