Class: TrafikantenTravel::Route

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

Defined Under Namespace

Classes: Step

Constant Summary collapse

BASE_URL =
'http://m.trafikanten.no/BetRes.asp?'
NOTFOUND =
/Ingen forbindelse funnet eller ingen stoppesteder funnet|Ingen turer ankommer ankomststed|Ingen turer utgår fra avgangsted/u

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

fetch, time_class

Constructor Details

#initialize(from, to, time = TrafikantenTravel::Utils.time_class.now) ⇒ Route

Returns a new instance of Route.



20
21
22
23
24
25
# File 'lib/trafikanten_travel/route.rb', line 20

def initialize(from, to, time = TrafikantenTravel::Utils.time_class.now)
  @from = from
  @to = to
  @time = time
  @steps = []
end

Instance Attribute Details

#durationObject

Returns the value of attribute duration.



9
10
11
# File 'lib/trafikanten_travel/route.rb', line 9

def duration
  @duration
end

#stepsObject

Returns the value of attribute steps.



9
10
11
# File 'lib/trafikanten_travel/route.rb', line 9

def steps
  @steps
end

Class Method Details

.find(from, to, time = TrafikantenTravel::Utils.time_class.now) ⇒ Object

Searches and returns a Route object for the found trip



14
15
16
17
18
# File 'lib/trafikanten_travel/route.rb', line 14

def self.find(from, to, time = TrafikantenTravel::Utils.time_class.now)
  route = Route.new(from, to, time)
  route.parse
  route
end

Instance Method Details

#parseObject

Parse the received HTML. First try some simple error-checking.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/trafikanten_travel/route.rb', line 28

def parse
  doc = TrafikantenTravel::Utils.fetch(BASE_URL + query_string)
  
  case doc
  when NOTFOUND
    return {}
  when /Trafikanten - Feilmelding/
    doc =~ /<p>(.+)<\/p>/
    raise Error.new($1)
  when /Microsoft VBScript runtime/
    raise BadRequest
  end
  
  do_parse(doc)
end