Class: ZohoInvoice::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/zoho_invoice/base.rb

Direct Known Subclasses

Contact, Customer, Invoice, InvoiceItem, Item

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, options = {}) ⇒ Base

Returns a new instance of Base.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/zoho_invoice/base.rb', line 40

def initialize(client, options = {})
  @client = client

  # Assign all of the single attribtues
  #
  if !self.attributes.empty?
    (self.attributes & options.keys).each do |attribute|
      self.send("#{attribute}=", options[attribute])
    end
  end

  # Assign all of the associations.  Not the most advanced
  #
  if self.reflections.is_a?(Array)
    self.reflections.each { |r| self.send("#{r}=", []) }
    (self.reflections & options.keys).each do |reflection|
      options[reflection].each do |reflection_obj|
        klass = ZohoInvoice.const_get(camel_case(reflection.to_s[0..-2]))
        if reflection_obj.is_a?(Hash)
          self.send("#{reflection}") << klass.new(@client, reflection_obj)
        elsif reflection_obj.is_a?(klass)
          self.send("#{reflection}") << reflection_obj
        end
      end
    end
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/zoho_invoice/base.rb', line 9

def client
  @client
end

Class Method Details

.create(client, options = {}) ⇒ Object



23
24
25
# File 'lib/zoho_invoice/base.rb', line 23

def self.create(client, options = {})
  self.new(client, options).save
end

.create_attributes(attrs) ⇒ Object



102
103
104
105
106
# File 'lib/zoho_invoice/base.rb', line 102

def self.create_attributes(attrs)
  attrs.each do |attr|
    attr_accessor attr
  end
end

.define_object_attrs(*attrs) ⇒ Object



11
12
13
14
# File 'lib/zoho_invoice/base.rb', line 11

def self.define_object_attrs(*attrs)
  @attributes = attrs
  create_attributes(attrs)
end

.has_many(*attrs) ⇒ Object

TODO Create an Association class to manage the relationship



18
19
20
21
# File 'lib/zoho_invoice/base.rb', line 18

def self.has_many(*attrs)
  @reflections = attrs
  create_attributes(attrs)
end

.search(client, input_text, options = {}) ⇒ Object

TODO need to build a class that is something like ActiveRecord::Relation TODO need to be able to handle associations when hydrating objects



30
31
32
33
34
35
36
37
38
# File 'lib/zoho_invoice/base.rb', line 30

def self.search(client, input_text, options = {})
  result_hash = client.get("/api/view/search/#{self.to_s.split('::').last.downcase}s", :searchtext => input_text).body
  objects_to_hydrate = result_hash['Response']["#{self.to_s.split('::').last}s"]["#{self.to_s.split('::').last}"]
  self.process_objects(client, objects_to_hydrate)
rescue Faraday::Error::ClientError => e
  if e.response[:body]
    raise ZohoInvoice::Error::ClientError.from_response(e.response)
  end
end

Instance Method Details

#attributesObject



72
73
74
# File 'lib/zoho_invoice/base.rb', line 72

def attributes
  self.class.instance_variable_get(:'@attributes') || []
end

#reflectionsObject



68
69
70
# File 'lib/zoho_invoice/base.rb', line 68

def reflections
  self.class.instance_variable_get(:'@reflections') || []
end

#saveObject

TODO Determining the resource to use will need to change



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/zoho_invoice/base.rb', line 78

def save

  action = 'create'
  action = 'update' if !send("#{self.class.to_s.split('::').last.downcase}_id").nil?

  result = client.post("/api/#{self.class.to_s.split('::').last.downcase + 's'}/#{action}", :XMLString => self.to_xml)

  if action == 'create' && !result.body.nil? && !result.body[self.class.to_s].nil?
    self.send("#{self.class.to_s.downcase}_id=", result.body[self.class.to_s]["#{self.class}ID"])
  end

  self
rescue Faraday::Error::ClientError => e
  if e.response[:body]
    raise ZohoInvoice::Error::ClientError.from_response(e.response)
  end
end

#to_xml(*args) ⇒ Object

This needs to be a Nokogiri::XML::Builder



98
99
100
# File 'lib/zoho_invoice/base.rb', line 98

def to_xml(*args)
  build_attributes.to_xml(*args)
end