class BooticClient::Entity
Constants
- CURIES_REL
- CURIE_EXP
- SPECIAL_PROP_EXP
Attributes
attrs[R]
client[R]
curies[R]
entities[R]
top[R]
Public Class Methods
new(attrs, client, top: self)
click to toggle source
# File lib/bootic_client/entity.rb, line 39 def initialize(attrs, client, top: self) @attrs = attrs.kind_of?(Hash) ? attrs : {} @client, @top = client, top build! self.extend EnumerableEntity if iterable? end
wrap(obj, client: nil, top: nil)
click to toggle source
# File lib/bootic_client/entity.rb, line 77 def self.wrap(obj, client: nil, top: nil) case obj when Hash new(obj, client, top: top) when Array obj.map{|e| wrap(e, client: client, top: top)} else obj end end
Public Instance Methods
[](key)
click to toggle source
# File lib/bootic_client/entity.rb, line 50 def [](key) key = key.to_sym has_property?(key) ? properties[key] : entities[key] end
can?(rel_name)
click to toggle source
# File lib/bootic_client/entity.rb, line 59 def can?(rel_name) has_rel? rel_name end
has?(prop_name)
click to toggle source
# File lib/bootic_client/entity.rb, line 55 def has?(prop_name) has_property?(prop_name) || has_entity?(prop_name) || has_rel?(prop_name) end
has_entity?(prop_name)
click to toggle source
# File lib/bootic_client/entity.rb, line 112 def has_entity?(prop_name) entities.has_key? prop_name.to_sym end
has_property?(prop_name)
click to toggle source
# File lib/bootic_client/entity.rb, line 108 def has_property?(prop_name) properties.has_key? prop_name.to_sym end
has_rel?(prop_name)
click to toggle source
# File lib/bootic_client/entity.rb, line 116 def has_rel?(prop_name) rels.has_key? prop_name.to_sym end
inspect()
click to toggle source
# File lib/bootic_client/entity.rb, line 63 def inspect %(#<#{self.class.name} props: [#{properties.keys.join(', ')}] rels: [#{rels.keys.join(', ')}] entities: [#{entities.keys.join(', ')}]>) end
links()
click to toggle source
# File lib/bootic_client/entity.rb, line 73 def links @links ||= attrs.fetch('_links', {}) end
method_missing(name, *args, &block)
click to toggle source
Calls superclass method
# File lib/bootic_client/entity.rb, line 88 def method_missing(name, *args, &block) if !block_given? if has_property?(name) self[name] elsif has_entity?(name) entities[name] elsif has_rel?(name) rels[name].run(*args) else super end else super end end
properties()
click to toggle source
# File lib/bootic_client/entity.rb, line 67 def properties @properties ||= attrs.select{|k,v| !(k =~ SPECIAL_PROP_EXP)}.each_with_object({}) do |(k,v),memo| memo[k.to_sym] = Entity.wrap(v, client: client, top: top) end end
rels()
click to toggle source
# File lib/bootic_client/entity.rb, line 120 def rels @rels ||= ( links = attrs.fetch('_links', {}) links.each_with_object({}) do |(rel,rel_attrs),memo| if rel =~ CURIE_EXP _, curie_namespace, rel = rel.split(CURIE_EXP) if curie = curies.find{|c| c['name'] == curie_namespace} rel_attrs['docs'] = Relation.expand(curie['href'], rel: rel) end end if rel != CURIES_REL rel_attrs['name'] = rel memo[rel.to_sym] = Relation.new(rel_attrs, client) end end ) end
respond_to_missing?(method_name, include_private = false)
click to toggle source
# File lib/bootic_client/entity.rb, line 104 def respond_to_missing?(method_name, include_private = false) has?(method_name) end
to_hash()
click to toggle source
# File lib/bootic_client/entity.rb, line 46 def to_hash @attrs end
Private Instance Methods
build!()
click to toggle source
# File lib/bootic_client/entity.rb, line 146 def build! @curies = top.links.fetch('curies', []) @entities = attrs.fetch('_embedded', {}).each_with_object({}) do |(k,v),memo| memo[k.to_sym] = if v.kind_of?(Array) v.map{|ent_attrs| Entity.new(ent_attrs, client, top: top)} else Entity.new(v, client, top: top) end end end
iterable?()
click to toggle source
# File lib/bootic_client/entity.rb, line 142 def iterable? has_entity?(:items) && entities[:items].respond_to?(:each) end