Class: TrafikantenTravel::Route::Step

Inherits:
Object
  • Object
show all
Defined in:
lib/trafikanten_travel/route.rb

Constant Summary collapse

WALK =

Regexes for matching steps in the HTML

/Gå\s+fra (.+) til (.+) ca. (\d) minutt/u
WAIT =
/Vent\s+(\d+) minutt/
TRANSPORT =
/(\S+) (.+).+Avg: (.+) (\d{2}.\d{2}).+Ank:(.+) (\d{2}.\d{2})/u
TYPE_MAP =
{
  'Tog' => :train,
  'T-bane' => :subway,
  'Sporvogn' => :tram,
  'Båt' => :boat,
  'Buss' => :bus,
  'Flytog' => :airport_express_train
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#arriveObject

Returns the value of attribute arrive.



77
78
79
# File 'lib/trafikanten_travel/route.rb', line 77

def arrive
  @arrive
end

#departObject

Returns the value of attribute depart.



77
78
79
# File 'lib/trafikanten_travel/route.rb', line 77

def depart
  @depart
end

#durationObject

Returns the value of attribute duration.



77
78
79
# File 'lib/trafikanten_travel/route.rb', line 77

def duration
  @duration
end

#lineObject

Returns the value of attribute line.



77
78
79
# File 'lib/trafikanten_travel/route.rb', line 77

def line
  @line
end

#typeObject

Returns the value of attribute type.



77
78
79
# File 'lib/trafikanten_travel/route.rb', line 77

def type
  @type
end

Class Method Details

.duration_between(date, from, to) ⇒ Object

Accepts two HH.MM and will calculate and return the difference in minutes based on the @time date FIXME: This is baaad



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/trafikanten_travel/route.rb', line 126

def self.duration_between(date, from, to)
  from  = from.gsub('.', ':')
  to    = to.gsub('.', ':')

  from_time = TrafikantenTravel::Utils.time_class.parse(date.strftime('%Y-%m-%d') + ' ' + from)
  to_time   = TrafikantenTravel::Utils.time_class.parse(date.strftime('%Y-%m-%d') + ' ' + to)

  if(to.to_f < from.to_f)
    to_time = to_time + (60 * 60 * 24)
  end

  ((to_time - from_time) / 60).to_i
end

.from_html(time, html) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/trafikanten_travel/route.rb', line 93

def self.from_html(time, html)
  step = new
  case html
  when WAIT
    step.type     = :wait
    step.duration = $1.to_i
  when WALK
    step.type     = :walk
    step.duration = $3.to_i
    step.depart   = {
      :station => $1
    }
    step.arrive   = {
      :station => $2
    }
  when TRANSPORT
    step.type     = TYPE_MAP[$1]
    step.line     = $2.strip
    step.duration = duration_between(time, $4, $6)
    step.depart   = {
      :station => $3,
      :time => timestr_to_time(time, $4, $4)
    }
    step.arrive   = {
      :station => $5,
      :time => timestr_to_time(time, $4, $6)
    }
  end
  step
end

.timestr_to_time(date, from_timestr, to_timestr) ⇒ Object

FIXME: Also baaad



141
142
143
144
145
146
147
148
# File 'lib/trafikanten_travel/route.rb', line 141

def self.timestr_to_time(date, from_timestr, to_timestr)
  time = TrafikantenTravel::Utils.time_class.parse(date.strftime('%Y-%m-%d') + ' ' + to_timestr.gsub('.', ':'))

  if(to_timestr.to_f < from_timestr.to_f)
    time = time + (60 * 60 * 24)
  end
  time
end