Class: Klaro::Client::RequestHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/klaro/client/request_handler.rb

Constant Summary collapse

Http =
HTTP

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ RequestHandler

Returns a new instance of RequestHandler.



8
9
10
11
12
# File 'lib/klaro/client/request_handler.rb', line 8

def initialize(url)
  @base_url = url.gsub(/\/api\/?$/, "")
  @token = nil
  @subdomain = nil
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



6
7
8
# File 'lib/klaro/client/request_handler.rb', line 6

def base_url
  @base_url
end

#tokenObject (readonly)

Returns the value of attribute token.



6
7
8
# File 'lib/klaro/client/request_handler.rb', line 6

def token
  @token
end

Instance Method Details

#authenticate(user, password) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/klaro/client/request_handler.rb', line 28

def authenticate(user, password)
  @token = get_token Http
  .headers({
    'Content-Type' => 'application/json'
  })
  .post("#{base_url}/api/auth/tokens/", payload(user, password))
end

#authenticated?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/klaro/client/request_handler.rb', line 14

def authenticated?
  not(@token.nil?)
end

#get(endpoint, raw = false) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/klaro/client/request_handler.rb', line 36

def get(endpoint, raw = false)
  url = "#{base_url}#{endpoint}"
  info("GET `#{url}`")
  response = http.get(url, ssl_context: http_ctx)
  raise Error::NoSuchBoardFound if response.status >= 300 || response.status <200

  raw ? response.to_s : JSON.parse(response.to_s)
end

#post(endpoint, body) ⇒ Object



45
46
47
48
49
# File 'lib/klaro/client/request_handler.rb', line 45

def post(endpoint, body)
  url = "#{base_url}#{endpoint}"
  info("POST `#{url}`")
  http.post(url, body.merge(ssl_context: http_ctx))
end

#with_project(subdomain) ⇒ Object



23
24
25
26
# File 'lib/klaro/client/request_handler.rb', line 23

def with_project(subdomain)
  @subdomain = subdomain
  self
end

#with_token(token_type, access_token) ⇒ Object



18
19
20
21
# File 'lib/klaro/client/request_handler.rb', line 18

def with_token(token_type, access_token)
  @token = "#{token_type} #{access_token}"
  self
end