Class: Occi::Core::Parsers::BaseParser Abstract
- Inherits:
-
Object
- Object
- Occi::Core::Parsers::BaseParser
- Includes:
- Helpers::ArgumentValidator, Helpers::ErrorHandler, Yell::Loggable
- Defined in:
- lib/occi/core/parsers/base_parser.rb
Overview
Not for direct use.
Implementes base components necessary to parse all renderings.
Direct Known Subclasses
Constant Summary collapse
- MEDIA_TYPES =
Media type constants
[].freeze
Instance Attribute Summary collapse
-
#media_type ⇒ String
type of content to parse.
-
#model ⇒ Occi::Core::Model, Occi::Infrastructure::Model
model to use as a primary reference point.
Class Method Summary collapse
-
.media_types ⇒ Array
Returns a list of supported media types for this parser.
-
.parses?(media_type) ⇒ TrueClass, FalseClass
Checks whether the given media type is supported by this parser.
Instance Method Summary collapse
-
#action_instances(_body, _headers) ⇒ Set
Parses action instances from the given body/headers.
-
#actions(body, headers) ⇒ Object
See ‘#categories`.
-
#categories(_body, _headers, _expectation = nil) ⇒ Set
Parses categories from the given body/headers and returns corresponding instances from the known model.
-
#entities(_body, _headers, _expectation = nil) ⇒ Set
Parses entities from the given body/headers.
-
#initialize(args = {}) ⇒ BaseParser
constructor
Constructs an instance of the parser that will use a particular model as the reference for every parsed instance.
-
#kinds(body, headers) ⇒ Object
See ‘#categories`.
-
#links(body, headers) ⇒ Object
See ‘#entities`.
-
#lookup(identifier, klass) ⇒ Object
Looks up the given category identifier in the model.
-
#mixins(body, headers) ⇒ Object
See ‘#categories`.
-
#parses?(media_type) ⇒ TrueClass, FalseClass
Checks whether the given media type is supported by this parser instance.
-
#resources(body, headers) ⇒ Object
See ‘#entities`.
Methods included from Helpers::ErrorHandler
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.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/occi/core/parsers/base_parser.rb', line 29 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_type ⇒ String
type of content to parse
11 12 13 |
# File 'lib/occi/core/parsers/base_parser.rb', line 11 def media_type @media_type end |
#model ⇒ Occi::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_types ⇒ Array
Returns a list of supported media types for this parser.
125 126 127 |
# File 'lib/occi/core/parsers/base_parser.rb', line 125 def media_types self::MEDIA_TYPES end |
.parses?(media_type) ⇒ TrueClass, FalseClass
Checks whether the given media type is supported by this parser.
135 136 137 |
# File 'lib/occi/core/parsers/base_parser.rb', line 135 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.
67 68 69 |
# File 'lib/occi/core/parsers/base_parser.rb', line 67 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`.
93 94 95 |
# File 'lib/occi/core/parsers/base_parser.rb', line 93 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.
78 79 80 |
# File 'lib/occi/core/parsers/base_parser.rb', line 78 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.
47 48 49 |
# File 'lib/occi/core/parsers/base_parser.rb', line 47 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`.
83 84 85 |
# File 'lib/occi/core/parsers/base_parser.rb', line 83 def kinds(body, headers) categories body, headers, Occi::Core::Kind end |
#links(body, headers) ⇒ Object
See ‘#entities`.
57 58 59 |
# File 'lib/occi/core/parsers/base_parser.rb', line 57 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.
113 114 115 116 117 118 119 |
# File 'lib/occi/core/parsers/base_parser.rb', line 113 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`.
88 89 90 |
# File 'lib/occi/core/parsers/base_parser.rb', line 88 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.
103 104 105 |
# File 'lib/occi/core/parsers/base_parser.rb', line 103 def parses?(media_type) self.media_type == media_type end |