Class: Occi::Core::Parsers::BaseParser Abstract

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

Overview

This class is abstract.

Not for direct use.

Implementes base components necessary to parse all renderings.

Author:

Direct Known Subclasses

JsonParser, TextParser

Constant Summary collapse

MEDIA_TYPES =

Media type constants

[].freeze
DELEGATED =

Shortcuts to interesting methods on logger

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::ErrorHandler

#handle, included

Constructor Details

#initialize(args = {}) ⇒ BaseParser

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.

Options Hash (args):

  • :model (Occi::Core::Model)

    model to use as a primary reference point

  • :media_type (String)

    type of content to parse


33
34
35
36
37
38
39
40
41
42
# 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

Instance Attribute Details

#media_typeString

type of content to parse


11
12
13
# File 'lib/occi/core/parsers/base_parser.rb', line 11

def media_type
  @media_type
end

#modelOcci::Core::Model, Occi::Infrastructure::Model

model to use as a primary reference point


11
12
13
# File 'lib/occi/core/parsers/base_parser.rb', line 11

def model
  @model
end

Class Method Details

.media_typesArray

Returns a list of supported media types for this parser.


133
134
135
# File 'lib/occi/core/parsers/base_parser.rb', line 133

def media_types
  self::MEDIA_TYPES
end

.parses?(media_type) ⇒ TrueClass, FalseClass

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


143
144
145
# File 'lib/occi/core/parsers/base_parser.rb', line 143

def parses?(media_type)
  media_types.include? media_type
end

Instance Method Details

#action_instances(_body, _headers) ⇒ Set

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


71
72
73
# 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) ⇒ Object

See `#categories`.


97
98
99
# 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) ⇒ Set

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


82
83
84
# 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) ⇒ Set

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


51
52
53
# 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) ⇒ Object

See `#categories`.


87
88
89
# File 'lib/occi/core/parsers/base_parser.rb', line 87

def kinds(body, headers)
  categories body, headers, Occi::Core::Kind
end

See `#entities`.


61
62
63
# File 'lib/occi/core/parsers/base_parser.rb', line 61

def links(body, headers)
  entities body, headers, Occi::Core::Link
end

#lookup(identifier, klass) ⇒ Object

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


117
118
119
120
121
122
123
# 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) ⇒ Object

See `#categories`.


92
93
94
# File 'lib/occi/core/parsers/base_parser.rb', line 92

def mixins(body, headers)
  categories body, headers, Occi::Core::Mixin
end

#parses?(media_type) ⇒ TrueClass, FalseClass

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


107
108
109
# File 'lib/occi/core/parsers/base_parser.rb', line 107

def parses?(media_type)
  self.media_type == media_type
end

#resources(body, headers) ⇒ Object

See `#entities`.


56
57
58
# File 'lib/occi/core/parsers/base_parser.rb', line 56

def resources(body, headers)
  entities body, headers, Occi::Core::Resource
end