Class: Occi::Core::Parsers::Json::Category

Inherits:
Object
  • Object
show all
Extended by:
Helpers::ParserDereferencer, Helpers::RawJsonParser
Includes:
Helpers::ErrorHandler, Yell::Loggable
Defined in:
lib/occi/core/parsers/json/category.rb

Overview

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

Author:

Constant Summary collapse

TYPECASTER_HASH =

Typecasting lambdas

{
  'boolean' => Boolean,
  'string'  => String,
  'number'  => Numeric,
  'array'   => Array,
  'object'  => Hash
}.freeze
PARENT_KEY =

Hash constants for ParserDereferencer

:parent
APPLIES_KEY =
:applies
DEPENDS_KEY =
:depends
DELEGATED =

Shortcuts to interesting methods on logger

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

Class Method Summary collapse

Methods included from Helpers::RawJsonParser

raw_hash

Methods included from Helpers::ParserDereferencer

dereference_identifiers!, first_or_die, lookup_action_references!, lookup_applies_references!, lookup_depends_references!, lookup_parent_references!, lookup_references!

Methods included from Helpers::ErrorHandler

#handle, included

Class Method Details

.attribute_definitions(raw) ⇒ Object

:nodoc:


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/occi/core/parsers/json/category.rb', line 79

def attribute_definitions(raw)
  return {} if raw.blank?

  attr_defs = {}
  raw.each_pair do |k, v|
    logger.debug "Creating AttributeDefinition for #{k.inspect} from #{v.inspect}" if logger_debug?
    def_hsh = typecast(v)
    unless def_hsh[:type]
      raise Occi::Core::Errors::ParsingError, "Attribute #{k.to_s.inspect} has no type"
    end
    attr_defs[k.to_s] = Occi::Core::AttributeDefinition.new def_hsh
  end

  attr_defs
end

.instantiate_hashes!(raw, model) ⇒ Object

:nodoc:


54
55
56
57
58
# File 'lib/occi/core/parsers/json/category.rb', line 54

def instantiate_hashes!(raw, model)
  raw[:kinds].each { |k| model << instatiate_hash(k, Occi::Core::Kind) } if raw[:kinds]
  raw[:mixins].each { |k| model << instatiate_hash(k, Occi::Core::Mixin) } if raw[:mixins]
  raw[:actions].each { |k| model << instatiate_hash(k, Occi::Core::Action) } if raw[:actions]
end

.instatiate_hash(raw, klass) ⇒ Object

:nodoc:


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/occi/core/parsers/json/category.rb', line 61

def instatiate_hash(raw, klass)
  logger.debug "Creating #{klass} from #{raw.inspect}" if logger_debug?

  obj = klass.new(
    term: raw[:term], schema: raw[:scheme], title: raw[:title],
    attributes: attribute_definitions(raw[:attributes])
  )

  if obj.respond_to?(:location)
    logger.debug "Setting location #{raw[:location].inspect}" if logger_debug?
    obj.location = handle(Occi::Core::Errors::ParsingError) { URI.parse(raw[:location]) }
  end

  logger.debug "Created category #{obj.inspect}" if logger_debug?
  obj
end

.json(body, model) ⇒ Occi::Core::Model

Parses categories into instances of subtypes of `Occi::Core::Category`. Internal references between objects are converted from strings to actual objects. Categories provided in the model will be reused but have to be declared in the parsed model as well.


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/occi/core/parsers/json/category.rb', line 41

def json(body, model)
  parsed = raw_hash(body)
  instantiate_hashes! parsed, model
  logger.debug "Parsed into raw hashes #{parsed.inspect}" if logger_debug?

  raw_categories = [parsed[:kinds], parsed[:mixins]].flatten.compact
  dereference_identifiers! model.categories, raw_categories

  logger.debug "Returning (updated) model #{model.inspect}" if logger_debug?
  model
end

.lookup_applies_references!(mixin, derefd, parsed_rel) ⇒ Object

:nodoc:


107
108
109
110
111
# File 'lib/occi/core/parsers/json/category.rb', line 107

def lookup_applies_references!(mixin, derefd, parsed_rel)
  logger.debug "Looking up applies from #{parsed_rel.inspect}" if logger_debug?
  return if parsed_rel.blank?
  parsed_rel.each { |kind| mixin.applies << first_or_die(derefd, kind) }
end

.lookup_depends_references!(mixin, derefd, parsed_rel) ⇒ Object

:nodoc:


114
115
116
117
118
# File 'lib/occi/core/parsers/json/category.rb', line 114

def lookup_depends_references!(mixin, derefd, parsed_rel)
  logger.debug "Looking up depens from #{parsed_rel.inspect}" if logger_debug?
  return if parsed_rel.blank?
  parsed_rel.each { |mxn| mixin.depends << first_or_die(derefd, mxn) }
end

.typecast(hash) ⇒ Object

:nodoc:


96
97
98
99
100
101
102
103
104
# File 'lib/occi/core/parsers/json/category.rb', line 96

def typecast(hash)
  hash = hash.clone
  hash[:type] = TYPECASTER_HASH[hash[:type]]

  return hash if hash[:pattern].blank?
  hash[:pattern] = handle(Occi::Core::Errors::ParsingError) { Regexp.new(hash[:pattern]) }

  hash
end