Class: ZohoInvoiceResource::Base

Inherits:
ActiveResource::Base
  • Object
show all
Includes:
ActiveRecord::AttributeAssignment, UnderscoreKeys
Defined in:
lib/zoho_invoice_resource/base.rb

Direct Known Subclasses

Customer, Invoice

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UnderscoreKeys

#load

Class Method Details

.cached_resource(options = {}) ⇒ Object

For CachedResource



40
41
42
43
44
45
46
47
48
# File 'lib/zoho_invoice_resource/base.rb', line 40

def cached_resource(options={})
  if defined?(::CachedResource::Model) && self.include?(::CachedResource::Model)
    super.tap do
      include CachedResourcePatch
    end
  else
    super
  end
end

.collection_path(prefix_options = {}, query_options = {}) ⇒ Object



19
20
21
22
# File 'lib/zoho_invoice_resource/base.rb', line 19

def collection_path(prefix_options = {}, query_options = {})
  query_options.merge!(auth_params)
  super(prefix_options, query_options)
end

.connection(refresh = false) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/zoho_invoice_resource/base.rb', line 75

def self.connection(refresh = false)
  @connection = Connection.new(site, format) if refresh || @connection.nil?
  @connection.tap do |c|
    c.proxy = proxy if proxy
    c.user = user if user
    c.password = password if password
    c.auth_type = auth_type if auth_type
    c.timeout = timeout if timeout
    c.ssl_options = ssl_options if ssl_options
  end
end

.element_path(id, prefix_options = {}, query_options = {}) ⇒ Object



14
15
16
17
# File 'lib/zoho_invoice_resource/base.rb', line 14

def element_path(id, prefix_options = {}, query_options = {})
  query_options.merge!(auth_params)
  super(id, prefix_options, query_options)
end

.find_all_recursively(options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/zoho_invoice_resource/base.rb', line 24

def find_all_recursively(options = {})
  collection = find(:all, options.merge(params: {'Per_Page' => 500}))
  while collection.page && (collection.page < collection.total_pages)
    next_collection = find(:all, params: {'Page' => collection.page + 1, 'Per_Page' => 500})
    collection.merge!(next_collection)
  end
  collection
rescue ActiveResource::ResourceNotFound
  nil
end

.formatObject



35
36
37
# File 'lib/zoho_invoice_resource/base.rb', line 35

def format
  self._format ||= Formats::Base.new(self.to_s.demodulize)
end

Instance Method Details

#create_resource_for(resource_name) ⇒ Object



68
69
70
71
72
73
# File 'lib/zoho_invoice_resource/base.rb', line 68

def create_resource_for(resource_name)
  super.tap do |resource|
    resource.send(:include, UnderscoreKeys)
    resource.send(:include, ActiveRecord::AttributeAssignment)
  end
end

#encode(options = {}) ⇒ Object



63
64
65
66
# File 'lib/zoho_invoice_resource/base.rb', line 63

def encode(options={})
  xml = self.class.format.encode(self, {:root => self.class.element_name}.merge(options))
  URI.encode_www_form(self.class.auth_params.merge('XMLString' => xml))
end

#updateObject



51
52
53
54
55
56
57
# File 'lib/zoho_invoice_resource/base.rb', line 51

def update
  run_callbacks :update do
    connection.post(self.class.custom_method_collection_url(:update, prefix_options), encode, self.class.headers).tap do |response|
      load_attributes_from_response(response)
    end
  end
end

#update_element_path(options = nil) ⇒ Object



59
60
61
# File 'lib/zoho_invoice_resource/base.rb', line 59

def update_element_path(options = nil)
  self.class.element_path(to_param, options || prefix_options)
end