Class: BooticClient::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/bootic_client/entity.rb

Constant Summary collapse

CURIE_EXP =
/(.+):(.+)/.freeze
CURIES_REL =
'curies'.freeze
SPECIAL_PROP_EXP =
/^_.+/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs, client, top = self) ⇒ Entity

Returns a new instance of Entity.



13
14
15
16
# File 'lib/bootic_client/entity.rb', line 13

def initialize(attrs, client, top = self)
  @attrs, @client, @top = attrs, client, top
  build!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bootic_client/entity.rb', line 51

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].get(*args)
    else
      super
    end
  else
    super
  end
end

Instance Attribute Details

#curies ⇒ Object (readonly)

Returns the value of attribute curies.



11
12
13
# File 'lib/bootic_client/entity.rb', line 11

def curies
  @curies
end

#entities ⇒ Object (readonly)

Returns the value of attribute entities.



11
12
13
# File 'lib/bootic_client/entity.rb', line 11

def entities
  @entities
end

Class Method Details

.wrap(obj) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/bootic_client/entity.rb', line 40

def self.wrap(obj)
  case obj
  when Hash
    OpenStruct.new(obj)
  when Array
    obj.map{|e| wrap(e)}
  else
    obj
  end
end

Instance Method Details

#[](key) ⇒ Object



18
19
20
# File 'lib/bootic_client/entity.rb', line 18

def [](key)
  properties[key.to_sym]
end

#each(&block) ⇒ Object



83
84
85
# File 'lib/bootic_client/entity.rb', line 83

def each(&block)
  iterable? ? entities[:items].each(&block) : [self].each(&block)
end

#has?(prop_name) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/bootic_client/entity.rb', line 22

def has?(prop_name)
  has_property?(prop_name) || has_entity?(prop_name) || has_rel?(prop_name)
end

#has_entity?(prop_name) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/bootic_client/entity.rb', line 75

def has_entity?(prop_name)
  entities.has_key? prop_name.to_sym
end

#has_property?(prop_name) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/bootic_client/entity.rb', line 71

def has_property?(prop_name)
  properties.has_key? prop_name.to_sym
end

#has_rel?(prop_name) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/bootic_client/entity.rb', line 79

def has_rel?(prop_name)
  rels.has_key? prop_name.to_sym
end

#inspect ⇒ Object



26
27
28
# File 'lib/bootic_client/entity.rb', line 26

def inspect
  %(#<#{self.class.name} props: [#{properties.keys.join(', ')}] rels: [#{rels.keys.join(', ')}] entities: [#{entities.keys.join(', ')}]>)
end


36
37
38
# File 'lib/bootic_client/entity.rb', line 36

def links
  @links ||= attrs.fetch('_links', {})
end

#properties ⇒ Object



30
31
32
33
34
# File 'lib/bootic_client/entity.rb', line 30

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)
  end
end

#rels ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/bootic_client/entity.rb', line 87

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, Entity)
      end
    end
  )
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/bootic_client/entity.rb', line 67

def respond_to_missing?(method_name, include_private = false)
  has?(method_name)
end