Module: RCAP

Defined in:
lib/rcap/alert.rb,
lib/rcap/config.rb,
lib/rcap/version.rb,
lib/rcap/base/area.rb,
lib/rcap/utilities.rb,
lib/rcap/base/info.rb,
lib/rcap/base/alert.rb,
lib/rcap/base/point.rb,
lib/rcap/base/circle.rb,
lib/rcap/cap_1_1/area.rb,
lib/rcap/base/geocode.rb,
lib/rcap/cap_1_0/info.rb,
lib/rcap/cap_1_2/area.rb,
lib/rcap/cap_1_2/info.rb,
lib/rcap/base/polygon.rb,
lib/rcap/cap_1_1/info.rb,
lib/rcap/cap_1_0/area.rb,
lib/rcap/cap_1_2/alert.rb,
lib/rcap/cap_1_2/point.rb,
lib/rcap/cap_1_1/alert.rb,
lib/rcap/cap_1_1/point.rb,
lib/rcap/cap_1_0/point.rb,
lib/rcap/cap_1_0/alert.rb,
lib/rcap/base/resource.rb,
lib/rcap/base/parameter.rb,
lib/rcap/cap_1_1/circle.rb,
lib/rcap/cap_1_0/circle.rb,
lib/rcap/cap_1_2/circle.rb,
lib/rcap/cap_1_1/geocode.rb,
lib/rcap/cap_1_1/polygon.rb,
lib/rcap/cap_1_0/polygon.rb,
lib/rcap/base/event_code.rb,
lib/rcap/cap_1_2/polygon.rb,
lib/rcap/cap_1_2/geocode.rb,
lib/rcap/cap_1_0/geocode.rb,
lib/rcap/cap_1_2/resource.rb,
lib/rcap/cap_1_1/resource.rb,
lib/rcap/cap_1_0/resource.rb,
lib/rcap/cap_1_2/parameter.rb,
lib/rcap/cap_1_1/parameter.rb,
lib/rcap/cap_1_0/parameter.rb,
lib/rcap/cap_1_1/event_code.rb,
lib/rcap/cap_1_0/event_code.rb,
lib/rcap/cap_1_2/event_code.rb

Defined Under Namespace

Modules: Alert, Base, CAP_1_0, CAP_1_1, CAP_1_2

Constant Summary

XML_PRETTY_PRINTER =
REXML::Formatters::Pretty.new( 2 )
RCAP_TIME_FORMAT =
"%Y-%m-%dT%H:%M:%S"
RCAP_ZONE_FORMAT =
"%+03i:00"
VERSION =
'2.2.0'

Class Method Summary (collapse)

Class Method Details

+ (Hash) attribute_values_to_hash(*attribute_values)

Converts an array of key value pairs into a hash, excluding any value that is nil.

Examples:

RCAP.attribute_values_to_hash( ['a', 1], ['b' , nil ]) # => { 'a' => 1 }

Parameters:

  • attribute_values (Array<Array(Object,Object)>)

    An array of arrays of key/value pairs

Returns:

  • (Hash)

    Hash of attributes



75
76
77
# File 'lib/rcap/utilities.rb', line 75

def self.attribute_values_to_hash( *attribute_values )
  Hash[ *attribute_values.reject{ |key, value| value.nil? }.flatten( 1 )]
end

+ (String) format_lines_for_inspect(header, inspect_string)

Formats output for inspect

Examples:

RCAP.format_lines_for_inspect( 'Test', 'one\ntwo\nthree' )
# returns
# .-------.
# | Test  |
# |-------|
# | one   |
# | two   |
# | three |
# '-------'

Parameters:

  • header (String)

    Output header

  • inspect_string (String)

    String to be output

Returns:

  • (String)

    Formatted output for inspect



59
60
61
62
63
64
65
66
# File 'lib/rcap/utilities.rb', line 59

def self.format_lines_for_inspect( header, inspect_string )
  max_line_length = inspect_string.lines.map{ |line| line.strip.length }.max
  "\n." + '-' * (max_line_length + 2) + ".\n"+
    '| ' + header.ljust( max_line_length ) + " |\n"+
    '|' + '-' * ( max_line_length + 2 ) + "|\n"+
    inspect_string.lines.map{ |line| '| ' + line.strip.ljust( max_line_length ) +' |'}.join( "\n" ) + "\n" +
    "'" + '-' * ( max_line_length + 2 ) + "'\n"
end

+ (String) generate_identifier

Returns a randomly generated UUID string

Returns:



7
8
9
# File 'lib/rcap/utilities.rb', line 7

def self.generate_identifier
  UUIDTools::UUID.random_create.to_s
end

+ (String?) parse_datetime(date)

If the parameter is a string and not empty the datetime is parsed out of it, otherwise returns nil.

Parameters:

  • date (String)

    String to parse

Returns:



97
98
99
100
101
102
103
104
105
106
# File 'lib/rcap/utilities.rb', line 97

def self.parse_datetime( date )
  case date
  when String
    if  !date.empty?
      DateTime.parse( date.strip )
    end
  when DateTime, Time, Date
    date.to_datetime
  end
end

+ (String?) strip_if_given(string)

If the string given is not nil, String#strip is called otherwise nil is returned

Parameters:

Returns:



125
126
127
128
129
# File 'lib/rcap/utilities.rb', line 125

def RCAP.strip_if_given( string )
  if string
    string.strip
  end
end

+ (Float?) to_f_if_given(number)

if the string is given, String#strip and then String#to_f are applied otherwise nil is returned.

Parameters:

Returns:

  • (Float, nil)


136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rcap/utilities.rb', line 136

def RCAP.to_f_if_given( number )
  if number
    case number
    when String
      number.strip.to_f
    when Numeric
      number.to_f
    else
      number.to_s.strip.to_f
    end
  end
end

+ (Integer?) to_i_if_given(number)

if the string is given, String#strip and then String#to_i are applied otherwise nil is returned.

Parameters:

Returns:

  • (Integer, nil)


154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/rcap/utilities.rb', line 154

def RCAP.to_i_if_given( number )
  if number
    case number
    when String
      number.strip.to_i
    when Numeric
      number.to_i
    else
      number.to_s.to_i
    end
  end
end

+ (String) to_s_for_cap(object)

Calls #to_s_for_cap on the object it it responds to that otherwise just calls #to_s

Parameters:

Returns:



83
84
85
86
87
88
89
90
91
# File 'lib/rcap/utilities.rb', line 83

def self.to_s_for_cap( object )
  if object
    if object.respond_to?( :to_s_for_cap )
      object.to_s_for_cap
    else
      object.to_s
    end
  end
end

+ (Array) unpack_if_given(list)

If the list is given will split it with the separator parameter otherwise retun an empty array.

Parameters:

  • list (String)

    List to split

Returns:



113
114
115
116
117
118
119
# File 'lib/rcap/utilities.rb', line 113

def RCAP.unpack_if_given( list )
  if list
    list.unpack_cap_list
  else
    []
  end
end

+ (REXML::Element?) xpath_first(xml_element, xpath, namespace)

Returns first descendent that matches the given XPath expression.

Parameters:

  • xml_element (REXML::Element)

    Element to start matching from.

  • xpath (String)

    XPath expression

  • namespace (String)

    Namespace in which to do the matching

Returns:

  • (REXML::Element, nil)

    Element matching XPath query or nil



29
30
31
# File 'lib/rcap/utilities.rb', line 29

def self.xpath_first( xml_element, xpath, namespace )
  REXML::XPath.first( xml_element, xpath, { 'cap' => namespace })
end

+ (Array<REXML::Element>) xpath_match(xml_element, xpath, namespace)

Returns all descendents that match the given XPath expression.

Parameters:

  • xml_element (REXML::Element)

    Element to start matching from.

  • xpath (String)

    XPath expression

  • namespace (String)

    Namespace in which to do the matching

Returns:

  • (Array<REXML::Element>)

    Collection of elements matching XPath query



39
40
41
# File 'lib/rcap/utilities.rb', line 39

def self.xpath_match( xml_element, xpath, namespace )
  REXML::XPath.match( xml_element, xpath, { 'cap' => namespace })
end

+ (String?) xpath_text(xml_element, xpath, namespace)

Returns the text of the first descendent that matches the given XPath expression.

Parameters:

  • xml_element (REXML::Element)

    Element to start matching from.

  • xpath (String)

    XPath expression

  • namespace (String)

    Namespace in which to do the matching

Returns:

  • (String, nil)

    Text content of element matching XPath query or nil



18
19
20
21
# File 'lib/rcap/utilities.rb', line 18

def self.xpath_text( xml_element, xpath, namespace )
  element = self.xpath_first( xml_element, xpath, namespace )
  element.text.strip if element && element.text
end