Sha256: 930c24728a2a3511df988a3ae76ba65c9e6b2b0934435bbf02e7cf620916b6f0

Contents?: true

Size: 957 Bytes

Versions: 3

Compression:

Stored size: 957 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

3 entries across 3 versions & 1 rubygems

Version Path
quakelive_api-0.1.2 lib/quakelive_api/game_time.rb
quakelive_api-0.1.1 lib/quakelive_api/game_time.rb
quakelive_api-0.1.0 lib/quakelive_api/game_time.rb