class Occi::Core::Parsers::JsonParser

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

@author Boris Parak <parak@cesnet.cz>

Constants

MEDIA_TYPES

Media type constants

Public Instance Methods

action_instances(body, _headers = nil) 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/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) 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/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) 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/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