Class: Klaro::Client::RequestHandler
- Inherits:
-
Object
- Object
- Klaro::Client::RequestHandler
- Defined in:
- lib/klaro/client/request_handler.rb
Constant Summary collapse
- Http =
HTTP
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, workspace = nil) ⇒ Object
- #authenticated? ⇒ Boolean
- #get(endpoint, raw = false, extra_headers = {}) ⇒ Object
- #get_in_cache(url, &bl) ⇒ Object
-
#initialize(url) ⇒ RequestHandler
constructor
A new instance of RequestHandler.
- #post(endpoint, body) ⇒ Object
- #with_cache(caching_options) ⇒ Object
- #with_project(subdomain) ⇒ Object
- #with_token(token_type, access_token) ⇒ Object
- #with_workspace(workspace) ⇒ Object
Constructor Details
#initialize(url) ⇒ RequestHandler
Returns a new instance of RequestHandler.
8 9 10 11 12 13 14 |
# File 'lib/klaro/client/request_handler.rb', line 8 def initialize(url) @base_url = url.gsub(/\/api\/?$/, "") @token = nil @subdomain = nil @workspace = nil = nil end |
Instance Attribute Details
#base_url ⇒ Object (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 |
#token ⇒ Object (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, workspace = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/klaro/client/request_handler.rb', line 40 def authenticate(user, password, workspace = nil) @workspace = workspace @token = get_token( Http .headers({ 'Content-Type' => 'application/json' }) .post("#{base_url}/api/auth/tokens/", payload(user, password)) ) end |
#authenticated? ⇒ Boolean
16 17 18 |
# File 'lib/klaro/client/request_handler.rb', line 16 def authenticated? not(@token.nil?) end |
#get(endpoint, raw = false, extra_headers = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/klaro/client/request_handler.rb', line 51 def get(endpoint, raw = false, extra_headers = {}) url = "#{base_url}#{endpoint}" info("GET `#{url}`") response = get_in_cache(url) do res = http(extra_headers).get(url, ssl_context: http_ctx) raise Error::NoSuchBoardFound unless res.status == 200 res.to_s end raw ? response.to_s : JSON.parse(response.to_s) end |
#get_in_cache(url, &bl) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/klaro/client/request_handler.rb', line 62 def get_in_cache(url, &bl) return bl.call unless sha = Digest::SHA1.hexdigest(url) file = [:path]/sha if file.file? file.read else str = bl.call file.parent.mkdir_p file.write(str) str end end |
#post(endpoint, body) ⇒ Object
77 78 79 80 81 |
# File 'lib/klaro/client/request_handler.rb', line 77 def post(endpoint, body) url = "#{base_url}#{endpoint}" info("POST `#{url}`") http.post(url, body.merge(ssl_context: http_ctx)) end |
#with_cache(caching_options) ⇒ Object
35 36 37 38 |
# File 'lib/klaro/client/request_handler.rb', line 35 def with_cache() = self end |
#with_project(subdomain) ⇒ Object
25 26 27 28 |
# File 'lib/klaro/client/request_handler.rb', line 25 def with_project(subdomain) @subdomain = subdomain self end |
#with_token(token_type, access_token) ⇒ Object
20 21 22 23 |
# File 'lib/klaro/client/request_handler.rb', line 20 def with_token(token_type, access_token) @token = "#{token_type} #{access_token}" self end |
#with_workspace(workspace) ⇒ Object
30 31 32 33 |
# File 'lib/klaro/client/request_handler.rb', line 30 def with_workspace(workspace) @workspace = workspace self end |