class Transprt::Client

Constants

DEFAULT_DOMAIN
VERSION

Attributes

domain[R]
version[R]

Public Class Methods

new(domain = DEFAULT_DOMAIN, version = VERSION) click to toggle source
# File lib/transprt/client.rb, line 12
def initialize(domain = DEFAULT_DOMAIN, version = VERSION)
  @domain = domain
  @version = version
end

Public Instance Methods

connections(parameters) click to toggle source

> find connections

# File lib/transprt/client.rb, line 32
def connections(parameters)
  allowed_parameters = %w(from to via date time isArrivalTime
                          transportations limit page direct sleeper
                          couchette bike)

  query = create_query(parameters, allowed_parameters)
  locations = perform('connections', query)

  locations['connections']
end
locations(parameters) click to toggle source

> find locations

# File lib/transprt/client.rb, line 20
def locations(parameters)
  allowed_parameters = %w(query x y type)

  query = create_query(parameters, allowed_parameters)
  locations = perform('locations', query)

  locations['stations']
end
stationboard(parameters) click to toggle source

> find station boards

# File lib/transprt/client.rb, line 46
def stationboard(parameters)
  allowed_parameters = %w(station id limit transportations datetime)

  query = create_query(parameters, allowed_parameters)
  locations = perform('stationboard', query)

  locations['stationboard']
end

Private Instance Methods

create_query(parameters, allowed_parameters) click to toggle source
# File lib/transprt/client.rb, line 75
def create_query(parameters, allowed_parameters)
  parameters.map do |k, v|
    next unless allowed_parameters.include?(k.to_s)

    "#{k}=#{CGI.escape(v)}"
  end.join('&')
end
create_url(endpoint) click to toggle source
# File lib/transprt/client.rb, line 71
def create_url(endpoint)
  [domain, version, endpoint].join('/') + '?'
end
limiter() click to toggle source
# File lib/transprt/client.rb, line 83
def limiter
  @limiter ||= RateLimiting.new
end
perform(endpoint, query) click to toggle source
# File lib/transprt/client.rb, line 59
def perform(endpoint, query)
  url = "#{create_url(endpoint)}#{query}"

  response = limiter.get(url)

  # Uncomment the line below to dump the response in order to generate
  # a file to use as response stub in tests.
  # File.write('/tmp/response.json', response)

  JSON.parse(response)
end