Class: SamtrafikenAPI::APIBase
- Inherits:
-
Object
- Object
- SamtrafikenAPI::APIBase
- Includes:
- HTTParty
- Defined in:
- lib/samtrafiken_api/api_base.rb
Overview
Base class for API implementations.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#response ⇒ HTTParty::Response
The HTTParty response object for the last request, which can be used for inspecting errors etc.
Instance Method Summary collapse
-
#initialize(api_key) ⇒ APIBase
constructor
Initialize a new API object.
-
#read_response(response) ⇒ Hash
Read a HTTParty response and return parsed data.
Constructor Details
#initialize(api_key) ⇒ APIBase
Initialize a new API object.
15 16 17 18 19 20 |
# File 'lib/samtrafiken_api/api_base.rb', line 15 def initialize(api_key) self.class.default_params({ apiVersion: '2.1', key: api_key }) end |
Instance Attribute Details
#response ⇒ HTTParty::Response
Returns the HTTParty response object for the last request, which can be used for inspecting errors etc.
10 11 12 |
# File 'lib/samtrafiken_api/api_base.rb', line 10 def response @response end |
Instance Method Details
#read_response(response) ⇒ Hash
Read a HTTParty response and return parsed data.
28 29 30 31 32 33 34 35 |
# File 'lib/samtrafiken_api/api_base.rb', line 28 def read_response response data = response.parsed_response.values[0] if response.code == 200 data else raise data['errors']['error']['errordescription'] end end |