Class: Klaro::Client::RequestHandler
- Inherits:
-
Object
- Object
- Klaro::Client::RequestHandler
- Defined in:
- lib/klaro/client/request_handler.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #authenticate(user, password) ⇒ Object
- #get(endpoint, raw = false) ⇒ Object
-
#initialize(url) ⇒ RequestHandler
constructor
A new instance of RequestHandler.
- #post(endpoint, body) ⇒ Object
Constructor Details
#initialize(url) ⇒ RequestHandler
Returns a new instance of RequestHandler.
7 8 9 |
# File 'lib/klaro/client/request_handler.rb', line 7 def initialize(url) @base_url = url end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
5 6 7 |
# File 'lib/klaro/client/request_handler.rb', line 5 def base_url @base_url end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
5 6 7 |
# File 'lib/klaro/client/request_handler.rb', line 5 def token @token end |
Instance Method Details
#authenticate(user, password) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/klaro/client/request_handler.rb', line 11 def authenticate(user, password) @token = get_token Http .headers({ 'Content-Type' => 'application/json' }) .post("#{base_url}/api/auth/tokens/", payload(user, password)) end |
#get(endpoint, raw = false) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/klaro/client/request_handler.rb', line 19 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
28 29 30 31 32 |
# File 'lib/klaro/client/request_handler.rb', line 28 def post(endpoint, body) url = "#{base_url}#{endpoint}" info("POST `#{url}`") http.post(url, body.merge(ssl_context: http_ctx)) end |