Class: NOTAM::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/notam/item.rb

Overview

Items are the building blocks of a NOTAM message. They usually consist of only one line of plain text each, however, D and E items may span over multiple lines of plain text.

Direct Known Subclasses

A, B, C, D, E, F, Footer, G, Header, Q

Constant Summary collapse

RE =
/[QA-G]\)\s/.freeze
ID_RE =
/(?<id_series>[A-Z])(?<id_number>\d{4})\/(?<id_year>\d{2})/.freeze
ICAO_RE =
/[A-Z]{4}/.freeze
TIME_RE =
/(?:\d{2})(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])(?:[01]\d|[2][0-4])(?:[0-5]\d)/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, data: {}) ⇒ NOTAM::Header, ...

Note:

Some NOTAM items (most notably D) depend on previous items for meaningful parsing and may fail if this information is not made available by passing the NOTAM message payload parsed so far as data.

Analyses the raw NOTAM item text and initialize the corresponding item object.

Examples:

NOTAM::Item.new('A0135/20 NOTAMN')   # => #<NOTAM::Head id="A0135/20">
NOTAM::Item.new('B) 0208231540')     # => #<NOTAM::B>
NOTAM::Item.new('foobar')            # => NOTAM::ParseError

Parameters:

  • text (String)
  • data (Hash) (defaults to: {})


47
48
49
# File 'lib/notam/item.rb', line 47

def initialize(text, data: {})
  @text, @data = text.strip, data
end

Instance Attribute Details

#capturesMatchData (readonly)

Captures from the default item regexp RE

Returns:

  • (MatchData)


24
25
26
# File 'lib/notam/item.rb', line 24

def captures
  @captures
end

#dataHash (readonly)

Parsed NOTAM message payload

Returns:

  • (Hash)


29
30
31
# File 'lib/notam/item.rb', line 29

def data
  @data
end

#textString (readonly)

Raw NOTAM item text

Returns:

  • (String)


19
20
21
# File 'lib/notam/item.rb', line 19

def text
  @text
end

Class Method Details

.type(text) ⇒ String

Analyses the raw NOTAM item text and detect its type.

Examples:

NOTAM::Item.type('A0135/20 NOTAMN')   # => :Header
NOTAM::Item.type('B) 0208231540')     # => :B
NOTAM::Item.type('SOURCE: LFNT')      # => :Footer
NOTAM::Item.type('foobar')            # => NOTAM::ParseError

Returns:

  • (String)

Raises:



70
71
72
73
74
75
76
77
# File 'lib/notam/item.rb', line 70

def type(text)
  case text.strip
    when /\A([A-GQ])\)/ then $1
    when NOTAM::Header::RE then 'Header'
    when NOTAM::Footer::RE then 'Footer'
    else fail(NOTAM::ParseError, 'item not recognized')
  end.to_sym
end

Instance Method Details

#fail!(message = nil) ⇒ Object

Raise ParseError along with some debugging information.

Parameters:

  • message (String) (defaults to: nil)

    optional error message

Raises:



130
131
132
# File 'lib/notam/item.rb', line 130

def fail!(message=nil)
  fail ParseError.new([message, text].compact.join(': '), item: self)
end

#inspectString

Returns:

  • (String)


135
136
137
# File 'lib/notam/item.rb', line 135

def inspect
  %Q(#<#{self.class} "#{truncated_text}">)
end

#merge(*methods) ⇒ self

This method is abstract.
Note:

Must be extended in subclasses.

Merges the return values of the given methods into the data hash.

Returns:

  • (self)


112
113
114
115
116
117
# File 'lib/notam/item.rb', line 112

def merge(*methods)
  fail 'nothing to merge' unless methods.any?
  methods.each { @data[_1] = send(_1) }
  @data.compact!
  self
end

#parseself

This method is abstract.
Note:

May be extended or overwritten in subclasses, but must always return self!

Matches the raw NOTAM item text against RE and populates #captures.

Examples:

NOTAM::Item.new('A0135/20 NOTAMN').parse   # => #<NOTAM::Header id="A0135/20">
NOTAM::Item.new('foobar').parse            # => NOTAM::ParseError

Returns:

  • (self)

Raises:



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/notam/item.rb', line 92

def parse
  if match_data = self.class::RE.match(text)
    begin
      @captures = match_data.named_captures
      self
    rescue
      fail! "invalid #{self.class.to_s.split('::').last} item"
    end
  else
    fail! 'text does not match regexp'
  end
end

#typeSymbol

Type of the item

Returns:

  • (Symbol)

    either :Header, :Q, (:A..:G) or :Footer



122
123
124
# File 'lib/notam/item.rb', line 122

def type
  self.class.to_s[7..].to_sym
end