Class: ZohoInvoiceResource::Formats::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveResource::Formats::XmlFormat
Defined in:
lib/zoho_invoice_resource/formats/base.rb

Direct Known Subclasses

Customer, Invoice

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element_name = nil) ⇒ Base

Returns a new instance of Base.



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

def initialize(element_name = nil)
  @element_name = element_name || self.class.to_s.demodulize
end

Instance Attribute Details

#element_nameObject (readonly)

Returns the value of attribute element_name.



8
9
10
# File 'lib/zoho_invoice_resource/formats/base.rb', line 8

def element_name
  @element_name
end

Instance Method Details

#decode(xml) ⇒ Object



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

def decode(xml)
  hash = ActiveResource::Formats.remove_root(Hash.from_xml(xml))
  if hash['status'] == '1'
    # success
    # single record or collection
    hash[element_name] || hash
  else
    # failure
    api_response = OpenStruct.new(Util.underscore_keys(hash))
    case api_response.code.to_i
    when 1002
      raise ActiveResource::ResourceNotFound.new(api_response)
    else
      raise ActiveResource::BadRequest.new(api_response)
    end
  end
end

#mime_type(http_method = :get) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/zoho_invoice_resource/formats/base.rb', line 13

def mime_type(http_method = :get)
  if http_method == :post
    'application/x-www-form-urlencoded'
  else
    'application/xml'
  end
end