Class: Vzaar::Resource::Base

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/vzaar/resources/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

#blank?, #hash_to_xml, #symb_keys

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



5
6
7
# File 'lib/vzaar/resources/base.rb', line 5

def api_version
  @api_version
end

#docObject (readonly)

Returns the value of attribute doc.



5
6
7
# File 'lib/vzaar/resources/base.rb', line 5

def doc
  @doc
end

#http_status_codeObject (readonly)

Returns the value of attribute http_status_code.



5
6
7
# File 'lib/vzaar/resources/base.rb', line 5

def http_status_code
  @http_status_code
end

Class Method Details

.attribute(name, opts = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vzaar/resources/base.rb', line 21

def attribute(name, opts={})
  field_name = (opts[:field] || name).to_s
  data_type = opts[:type] || String
  node = opts[:node]

  define_method(name) do
    val = instance_variable_get(:"@#{name.to_s}")
    unless val
      root_node = instance_variable_get(:@root_node)
      _node = node ? (root_node + "/" + node.to_s) : root_node

      value = extract_value(_node, field_name)
      return set_value!(name, value, data_type)
    end
    val
  end
end

.root_node(node) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/vzaar/resources/base.rb', line 8

def root_node(node)
  define_method(:initialize) do |*args|
    xml_body = args[0]
    status_code = args[1]
    _node = node

    instance_variable_set(:@root_node, _node)
    instance_variable_set(:@doc, Nokogiri::XML(xml_body))
    instance_variable_set(:@http_status_code, status_code.to_i)
    set_api_version!(_node)
  end
end

Instance Method Details

#errorsObject



40
41
42
43
44
45
# File 'lib/vzaar/resources/base.rb', line 40

def errors
  doc.xpath("//vzaar-api/errors").map do |e|
    node = e.children.first
    { node.name => node.text }
  end
end