Class: Krane::DurationParser

Inherits:
Object
  • Object
show all
Defined in:
lib/krane/duration_parser.rb

Overview

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.

Defined Under Namespace

Classes: ParsingError

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ DurationParser

Returns a new instance of DurationParser.



13
14
15
# File 'lib/krane/duration_parser.rb', line 13

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

Instance Method Details

#parse!Object



17
18
19
20
21
22
23
24
25
# File 'lib/krane/duration_parser.rb', line 17

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