class Map::Tube::Parser

Public Class Methods

new(doc) click to toggle source
# File lib/map/tube/parser.rb, line 4
def initialize(doc)
  @document = Nokogiri::XML(doc)
  @graph = Graph.new
end

Public Instance Methods

parse!() click to toggle source
# File lib/map/tube/parser.rb, line 9
def parse!
  parse_lines!
  parse_stations!
  @graph
end

Private Instance Methods

parse_line(line) click to toggle source
# File lib/map/tube/parser.rb, line 41
def parse_line(line)
  if line.include?(":")
    line[/(.*):/, 1]
  else
    line
  end
end
parse_lines!() click to toggle source
# File lib/map/tube/parser.rb, line 33
def parse_lines!
  lines = @document.search("//lines/line")
  lines.each do |line|
    element = Line.new(line["id"], line["name"], line["color"])
    @graph.add_line(element)
  end
end
parse_stations!() click to toggle source
# File lib/map/tube/parser.rb, line 17
def parse_stations!
  stations = @document.search("//stations/station")
  stations.each do |station|
    current_station = Station.new(
      station["id"],
      station["name"],
      parse_line(station["line"]))

    links = station["link"].split(",")
    links += compute_other_links(station["other_link"]) if station["other_link"]
    links.each { |link| current_station.add_link(link) }

    @graph.add_station(current_station)
  end
end