Sha256: af7b9e162422c50212803672d9dd1c99bad43edbf704a29f76e20d5fedb84323

Contents?: true

Size: 967 Bytes

Versions: 1

Compression:

Stored size: 967 Bytes

Contents

module QuakeliveApi
  class GameTime
    attr_accessor :ranked, :unranked

    class Interval < Struct.new(:seconds, :minutes, :hours, :days)
      def total
        members.sum { |m| send(m).send(m) } # see what I did there?
      end
    end

    # accepts unparsed string directly from QL profile, for example:
    # * Ranked Time: 21:50 Unranked Time: 04:54
    # * Ranked Time: 50.06:18:30 Unranked Time: 02:31:02
    def initialize(unparsed_string)
      matches = unparsed_string.match(/Ranked Time: ([\d:.]+) Unranked Time: ([\d:.]+)/)
      return unless matches

      @ranked   = reverse_match(matches[1])
      @unranked = reverse_match(matches[2])
    end

    def ==(other)
      return ranked == other.ranked && unranked == other.unranked
    end

    private

      def reverse_match(string)
        attrs = string.split(/\.|:/).reverse.map { |a| a.to_i }
        (4 - attrs.size).times { attrs << 0 }
        Interval.new(*attrs)
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
quakelive_api-0.0.1 lib/quakelive_api/game_time.rb