class KVV::Liveapi
Constants
- API_BASE
- API_KEY
- URL_PATH_DEPARTURES_BY_LATLON
- URL_PATH_DEPARTURES_BY_ROUTE
- URL_PATH_DEPARTURES_BY_STOP
- URL_PATH_STOPS_BY_NAME
- VERSION
Public Class Methods
departures_by_route(route: nil, stop_id: nil)
click to toggle source
# File lib/kvv/liveapi.rb, line 24 def self.departures_by_route route: nil, stop_id: nil return {} unless route && stop_id fetch_api_path [URL_PATH_DEPARTURES_BY_ROUTE, route, stop_id] end
departures_bystop(stop_id)
click to toggle source
# File lib/kvv/liveapi.rb, line 19 def self.departures_bystop stop_id return {} unless stop_id fetch_api_path [URL_PATH_DEPARTURES_BY_STOP, stop_id] end
departures_bystop_name(name)
click to toggle source
# File lib/kvv/liveapi.rb, line 15 def self.departures_bystop_name name self.departures_bystop guess_stop_id_by_name(name) end
stops_by_latlon(lat: nil, lon: nil)
click to toggle source
# File lib/kvv/liveapi.rb, line 35 def self.stops_by_latlon lat: nil, lon: nil return [] unless lat && lon response = fetch_api_path [URL_PATH_DEPARTURES_BY_LATLON, String(lat), String(lon)] response["stops"] || {} end
stops_by_name(name)
click to toggle source
# File lib/kvv/liveapi.rb, line 29 def self.stops_by_name name return [] if name.to_s.empty? response = fetch_api_path [URL_PATH_STOPS_BY_NAME, name] response["stops"] || {} end
Private Class Methods
fetch(url)
click to toggle source
# File lib/kvv/liveapi.rb, line 48 def self.fetch url uri = URI( url ) request = Net::HTTP::Get.new(uri) req_options = { use_ssl: uri.scheme == "https", } response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| http.request(request) end case response.code when "200" JSON.parse(response.body) else {} end end
fetch_api_path(*path)
click to toggle source
# File lib/kvv/liveapi.rb, line 43 def self.fetch_api_path *path path = [ path ].flatten.map{ |p| CGI.escape p }.join("/") fetch [API_BASE, path , API_KEY].join end
guess_stop_id_by_name(name)
click to toggle source
# File lib/kvv/liveapi.rb, line 68 def self.guess_stop_id_by_name name first_stop = stops_by_name(name).first first_stop["id"] unless first_stop.nil? end