main.rb
##
# Combines /ip, /locations and /stationboard GET requests to the Transport Opendata API.
#
# Takes an IP address as parameter and returns the next 5 (train) connections running from 
# the nearest train station to this IP location.
#
# Parameter IP is mandatory.
Combines /ip, /locations and /stationboard GET requests to the Transport Opendata API.
Takes an IP address as parameter and returns the next 5 (train) connections running from
the nearest train station to this IP location.

Parameter IP is mandatory.
Combines / ip / locations and / stationboard GET requests to the Transport Opendata API . Takes an IP address as parameter and returns the next 5 ( train ) connections running from the nearest train station to this IP location . Parameter IP is mandatory .
def doStationsRequest(_params)
  res = doIPRequest(params)
  ip = JSON.parse(res)
  res = doLocationRequest({"type"=>"station","transportations[]"=>"ec_ic","transportations[]"=>"ice_tgv_rj","transportations[]"=>"ir","transportations[]"=>"re_d", "x" => ip["lat"].to_s, "y" => ip["lon"].to_s})
  location = JSON.parse(res)
  station = (location["stations"].sort_by { |hash| hash['distance'].to_i }).first
  if station.nil?
    return doError('No station was found!')
  else
    departing_station_name = station["name"]
    res = doStationboardRequest({"id"=>station["id"].to_s, "limit"=>"5", "transportations[]"=>"ec_ic", "transportations[]"=>"ice_tgv_rj", "transportations[]"=>"ir", "transportations[]"=>"re_d"})
    stationboard = JSON.parse(res)
    return ({ departing_station_name: departing_station_name, stationboard: stationboard["stationboard"].first(5) }.to_json)
  end
end
def doStationsRequest ( _params ) res = doIPRequest ( params ) ip = JSON . parse ( res ) res = doLocationRequest ( { "type" => "station" , "transportations[]" => "ec_ic" , "transportations[]" => "ice_tgv_rj" , "transportations[]" => "ir" , "transportations[]" => "re_d" , "x" => ip [ "lat" ] . to_s , "y" => ip [ "lon" ] . to_s } ) location = JSON . parse ( res ) station = ( location [ "stations" ] . sort_by { | hash | hash [ 'distance' ] . to_i } ) . first if station . nil? return doError ( 'No station was found!' ) else departing_station_name = station [ "name" ] res = doStationboardRequest ( { "id" => station [ "id" ] . to_s , "limit" => "5" , "transportations[]" => "ec_ic" , "transportations[]" => "ice_tgv_rj" , "transportations[]" => "ir" , "transportations[]" => "re_d" } ) stationboard = JSON . parse ( res ) return ( { departing_station_name : departing_station_name , stationboard : stationboard [ "stationboard" ] . first ( 5 ) } . to_json ) end end
Combines /ip, /locations and /stationboard GET requests to the Transport Opendata API.
Combines / ip / locations and / stationboard GET requests to the Transport Opendata API .
# Get the stationboard with the next 5 trains leaving from the given station. # StationsRequest param limit=5 can respond more than 5 elements (if on same time) # therefore the filter first(5) must be applied
_params
<not_specific>