Class: ApiBee::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/api_bee/node.rb

Direct Known Subclasses

List, Single

Defined Under Namespace

Classes: List, Single

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, config, attrs, href) ⇒ Node

Returns a new instance of Node.



24
25
26
27
28
29
30
# File 'lib/api_bee/node.rb', line 24

def initialize(adapter, config, attrs, href)
  @adapter = adapter
  @config = config
  @attributes = {}
  @href = href
  update_attributes attrs
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



22
23
24
# File 'lib/api_bee/node.rb', line 22

def adapter
  @adapter
end

Class Method Details

.resolve(adapter, config, attrs, href = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/api_bee/node.rb', line 12

def self.resolve(adapter, config, attrs, href = nil)
  attrs = simbolized(attrs)
  keys = attrs.keys.map{|k| k.to_sym}
  if keys.include?(config.total_entries_property_name) && keys.include?(config.uri_property_name.to_sym) # is a paginator
    List.new adapter, config, attrs, href
  else
    Single.new adapter, config, attrs, href
  end
end

.simbolized(hash) ⇒ Object



5
6
7
8
9
10
# File 'lib/api_bee/node.rb', line 5

def self.simbolized(hash)
  hash.inject({}) do |options, (key, value)|
    options[(key.to_sym rescue key) || key] = value
    options
  end
end

Instance Method Details

#[](attribute_name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/api_bee/node.rb', line 36

def [](attribute_name)
  if respond_to?(attribute_name)
    send attribute_name
  elsif value = @attributes[attribute_name]
    resolve_values_to_nodes value
  elsif has_more? # check whether there's more info in API
    load_more!
    self[attribute_name] # recurse once
  else
    nil
  end
end

#to_dataObject



32
33
34
# File 'lib/api_bee/node.rb', line 32

def to_data
  @attributes
end