class TokyoMetro::Api::StationTimetable::Info::TrainTime::Info
個別の列車の情報を扱うクラス
Attributes
@return [String] 行先 - odpt:Station
@return [Integer] 車両数 - xsd:integer @note 駅に停車する車両数が列車毎に異なる場合に格納される。
@return [Integer] 車両数 - xsd:integer @note 駅に停車する車両数が列車毎に異なる場合に格納される。
@return [DateTime] 列車の出発時間(ISO8601時刻形式) - odpt:Time @example
05:09
@return [Boolean] 最終電車か否かの真偽値(最終電車の場合 true) - xsd:boolean
@return [Boolean] 始発駅か否かの真偽値(始発駅の場合 true) - xsd:boolean
@return [Boolean] 最終電車か否かの真偽値(最終電車の場合 true) - xsd:boolean
@return [String] その他の注釈(接続、通過待ちなど) - xsd:string
@return [Boolean] 始発駅か否かの真偽値(始発駅の場合 true) - xsd:boolean
@return [String] 行先 - odpt:Station
@return [String] 列車種別 - odpt:TrainType @example
各停 (odpt.TrainType:Local) 、急行 (odpt.TrainType:Express) 、快速 (odpt.TrainType:Rapid) 、特急 (odpt.TrainType:LimitedExpress) など
Public Class Methods
深夜(日付変更後)の列車か否かの判定 @param h [DateTime] 時刻の設定(「時」を指定する) @return [Boolean]
# File lib/tokyo_metro/api/station_timetable/info/train_time/info.rb, line 248 def self.depart_in_midnight?(h) h < ::TokyoMetro::DATE_CHANGING_HOUR end
# File lib/tokyo_metro/api/station_timetable/info/train_time/info.rb, line 239 def self.factory_for_seeding_this_class factory_for_seeding_station_train_time end
@!group クラスメソッド (2) - データの取得・保存
# File lib/tokyo_metro/api/station_timetable/info/train_time/info.rb, line 235 def self.factory_for_this_class factory_for_generating_train_from_hash end
Constructor
# File lib/tokyo_metro/api/station_timetable/info/train_time/info.rb, line 21 def initialize( departure_time , terminal_station , train_type , is_last , is_origin , car_composition , notes ) @departure_time = departure_time @terminal_station = terminal_station @train_type = train_type @is_last = is_last @is_origin = is_origin @car_composition = car_composition @notes = notes @seed_completed = false end
Public Instance Methods
# File lib/tokyo_metro/api/station_timetable/info/train_time/info.rb, line 34 def ==( other ) [ :departure_time , :terminal_station , :train_type , :is_last , :is_origin , :car_composition , :notes ].all?{ | method | self.send( method ) == other.send( method ) } end
指定した時刻より後に出発するか否かの判定 @param time [DateTime] 時刻の設定(デフォルトは現在時刻) @return [Boolean]
# File lib/tokyo_metro/api/station_timetable/info/train_time/info.rb, line 155 def after_now?( time = ::TokyoMetro.time_now ) self.departure_datetime( time ) <= time end
指定した時刻より前に出発するか否かの判定 @param time [DateTime] 時刻の設定(デフォルトは現在時刻) @return [Boolean]
# File lib/tokyo_metro/api/station_timetable/info/train_time/info.rb, line 148 def before_now?( time = ::TokyoMetro.time_now ) self.departure_datetime( time ) > time end
列車の出発時刻を Time のインスタンスとして取得するメソッド @param time_now [DateTime] 現在の時刻(日付の情報を使用するために必要) @param change_day_of_midnight_train [Boolean] false の場合は日付の変更を行わない。(default は true だが、::TokyoMetro::Api::StationTimetable::Info::TrainTime::List#last_train_not_departed_yet? では false に設定している) @return [DateTime] @note 時刻の比較を簡単にする。
# File lib/tokyo_metro/api/station_timetable/info/train_time/info.rb, line 100 def departure_datetime( time_now = ::TokyoMetro.time_now , change_day_of_midnight_train: true ) train_hour , train_min = self.departure_time_array time = DateTime.new( time_now.year , time_now.month , time_now.day , train_hour , train_min , 0 , Rational( 9 , 24 ) ) unless change_day_of_midnight_train return time end # 【注意】change_day_of_midnight_train が true の場合 # 以下の処理は、この列車が含まれる時刻表が # TokyoMetro::Api::StationTimetable#today_s_timetable などによって、 # time_now (通常は現在時刻)の時点で適用される時刻表として選択されていることを前提にしている。 # 日付が変わった直後の深夜の列車か否か midnight_train = self.class.depart_in_midnight?( train_hour ) #-------- 【以下、書きかけ】 # 現在は深夜か unless midnight_train return time end # 出発時刻が 0:00 - 2:59 で、現在時刻が3時を過ぎている場合 if midnight_train and time_now.hour >= 3 # 列車の発車の日付は「現在の日付【が】変わった後」の0時~2時となるため、日付を1日進める。 time.next_day # 出発時刻が 3:00 - 22:59 で、現在時刻が3時より前の場合 # 時刻表に掲載されている時間が2時より後(3時~23時)かつ # 現在が0時~2時の場合 elsif train_hour > 2 and time_now.hour <= 2 time.prev_day elsif train_hour <= 2 and time_now.hour <= 2 time else # train_hour > 2 and time_now.hour > 2 time end end
列車の出発時刻を配列として取得するメソッド @return [::Array <Integer>] @note 「hh:mm」の形(hは時間、mは分)の文字列を、時間と分(それぞれ整数)の配列に変換するメソッド
# File lib/tokyo_metro/api/station_timetable/info/train_time/info.rb, line 91 def departure_time_array [ @departure_time.hour , @departure_time.min ] end
# File lib/tokyo_metro/api/station_timetable/info/train_time/info.rb, line 76 def platform_number info = @notes.find { |i| i.instance_of?( ::TokyoMetro::Api::StationTimetable::Info::TrainTime::Info::Note::PlatformNumber ) } if info.nil? nil else info.number end end
ある駅に停車するか否かを判定するメソッド @param stations [String] 判定する駅名 @return [Boolean] @note 複数指定した場合は、指定された【すべて】の駅に停車する列車を取得する。
# File lib/tokyo_metro/api/station_timetable/info/train_time/info.rb, line 168 def stops_at?( *stations ) if stations == [ nil ] true elsif @stops.nil? true else stations.all? { | station | ( station.string? and @stops.include?( station ) ) # or ( station.instance_of?( Station ) and @stops.include?( station ) ) } end end
インスタンスの情報をハッシュにして返すメソッド @return [Hash]
# File lib/tokyo_metro/api/station_timetable/info/train_time/info.rb, line 187 def to_h h = ::Hash.new set_data_to_hash( h , "odpt:departureTime" , @departure_time.strftime( "%H:%M" ) ) set_data_to_hash( h , "odpt:destinationStation" , @terminal_station ) set_data_to_hash( h , "odpt:trainType" , @train_type ) set_data_to_hash( h , "odpt:isLast" , @is_last ) set_data_to_hash( h , "odpt:isOrigin" , @is_origin ) set_data_to_hash( h , "odpt:carComposition" , @car_composition ) set_data_to_hash( h , "odpt:notes" , @notes ) h end
インスタンスの情報を整形した文字列にして返すメソッド @param indent [Integer (>=0)] インデントの幅 @return [String]
# File lib/tokyo_metro/api/station_timetable/info/train_time/info.rb, line 204 def to_s( indent = 0 ) h = self.to_h str_ary = ::Array.new str_ary << ( "出発:" + h[ "odpt:departureTime" ] ) str_ary << ( "行先:" + h[ "odpt:destinationStation" ] ) str_ary << ( "種別:" + h[ "odpt:trainType" ] ) str_note_ary = ::Array.new if h[ "odpt:isLast" ] str_note_ary << "【最終】" end if h[ "odpt:isOrigin" ] str_note_ary << "【当駅始発】" end if h[ "odpt:carComposition" ].integer? str_note_ary << "[#{h[ "odpt:carComposition" ]}両編成]" end str_ary << " " * 3 + str_note_ary.join(" ") if h[ "odpt:note" ].present? str_ary << " " * 3 + h[ "odpt:note" ] end str_ary.map { | str | " " * indent + str }.join( "\n" ) end