class Occi::Core::Parsers::BaseParser

Implementes base components necessary to parse all renderings.

@attr model [Occi::Core::Model, Occi::Infrastructure::Model] model to use as a primary reference point @attr #media_type [String] type of content to parse

@abstract Not for direct use. @author Boris Parak <parak@cesnet.cz>

Constants

DELEGATED

Shortcuts to interesting methods on logger

MEDIA_TYPES

Media type constants

Attributes

media_type[RW]
model[RW]

Public Class Methods

media_types() click to toggle source

Returns a list of supported media types for this parser.

@return [Array] list of supported media types

# File lib/occi/core/parsers/base_parser.rb, line 133
def media_types
  self::MEDIA_TYPES
end
new(args = {}) click to toggle source

Constructs an instance of the parser that will use a particular model as the reference for every parsed instance. Only instances allowed by the model will be successfuly parsed. In case of `Occi::Core::Category` instances, only identifiers are parsed and existing instances from the model are returned.

@param args [Hash] constructor arguments in a Hash @option args [Occi::Core::Model] :model model to use as a primary reference point @option args [String] :media_type type of content to parse

# File lib/occi/core/parsers/base_parser.rb, line 33
def initialize(args = {})
  pre_initialize(args)
  default_args! args

  @model = args.fetch(:model)
  @media_type = args.fetch(:media_type)
  logger.debug "Initializing parser for #{media_type.inspect}"

  post_initialize(args)
end
parses?(media_type) click to toggle source

Checks whether the given media type is supported by this parser.

@param #media_type [String] media type string as provided by the transport protocol @return [TrueClass] if supported @return [FalseClass] if not supported

# File lib/occi/core/parsers/base_parser.rb, line 143
def parses?(media_type)
  media_types.include? media_type
end

Public Instance Methods

action_instances(_body, _headers) click to toggle source

Parses action instances from the given body/headers. Only actions already declared in the model are allowed.

@param body [String] raw `String`-like body as provided by the transport protocol @param headers [Hash] raw headers as provided by the transport protocol @return [Set] set of parsed instances

# File lib/occi/core/parsers/base_parser.rb, line 71
def action_instances(_body, _headers)
  raise Occi::Core::Errors::ParserError, 'This method needs to be implemented in subclasses'
end
actions(body, headers) click to toggle source

See `#categories`.

# File lib/occi/core/parsers/base_parser.rb, line 97
def actions(body, headers)
  categories body, headers, Occi::Core::Action
end
categories(_body, _headers, _expectation = nil) click to toggle source

Parses categories from the given body/headers and returns corresponding instances from the known model.

@param body [String] raw `String`-like body as provided by the transport protocol @param headers [Hash] raw headers as provided by the transport protocol @param expectation [Class] expected class of the returned instance(s) @return [Set] set of instances

# File lib/occi/core/parsers/base_parser.rb, line 82
def categories(_body, _headers, _expectation = nil)
  raise Occi::Core::Errors::ParserError, 'This method needs to be implemented in subclasses'
end
entities(_body, _headers, _expectation = nil) click to toggle source

Parses entities from the given body/headers. Only kinds, mixins, and actions already declared in the model are allowed.

@param body [String] raw `String`-like body as provided by the transport protocol @param headers [Hash] raw headers as provided by the transport protocol @param expectation [Class] expected class of the returned instance(s) @return [Set] set of instances

# File lib/occi/core/parsers/base_parser.rb, line 51
def entities(_body, _headers, _expectation = nil)
  raise Occi::Core::Errors::ParserError, 'This method needs to be implemented in subclasses'
end
kinds(body, headers) click to toggle source

See `#categories`.

# File lib/occi/core/parsers/base_parser.rb, line 87
def kinds(body, headers)
  categories body, headers, Occi::Core::Kind
end
lookup(identifier, klass) click to toggle source

Looks up the given category identifier in the model. Unsuccessfull lookup will raise an error, as will an unexpected class of the found instance.

@param identifier [String] category identifier to look up in the model @param klass [Class] expected class (raises error otherwise) @return [Object] found instance

# File lib/occi/core/parsers/base_parser.rb, line 117
def lookup(identifier, klass)
  found = handle(Occi::Core::Errors::ParsingError) { model.find_by_identifier!(identifier) }
  unless found.is_a?(klass)
    raise Occi::Core::Errors::ParsingError, "#{identifier.inspect} is not of expected class #{klass}"
  end
  found
end
mixins(body, headers) click to toggle source

See `#categories`.

# File lib/occi/core/parsers/base_parser.rb, line 92
def mixins(body, headers)
  categories body, headers, Occi::Core::Mixin
end
parses?(media_type) click to toggle source

Checks whether the given media type is supported by this parser instance.

@param #media_type [String] media type string as provided by the transport protocol @return [TrueClass] if supported @return [FalseClass] if not supported

# File lib/occi/core/parsers/base_parser.rb, line 107
def parses?(media_type)
  self.media_type == media_type
end
resources(body, headers) click to toggle source

See `#entities`.

# File lib/occi/core/parsers/base_parser.rb, line 56
def resources(body, headers)
  entities body, headers, Occi::Core::Resource
end