Class: TrafikantenTravel::Station

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

Constant Summary collapse

BASE_URL =
'http://www5.trafikanten.no/txml/?type=1&stopname=%s'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Station

Returns a new instance of Station.



7
8
9
10
11
# File 'lib/trafikanten_travel/station.rb', line 7

def initialize(attrs = {})
  attrs.each do |k,v|
    self.__send__("#{k}=", v)
  end
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/trafikanten_travel/station.rb', line 5

def id
  @id
end

#latObject

Returns the value of attribute lat.



5
6
7
# File 'lib/trafikanten_travel/station.rb', line 5

def lat
  @lat
end

#lngObject

Returns the value of attribute lng.



5
6
7
# File 'lib/trafikanten_travel/station.rb', line 5

def lng
  @lng
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/trafikanten_travel/station.rb', line 5

def name
  @name
end

#typeObject

Internal type @ Trafikanten. Used with GET requests for routes in depType and arrType. We guess these unless set.



41
42
43
# File 'lib/trafikanten_travel/station.rb', line 41

def type
  @type
end

Class Method Details

.find_by_name(name) ⇒ Object

Query the Travel XML API @ trafikanten.no for Stations. This will only include actual stations, not regions (type 2) We’ll have to parse m.trafikanten.no to receive those, but that will not give us coordinates for the actual stations. See git history for an implementation that did this.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/trafikanten_travel/station.rb', line 18

def self.find_by_name(name)
  raw = open(BASE_URL % CGI.escape(name))
  doc = Nokogiri::XML.parse raw
  hits = doc.css('StopMatch').inject([]) do |ary, stop|
    
    x_coord = stop.css('XCoordinate').text
    y_coord = stop.css('YCoordinate').text
    
    if x_coord != '0' && y_coord != '0'
      lat_lng = GeoUtm::UTM.new('32V', x_coord.to_i, y_coord.to_i).to_lat_lon
    end

    ary << Station.new({
      :id => stop.css('fromid').text, 
      :name => stop.css('StopName').text,
      :lat => lat_lng ? lat_lng.lat.to_s : nil,
      :lng => lat_lng ? lat_lng.lon.to_s : nil
    })
  end
end