Module: DVB

Defined in:
lib/dvb.rb,
lib/dvb/find.rb,
lib/dvb/utils.rb,
lib/dvb/monitor.rb,
lib/dvb/version.rb

Defined Under Namespace

Classes: Departure, Stop, TransportMode

Constant Summary collapse

VERSION =
'1.1.0'

Class Method Summary collapse

Class Method Details

.distance_between(lat1, lng1, lat2, lng2) ⇒ Object



74
75
76
77
78
79
# File 'lib/dvb/utils.rb', line 74

def self.distance_between(lat1, lng1, lat2, lng2)
  point1 = Geokit::LatLng.new(lat1, lng1)
  point2 = Geokit::LatLng.new(lat2, lng2)

  point1.distance_to(point2) * 1000
end

.find(search_str, region = 'Dresden') ⇒ Object



2
3
4
5
6
7
8
9
10
11
# File 'lib/dvb/find.rb', line 2

def self.find(search_str, region = 'Dresden')
  all_stops = load_local_stops
  all_stops.select do |stop|
    # TODO: Improve the matching here. Something relatively fuzzy would awesome.
    search_str.downcase!
    (stop.name.downcase.include?(search_str) || stop.search_str.downcase.include?(search_str)) && stop.region == region
  end
  # The following would theoretically make sense, if priority values weren't so weird...
  # all_stops.sort_by { |s| s.priority }.reverse
end

.find_near(lat, lng, radius = 500) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/dvb/find.rb', line 13

def self.find_near(lat, lng, radius = 500)
  # Unfortunately some stops (exactly 100 of 4480) don't have location data associated with them
  # Filtering those out here, as they're not relevant for location-based searching
  all_stops = load_local_stops.reject { |s| s.lat.nil? || s.lng.nil? }
  all_stops.select do |stop|
    DVB::distance_between(stop.lat, stop.lng, lat, lng) <= radius
  end
end

.load_local_stopsObject



22
23
24
25
# File 'lib/dvb/find.rb', line 22

def self.load_local_stops
  path = File.expand_path(File.dirname(__FILE__)) + '/VVOStops.json'
  JSON.parse(File.read(path)).map { |e| DVB::Stop.with_str_hash(e) }
end

.monitor(stop, offset = 0, lim = 0, city = 'Dresden') ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/dvb/monitor.rb', line 8

def self.monitor(stop, offset = 0, lim = 0, city = 'Dresden')
  response = RestClient.get 'http://widgets.vvo-online.de/abfahrtsmonitor/Abfahrten.do',
    { params: {
      ort: city,
      hst: stop,
      vz: offset,
      lim: lim
    }}
  JSON.parse(response.to_str).map { |e| Departure.with_arr(e) }
end

.parse_mode(mode) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dvb/utils.rb', line 6

def self.parse_mode(mode)
  if mode.is_i?
    mode = mode.to_i
    return TransportMode::TRAM if mode.between?(0, 59)
    return TransportMode::CITYBUS if mode.between?(60, 99)
    return TransportMode::REGIOBUS if mode.between?(100, 1000)
  end

  return TransportMode::LIFT if mode == 'SWB'

  if /^E(\d+)/.match(mode)
    match = /^E(\d+)/.match(mode)
    return TransportMode::TRAM if match[1].to_i <= 59
    return TransportMode::CITYBUS
  end

  return TransportMode::REGIOBUS if /^\D$|^\D\/\D$/.match(mode)
  return TransportMode::FERRY if /^F/.match(mode)
  return TransportMode::TRAIN if /^RE|^IC|^TL|^RB|^SB|^SE|^U\d/.match(mode)
  return TransportMode::METROPOLITAN if /^S/.match(mode)
  return TransportMode::AST if /alita/.match(mode)

  nil
end