Sha256: 9256b4eef7bc6ed4a328c72c4910ef56c21031108a7defea2983979dc357425b

Contents?: true

Size: 933 Bytes

Versions: 58

Compression:

Stored size: 933 Bytes

Contents

# frozen_string_literal: true

require 'active_support/duration'

module Krane
  # This class is a less strict extension of ActiveSupport::Duration::ISO8601Parser.
  # In addition to full ISO8601 durations, it can parse unprefixed ISO8601 time components (e.g. '1H').
  # It is also case-insensitive.
  # For example, this class considers the values "1H", "1h" and "PT1H" to be valid and equivalent.
  class DurationParser
    class ParsingError < ArgumentError; end

    def initialize(value)
      @iso8601_str = value.to_s.strip.upcase
    end

    def parse!
      ActiveSupport::Duration.parse("PT#{@iso8601_str}") # By default assume it is just a time component
    rescue ActiveSupport::Duration::ISO8601Parser::ParsingError
      begin
        ActiveSupport::Duration.parse(@iso8601_str)
      rescue ActiveSupport::Duration::ISO8601Parser::ParsingError => e
        raise ParsingError, e.message
      end
    end
  end
end

Version data entries

58 entries across 58 versions & 3 rubygems

Version Path
krane-3.7.0 lib/krane/duration_parser.rb
krane-3.6.2 lib/krane/duration_parser.rb
krane-3.6.1 lib/krane/duration_parser.rb
krane-3.5.2 lib/krane/duration_parser.rb
krane-3.5.1 lib/krane/duration_parser.rb
krane-3.5.0 lib/krane/duration_parser.rb
krane-3.4.2 lib/krane/duration_parser.rb
krane-3.4.1 lib/krane/duration_parser.rb
krane-3.4.0 lib/krane/duration_parser.rb
krane-3.3.0 lib/krane/duration_parser.rb
krane-3.2.0 lib/krane/duration_parser.rb
krane-3.1.0 lib/krane/duration_parser.rb
krane-3.0.1 lib/krane/duration_parser.rb
krane-3.0.0 lib/krane/duration_parser.rb
krane-2.4.9 lib/krane/duration_parser.rb
krane-2.4.8 lib/krane/duration_parser.rb
krane-2.4.7 lib/krane/duration_parser.rb
krane-2.4.6 lib/krane/duration_parser.rb
krane-2.4.5 lib/krane/duration_parser.rb
krane-2.4.4 lib/krane/duration_parser.rb