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_url) ⇒ BasicApiClient

Returns a new instance of BasicApiClient.



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

def initialize(cn, password, site, auth_url)
  @cn = cn
  @password = password
  @site = site
  @auth_url = auth_url
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

Instance Method Details

#authorizeObject



31
32
33
34
35
36
37
38
39
# File 'lib/basic_api_client.rb', line 31

def authorize
  res = conn.post do |req|
    req.url @auth_url 
    req.params['cn'] = @cn
    req.params['password'] = @password
  end

  handle_response(res)
end

#connObject



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

def conn
  @conn ||= begin
    Faraday.new(url: @site) do |faraday|
      faraday.request :url_encoded
      faraday.response :logger
      faraday.adapter Faraday.default_adapter
    end
  end
end

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



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/basic_api_client.rb', line 41

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

  res = conn.get do |req|
    req.url url
    req.params = params.merge(req.params)
    req.headers = headers
  end

  handle_response(res)
end

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



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/basic_api_client.rb', line 53

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

  res = conn.post do |req|
    req.url url
    req.params = params.merge(req.params)
    req.headers = headers
  end

  handle_response(res)
end