Class: KashflowApi::SoapObject

Inherits:
Object
  • Object
show all
Includes:
Expects
Defined in:
lib/kashflow_api/soap_object.rb

Direct Known Subclasses

Customer, Invoice, Line, NominalCode, Receipt, Supplier

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil) ⇒ SoapObject

Returns a new instance of SoapObject.



7
8
9
10
11
# File 'lib/kashflow_api/soap_object.rb', line 7

def initialize(hash = nil)
  expects(hash, Hash) if hash
  @hash = (hash || new_object_hash)
  init_class
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



5
6
7
# File 'lib/kashflow_api/soap_object.rb', line 5

def hash
  @hash
end

Class Method Details

.allObject



17
18
19
20
21
22
23
24
# File 'lib/kashflow_api/soap_object.rb', line 17

def self.all
  result = KashflowApi.api.send("get_#{self::KFObject[:plural]}".to_sym)
  results = []
  result.hash[:envelope][:body]["get_#{self::KFObject[:plural]}_response".to_sym]["get_#{self::KFObject[:plural]}_result".to_sym][self::KFObject[:singular].to_sym].each do |result|
    results.push self.new result
  end
  return results
end

.define_methodsObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kashflow_api/soap_object.rb', line 34

def self.define_methods
  self::Finds.each do |find|
    method_name = "find_by_#{find}".to_sym
    result_name = find == self::Finds.first ? "get_#{self::KFObject[:singular]}" : "get_#{self::KFObject[:singular]}_by_#{find}"
    
    self.define_singleton_method(method_name, Proc.new { |search|
      result = KashflowApi.api.send(result_name.to_sym, search)
      hash = result.hash[:envelope][:body]["#{result_name}_response".to_sym]["#{result_name}_result".to_sym]
      return self.new(hash)
    })
  end
end

.find(search) ⇒ Object



26
27
28
# File 'lib/kashflow_api/soap_object.rb', line 26

def self.find(search)
  self.send(find_method, search)
end

.find_methodObject



30
31
32
# File 'lib/kashflow_api/soap_object.rb', line 30

def self.find_method
  "find_by_#{self::Finds.first}".to_sym
end

.inherited(klass) ⇒ Object



13
14
15
# File 'lib/kashflow_api/soap_object.rb', line 13

def self.inherited(klass)
  KashflowApi.add_soap_object(klass)
end

Instance Method Details

#to_xmlObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kashflow_api/soap_object.rb', line 47

def to_xml
    xml = []
    id_line = ""
    @hash.keys.each do |key|
        if key == self.class::XMLKey
            id_line = "<#{key}>#{@hash[key]}</#{key}>" unless @hash[key] == "0"
        else
            xml.push("<#{key}>#{@hash[key]}</#{key}>")
        end
    end
    [id_line, xml.join].join
end