Class: Occi::Core::Parsers::JsonParser

Inherits:
BaseParser
  • Object
show all
Defined in:
lib/occi/core/parsers/json_parser.rb

Overview

Implementes components necessary to parse all required instance types from `JSON` or `JSON`-like format.

Author:

Constant Summary collapse

MEDIA_TYPES =

Media type constants

%w[application/occi+json application/json].freeze

Constants inherited from BaseParser

BaseParser::DELEGATED

Instance Attribute Summary

Attributes inherited from BaseParser

#media_type, #model

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseParser

#actions, #initialize, #kinds, #links, #lookup, media_types, #mixins, #parses?, parses?, #resources

Methods included from Helpers::ErrorHandler

#handle, included

Constructor Details

This class inherits a constructor from Occi::Core::Parsers::BaseParser

Class Method Details

.locations(body, _headers, media_type) ⇒ Array

Extracts URI-like locations from body and headers. For details, see `Occi::Core::Parsers::Json::Location`.


128
129
130
131
132
133
134
135
136
# File 'lib/occi/core/parsers/json_parser.rb', line 128

def locations(body, _headers, media_type)
  unless media_types.include?(media_type)
    raise Occi::Core::Errors::ParsingError, "Locations cannot be parsed from #{media_type.inspect}"
  end
  logger.debug "Parsing locations from #{media_type.inspect} in #{body.inspect}" if logger_debug?

  Json::Validator.validate_locations! body
  handle(Occi::Core::Errors::ParsingError) { JSON.parse(body).map { |i| URI.parse(i) } }
end

.model(body, _headers, media_type, model) ⇒ Occi::Core::Model

Extracts categories from body and headers. For details, see `Occi::Core::Parsers::Json::Category`.


109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/occi/core/parsers/json_parser.rb', line 109

def model(body, _headers, media_type, model)
  unless media_types.include?(media_type)
    raise Occi::Core::Errors::ParsingError,
          "Model cannot be parsed from #{media_type.inspect}"
  end
  logger.debug "Parsing model from #{media_type.inspect} in #{body.inspect}" if logger_debug?

  Json::Validator.validate_model! body
  Json::Category.json body, model

  model
end

Instance Method Details

#action_instances(body, _headers = nil) ⇒ Set

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


57
58
59
60
61
# File 'lib/occi/core/parsers/json_parser.rb', line 57

def action_instances(body, _headers = nil)
  logger.debug "Parsing Occi::Core::ActionInstance(s) from #{body.inspect}" if logger_debug?
  Json::Validator.validate_action_instance! body
  Set.new [Json::ActionInstance.json(body, model)]
end

#categories(body, _headers = nil, expectation = nil) ⇒ Set

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


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/occi/core/parsers/json_parser.rb', line 70

def categories(body, _headers = nil, expectation = nil)
  expectation ||= Occi::Core::Category
  logger.debug "Parsing #{expectation}(s) from #{body.inspect}" if logger_debug?
  Json::Validator.validate_category_identifiers! body

  cats = Set.new
  hsh = handle(Occi::Core::Errors::ParsingError) { JSON.parse(body) }
  hsh.values.flatten.each { |cat_id| cats << lookup(cat_id, expectation) }

  cats
end

#entities(body, _headers = nil, expectation = nil) ⇒ Set

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


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/occi/core/parsers/json_parser.rb', line 35

def entities(body, _headers = nil, expectation = nil)
  expectation ||= Occi::Core::Entity
  logger.debug "Parsing #{expectation}(s) from #{body.inspect}" if logger_debug?
  type = validate_entities! body

  entity_parser = Json::Entity.new(model: model)
  entities = entity_parser.json body, type
  entities.each do |entity|
    unless entity.is_a?(expectation)
      raise Occi::Core::Errors::ParsingError, "Entity is not of type #{expectation}"
    end
  end

  entities
end