Class: Natour::PublicTransport

Inherits:
Object
  • Object
show all
Defined in:
lib/natour/public_transport.rb

Class Method Summary collapse

Class Method Details

.search_station(position, radius: 200) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/natour/public_transport.rb', line 8

def self.search_station(position, radius: 200)
  position = position.join(',') if position.is_a?(Array)
  uri = URI('https://timetable.search.ch/api/completion.json')
  uri.query = URI.encode_www_form(latlon: position.gsub(' ', ''))
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  response = http.request(Net::HTTP::Get.new(uri))
  return unless response.is_a?(Net::HTTPSuccess)

  stations = JSON.parse(response.body, symbolize_names: true)
  stations.reject! { |station| !station.key?(:dist) || station[:dist] > radius }
  station_types = %w[
    sl-icon-type-train
    sl-icon-type-strain
    sl-icon-type-tram
    sl-icon-type-bus
    sl-icon-type-ship
    sl-icon-type-funicular
    sl-icon-type-cablecar
    sl-icon-type-gondola
    sl-icon-type-chairlift
  ]

  stations = station_types.map do |station_type|
    stations.find { |station| station[:iconclass] == station_type }
  end

  station = stations.compact.first
  return unless station

  Station.new(
    station[:label],
    station[:iconclass].delete_prefix('sl-icon-type-').to_sym,
    station[:dist]
  )
end