Class: KrkTimetables::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/krk-timetables_line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, destination, url) ⇒ Line

Returns a new instance of Line.



5
6
7
8
9
10
# File 'lib/krk-timetables_line.rb', line 5

def initialize(number, destination, url)
  @number, @destination, @departures = number, destination, {}

  # the following "r" to "t" substitution is needed to get the right page without any frames
  @url = url.gsub(/r|\.\.\//, "r" => "t", "../" => "")
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



3
4
5
# File 'lib/krk-timetables_line.rb', line 3

def destination
  @destination
end

#numberObject (readonly)

Returns the value of attribute number.



3
4
5
# File 'lib/krk-timetables_line.rb', line 3

def number
  @number
end

Instance Method Details

#departuresObject



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
# File 'lib/krk-timetables_line.rb', line 12

def departures
  return @departures unless @departures.empty?

  departure_types = ["work_weekdays", "saturdays", "sundays"]
  departure_types.each do |type|
    @departures[type] = []
  end

  doc = Nokogiri::HTML(open(KrkTimetables::URL_PREFIX + @url))

  doc.css("table table tr").each do |row|
    next if (cells = row.css("td")).size == 3

    hour = nil

    cells.each_with_index do |cell, cell_index|
      if (hour_cell = cell.css("b").first)
        hour = hour_cell.content
      elsif not (minutes = cell.content.split).empty?
        minutes.each do |minute|
          next if minute == "-"

          type = departure_types[cell_index/2]

          @departures[type] << KrkTimetables::Departure.new(hour.to_i, minute.to_i)
        end
      end
    end
  end

  @departures
end

#typeObject



45
46
47
# File 'lib/krk-timetables_line.rb', line 45

def type
  number < 100 ? "tram" : "bus"
end