class Occi::Core::Parsers::Json::Entity

Static parsing class responsible for extracting entities from JSON. Class supports 'application/json' via `json`. No other formats are supported.

@attr model [Occi::Core::Model, Occi::Infrastructure::Model] model to use as a primary reference point

@author Boris Parak <parak@cesnet.cz>

Constants

DELEGATED

Shortcuts to interesting methods on logger

MULTI_INSTANCE_TYPES
SINGLE_INSTANCE_TYPES

Constants

TYPECASTER_HASH

Attributes

model[R]

Public Class Methods

new(args = {}) click to toggle source

Constructs an instance of the entity parser. Only entities (their kinds) defined by the model are allowed.

@param args [Hash] constructor arguments in a Hash @option args [Occi::Core::Model] :model model to use as a primary reference point

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

  @model = args.fetch(:model)

  post_initialize(args)
end

Public Instance Methods

json(body, type) click to toggle source

Builds an entity instances from the lines provided as input.

@param body [String] JSON body for parsing @param type [Symbol] `:resource`, `:link`, or `:'entity-collection'` @return [Array] constructed instances

# File lib/occi/core/parsers/json/entity.rb, line 52
def json(body, type)
  symbol = case type
           when *SINGLE_INSTANCE_TYPES
             :json_single
           when *MULTI_INSTANCE_TYPES
             :json_collection
           else
             raise Occi::Core::Errors::ParserError, "#{type.inspect} is not a valid type"
           end

  send symbol, self.class.raw_hash(body)
end
json_collection(hash) click to toggle source

Builds entity instances from the hash provided as input.

@param hash [Hash] Hash body for parsing @return [Array] constructed instances

# File lib/occi/core/parsers/json/entity.rb, line 85
def json_collection(hash)
  all = []

  logger.debug "Converting #{hash.inspect} into multiple instances" if logger_debug?
  all.concat hash[:resources] if hash[:resources]
  all.concat hash[:links] if hash[:links]
  all.map! { |a| json_single(a) }

  logger.debug "Created instances #{all.inspect}" if logger_debug?
  Set.new(all).flatten
end
json_single(hash) click to toggle source

Builds an entity instance from the hash provided as input.

@param hash [Hash] Hash body for parsing @return [Array] constructed instances

# File lib/occi/core/parsers/json/entity.rb, line 69
def json_single(hash)
  logger.debug "Converting #{hash.inspect} into a single instance" if logger_debug?
  instance = @_ib.get hash[:kind], mixins: lookup(hash[:mixins]), actions: lookup(hash[:actions])

  set_attributes! instance, hash[:attributes]
  set_links! instance, hash[:links] if instance.respond_to?(:links)
  set_target! instance, hash[:target] if instance.respond_to?(:target)

  logger.debug "Created instance #{instance.inspect}" if logger_debug?
  Set.new [instance]
end