Class: Occi::Core::Parsers::Text::Location

Inherits:
Object
  • Object
show all
Includes:
Helpers::ErrorHandler, Yell::Loggable
Defined in:
lib/occi/core/parsers/text/location.rb

Overview

Static parsing class responsible for extracting URI-like locations from plain text. Class supports both 'text/uri-list' and 'text/plain' via `uri_list` and `plain` respectively.

Author:

Constant Summary collapse

DELEGATED =

Shortcuts to interesting methods on logger

%i[debug? info? warn? error? fatal?].freeze

Class Method Summary collapse

Methods included from Helpers::ErrorHandler

#handle, included

Class Method Details

.plain(lines) ⇒ Array

Parses text/plain OCCI locations into `URI` instances suitable for futher processing. Every location line is expected to begin with 'X-OCCI-Location'.


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/occi/core/parsers/text/location.rb', line 23

def plain(lines)
  regexp = Regexp.new(Constants::REGEXP_LOCATION)

  locations = lines.map do |line|
    next if line.blank?
    logger.debug "Parsing location from line #{line.inspect}" if logger_debug?

    matched = line.match(regexp)
    unless matched
      raise Occi::Core::Errors::ParsingError, "#{line.inspect} does not match 'X-OCCI-Location: URI'"
    end
    handle(Occi::Core::Errors::ParsingError) { URI.parse(matched[:location].strip) }
  end

  locations.compact
end

.uri_list(lines) ⇒ Array

Parses text/uri-list lines into `URI` instances suitable for futher processing. Lines starting with '#' are ommited, as per tools.ietf.org/html/rfc2483#section-5


45
46
47
48
49
50
51
52
53
54
# File 'lib/occi/core/parsers/text/location.rb', line 45

def uri_list(lines)
  uris = lines.map do |line|
    next if line.blank? || line.start_with?('#')
    logger.debug "Parsing location from line #{line.inspect}" if logger_debug?

    handle(Occi::Core::Errors::ParsingError) { URI.parse(line.strip) }
  end

  uris.compact
end