Class: SamtrafikenAPI::APIBase

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/samtrafiken_api/api_base.rb

Overview

Base class for API implementations.

Direct Known Subclasses

Resrobot, ResrobotStops, Stationinfo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ APIBase

Initialize a new API object.

Parameters:

  • api_key (String)

    The ResRobot Sök resa API key.



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

#responseHTTParty::Response

Returns the HTTParty response object for the last request, which can be used for inspecting errors etc.

Returns:

  • (HTTParty::Response)

    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.

Parameters:

  • response (HTTParty:Response)

    the HTTParty response.

Returns:

  • (Hash)

    a hash containing the returned 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