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?'

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.



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

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



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

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 error-checking.



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

def parse
  doc = TrafikantenTravel::Utils.fetch(BASE_URL + query_string)
  
  if doc =~ /Ingen forbindelse funnet eller ingen stoppesteder funnet/
    return {}
  end
  
  if doc =~ /Trafikanten - Feilmelding/
    doc =~ /<p>(.+)<\/p>/
    raise Error.new($1)
  end
  
  if doc =~ /Microsoft VBScript runtime/
    raise BadRequest
  end
  
  do_parse(doc)
end