Class: FindworkApi::Resource
- Inherits:
-
Object
- Object
- FindworkApi::Resource
- Defined in:
- lib/findwork_api/resource.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #default_headers ⇒ Object
- #get_request(url, params: {}, headers: {}) ⇒ Object
- #handle_response(response) ⇒ Object
-
#initialize(client) ⇒ Resource
constructor
A new instance of Resource.
Constructor Details
#initialize(client) ⇒ Resource
Returns a new instance of Resource.
5 6 7 |
# File 'lib/findwork_api/resource.rb', line 5 def initialize(client) @client = client end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
3 4 5 |
# File 'lib/findwork_api/resource.rb', line 3 def client @client end |
Instance Method Details
#default_headers ⇒ Object
13 14 15 |
# File 'lib/findwork_api/resource.rb', line 13 def default_headers { Authorization: "Token #{client.api_key}" } end |
#get_request(url, params: {}, headers: {}) ⇒ Object
9 10 11 |
# File 'lib/findwork_api/resource.rb', line 9 def get_request(url, params: {}, headers: {}) handle_response(client.connection.get(url, params, default_headers.merge(headers))) end |
#handle_response(response) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/findwork_api/resource.rb', line 17 def handle_response(response) = (response.body || {})["error"] case response.status when 400 raise Error, when 401 raise Error, when 403 raise Error, when 404 raise Error, when 422 raise Error, when 429 raise Error, when 500 raise Error, end response end |