Class: NationalRail::JourneyPlanner::DetailsPageParser
- Inherits:
-
Object
- Object
- NationalRail::JourneyPlanner::DetailsPageParser
- Defined in:
- lib/national-rail/journey_planner/details_page_parser.rb
Instance Method Summary collapse
-
#initialize(doc, date) ⇒ DetailsPageParser
constructor
A new instance of DetailsPageParser.
- #parse ⇒ Object
Constructor Details
#initialize(doc, date) ⇒ DetailsPageParser
Returns a new instance of DetailsPageParser.
5 6 7 |
# File 'lib/national-rail/journey_planner/details_page_parser.rb', line 5 def initialize(doc, date) @doc, @date = doc, date end |
Instance Method Details
#parse ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/national-rail/journey_planner/details_page_parser.rb', line 9 def parse details = {} description_divs = (@doc/"table#journeyLegDetails tbody tr.lastRow td[@colspan='6'] div") return details unless description_divs.any? description = description_divs.last.inner_text.gsub(/\s+/, " ").strip company_matches = /(.*) service from .* to .*/.match(description) return details unless company_matches details[:company] = company_matches[1].strip origins, destinations = (/.* service from (.*) to (.*)/.match(description)[1,2]).map{ |s| s.split(",").map(&:strip) } details[:origins], details[:destinations] = origins, destinations parser = TimeParser.new(@date) origin_code = (@doc/"td.origin abbr").inner_html.strip departs_at = (@doc/"td.leaving").inner_html.strip details[:initial_stop] = { :station_code => origin_code, :departs_at => parser.time(departs_at) } details[:stops] = [] (@doc/".callingpoints table > tbody > tr").each do |tr| if (tr/".calling-points").length > 0 station_code = (tr/".calling-points > a > abbr").inner_html.strip arrives_at = (tr/".arrives").inner_html.strip departs_at = (tr/".departs").inner_html.strip departs_at = arrives_at if arrives_at.present? && departs_at.blank? arrives_at = departs_at if arrives_at.blank? && departs_at.present? details[:stops] << { :station_code => station_code, :arrives_at => parser.time(arrives_at), :departs_at => parser.time(departs_at) } end end destination_code = (@doc/"td.destination abbr").inner_html.strip arrives_at = (@doc/"td.arriving").inner_html.strip details[:final_stop] = { :station_code => destination_code, :arrives_at => parser.time(arrives_at) } details end |