class Gares::Base

Represents a station on gares-sncf.com

Constants

GARES_SNCF
OPEN_DATA_SNCF

Private Class Methods

external_gares_sncf(tvs, direction = :departure, refresh = false) click to toggle source
# File lib/gares/base.rb, line 86
def self.external_gares_sncf(tvs, direction = :departure, refresh = false)
  @gares_sncf ||= {}
  @gares_sncf[tvs] ||= {}
  if refresh || @gares_sncf.nil? || @gares_sncf[direction].nil?
    @gares_sncf[tvs][direction] = JSON.parse(open(GARES_SNCF % [direction, tvs]).read, :symbolize_names => true)[:trains]
  end
  @gares_sncf[tvs][direction]
end
find_by_sncf_id(query) click to toggle source

Convenience method for search_by_sncf_id

# File lib/gares/base.rb, line 101
def self.find_by_sncf_id(query)
  Gares::Search.new(query, :sncf_id).stations.first
end
open_data_sncf(uic8_sncf, field) click to toggle source
# File lib/gares/base.rb, line 78
def self.open_data_sncf(uic8_sncf, field)
  @open_data ||= {}
  @open_data[uic8_sncf] ||= JSON.parse(open(OPEN_DATA_SNCF % ("%010d" % uic8_sncf).to_s).read, :symbolize_names => true)[:records]
  unless @open_data[uic8_sncf].empty?
    @open_data[uic8_sncf].first[:fields][field]
  end
end

Public Instance Methods

arrivals(refresh = false) click to toggle source

List of the next arriving trains in this station. @param refresh [Boolean] whether to fetch fresh data from gares-sncf.com or not. @return [Array<Train>]

# File lib/gares/base.rb, line 46
def arrivals(refresh = false)
  if tvs
    trains(:arrival, refresh)
  end
end
Also aliased as: arriving_trains
arriving_trains(refresh = false)
Alias for: arrivals
departing_trains(refresh = false)
Alias for: departures
departures(refresh = false) click to toggle source

List of the next departing trains from this station. @param refresh [Boolean] whether to fetch fresh data from gares-sncf.com or not. @return [Array<Train>]

# File lib/gares/base.rb, line 36
def departures(refresh = false)
  if tvs
    trains(:departure, refresh)
  end
end
Also aliased as: departing_trains
has_borne?() click to toggle source

Whether this station has a “borne” (yellow self-service ticket machine) @return [Boolean]

# File lib/gares/base.rb, line 11
def has_borne?
  sncf_self_service_machine == "t"
end
lat() click to toggle source

@deprecated

# File lib/gares/base.rb, line 22
def lat
  warn "[DEPRECATION] favor the 'latitude' method instead of 'lat'."
  latitude
end
long() click to toggle source

@deprecated

# File lib/gares/base.rb, line 28
def long
  warn "[DEPRECATION] favor the 'longitude' method instead of 'long'."
  longitude
end
slug() click to toggle source

@deprecated

# File lib/gares/base.rb, line 16
def slug
  warn "[DEPRECATION] favor the 'sncf_id' method instead of 'slug'."
  sncf_id.downcase
end

Private Instance Methods

trains(direction = :departure, refresh = false) click to toggle source
# File lib/gares/base.rb, line 62
def trains(direction = :departure, refresh = false)
  variable = "@#{direction}".to_sym
  if tvs && (refresh || instance_variable_get(variable).nil?)
    raw_trains = self.class.external_gares_sncf(tvs, direction, refresh)
    all_trains = raw_trains.map do |raw_train|
      raw_train[:num] = raw_train[:num].to_i
      raw_train[:date] = Time.now
      key = direction == :departure ? :orig : :dest
      raw_train[key] = self
      Gares::Train.new(raw_train)
    end
    instance_variable_set(variable, all_trains)
  end
  instance_variable_get(variable)
end
tvs() click to toggle source
# File lib/gares/base.rb, line 55
def tvs
  if uic8_sncf
    @raw_tvs ||= self.class.open_data_sncf(uic8_sncf, :tvs)
    @raw_tvs.split(" ").first if @raw_tvs
  end
end