Sha256: d2088176efa69f2851ee32ff85b1e7eb927b87cfa21a6f6e359b0fbf749491b7

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

class TokyoMetro::Api::TrainLocation::Info::Delay

  def initialize( delay )
    @delay = delay.try( :floor )
  end

  attr_reader :delay

  def displayed_in_train_operation_info?
    @delay.present? and @delay > 0
  end

  def sec
    @delay
  end

  def min
    ( @delay * 1.0 / 60 ).ceil
  end

  def decorate( request )
    ::TokyoMetro::Factory::Decorate::Api::TrainLocation::Info::Delay.new( request , self )
  end

  def hour_part
    @delay / 3600
  end

  def min_part( add_hour_part: false )
    _min_part = @delay / 60
    if add_hour_part
      _min_part % 60
    else
      _min_part
    end
  end

  def sec_part
    @delay % 60
  end

  def to_h( add_hour_part: false )
    if add_hour_part
      { hour: hour_part , min: min_part( add_hour_part: true ) , sec: sec_part }
    else
      { min: min_part , sec: sec_part }
    end
  end

  def to_s_separated_by_comma( add_hour_part: false )
    ary = ::Array.new
    if add_hour_part
      ary << hour_part
    end
    ary << sprintf( "%02d" , min_part( add_hour_part: add_hour_part ) )
    ary << sprintf( "%02d" , sec_part )
    ary.map( &:to_s ).join( ":" )
  end

  def to_s_ja( add_hour_part: false )
    ary = ::Array.new
    if add_hour_part
      ary << hour_part
      ary << "時間"
    end
    ary << min_part( add_hour_part: add_hour_part )
    ary << "分"
    ary << sec_part
    ary << "秒"
    ary.map( &:to_s ).join
  end

  def to_s_en( add_hour_part: false )
    ary = ::Array.new
    if add_hour_part
      ary << hour_part
      ary << "hour"
    end
    ary << min_part( add_hour_part: add_hour_part )
    ary << "min"
    ary << sec_part
    ary << "sec"
    ary.map( &:to_s ).join( " " )
  end

  def on_schedule?
    has_valid_delay? and @delay < 30
  end

  def nearly_on_schedule?
    has_valid_delay? and @delay >= 30 and @delay < 180
  end

  def delayed?
    has_valid_delay? and @delay >= 180
  end

  private

  def has_valid_delay?
    @delay.present?
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tokyo_metro-0.10.1 lib/tokyo_metro/api/train_location/info/delay.rb