Class: BasicApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/basic_api_client.rb

Constant Summary collapse

AUTH_HEADERS =
%w{ access-token token-type uid expiry client }.sort

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cn, password, site, auth_path) ⇒ BasicApiClient

Returns a new instance of BasicApiClient.



10
11
12
13
14
15
16
# File 'lib/basic_api_client.rb', line 10

def initialize(cn, password, site, auth_path)
  @cn = cn
  @password = password
  @site = site
  @site.sub!(/\/+$/, '')
  @auth_url = url_for(auth_path)
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/basic_api_client.rb', line 7

def headers
  @headers
end

#last_responseObject

Returns the value of attribute last_response.



8
9
10
# File 'lib/basic_api_client.rb', line 8

def last_response
  @last_response
end

#last_returnObject

Returns the value of attribute last_return.



8
9
10
# File 'lib/basic_api_client.rb', line 8

def last_return
  @last_return
end

Instance Method Details

#authorizeObject



22
23
24
25
26
27
# File 'lib/basic_api_client.rb', line 22

def authorize
  @headers = {}
  params = { cn: @cn, password: @password }
  Curl.post(@auth_url, params, &method(:setup_request))
  last_return
end

#get(path, params = {}) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/basic_api_client.rb', line 29

def get(path, params = {})
  authorize unless auth_headers?

  url = url_for(path)
  Curl.get(url, params, &method(:setup_request))
  last_return
end

#post(path, params = {}) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/basic_api_client.rb', line 37

def post(path, params = {})
  authorize unless auth_headers?

  url = url_for(path)
  Curl.post(url, params, &method(:setup_request))
  last_return
end