class Gares::Train

Represents a train from www.sncf.com/fr/horaires-info-trafic/train

Public Class Methods

new(*arguments) click to toggle source

Initialize a new Train object with at least it's number and departure date

train = Gares::Train.new(num: 6704, date: Time.parse('2015-04-15'))

Gares::Train objects are lazy loaded, meaning that no HTTP request will be performed when a new object is created. An HTTP request is made Only when you use an accessor that needs remote data.

Calls superclass method
# File lib/gares/train.rb, line 27
def initialize(*arguments)
  attributes = arguments.first

  fail "Please provide a train number" unless attributes[:num].is_a?(Integer)
  fail "Please provide a departure date" unless attributes[:date].is_a?(Time)

  if attributes[:origdest]
    @origdest = attributes[:orig].nil? ? :orig : :dest
    attributes[@origdest] = Gares::Station.search(attributes[:origdest]).first
    attributes.delete(:origdest)
  end

  super(*arguments)
end

Private Class Methods

request_sncf(number, date) click to toggle source
# File lib/gares/train.rb, line 104
def self.request_sncf(number, date)
  uri = URI.parse("http://www.sncf.com/sncf/train")
  response = Net::HTTP.post_form(uri, {"numeroTrain" => number, "date" => date.strftime("%d/%m/%Y")})
  @cookies = response.get_fields('Set-Cookie').map { |cookie| cookie.split(";").first }.join(";")

  uri = URI.parse("http://www.sncf.com/en/horaires-info-trafic/train/resultats")
  req = Net::HTTP::Get.new(uri.path)
  req.add_field("Cookie", @cookies)
  Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) }.body
end
request_sncf_itinerary(index) click to toggle source
# File lib/gares/train.rb, line 115
def self.request_sncf_itinerary(index)
  uri = URI.parse("http://www.sncf.com/sncf/train/displayDetailTrain?idItineraire=#{index}")
  req = Net::HTTP::Get.new(uri)
  req.add_field("Cookie", @cookies) if @cookies
  Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) }.body

  uri = URI.parse("http://www.sncf.com/en/horaires-info-trafic/train/resultats?#{index}")
  req = Net::HTTP::Get.new(uri)
  req.add_field("Cookie", @cookies) if @cookies
  Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) }.body
end

Public Instance Methods

arrival() click to toggle source

@return [Station] The arrival station of the train

# File lib/gares/train.rb, line 62
def arrival
  if dest
    dest
  else
    @arrival ||= TrainStop.new(document.at('tr.itinerary-end'), date)
  end
end
delayed?() click to toggle source
# File lib/gares/train.rb, line 76
def delayed?
  retard || ([departure] + stops + [arrival]).any?(&:delayed?)
end
departure() click to toggle source

@return [Station] The departure station of the train

# File lib/gares/train.rb, line 48
def departure
  if orig
    orig
  else
    @departure ||= TrainStop.new(document.at('tr.itinerary-start'), date)
  end
end
number() click to toggle source

@return [Integer] The train number

# File lib/gares/train.rb, line 43
def number
  num
end
origdest() click to toggle source

@deprecated

# File lib/gares/train.rb, line 71
def origdest
  warn("[DEPRECATED] Warning: This method is deprecated, please use `orig` or `dest` instead of `origdest`")
  send(@origdest)
end
platform() click to toggle source
# File lib/gares/train.rb, line 80
def platform
  voie
end
stops() click to toggle source

@return [Array<TrainStop>] A list of all stops between departure and arrival stations.

# File lib/gares/train.rb, line 57
def stops
  @stops ||= document.css('tr.itinerary-stop').map { |stop| TrainStop.new(stop, date) }
end

Private Instance Methods

document() click to toggle source

Returns a new Nokogiri document for parsing.

# File lib/gares/train.rb, line 91
def document
  if !@document
    @document = Nokogiri::HTML(self.class.request_sncf(number, date))
    if !itinerary_available?
      @document = Nokogiri::HTML(self.class.request_sncf_itinerary(0))
    end
  end
  if @document.at('#no_results')
    fail TrainNotFound, @document.at('#no_results b').inner_html
  end
  @document
end
itinerary_available?() click to toggle source
# File lib/gares/train.rb, line 86
def itinerary_available?
  document.css('ul.itinerary-details').size > 0
end