kashflow_api 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +14 -0
  4. data/Gemfile +2 -0
  5. data/README.markdown +29 -7
  6. data/kashflow_api.gemspec +1 -1
  7. data/lib/kashflow_api.rb +35 -24
  8. data/lib/kashflow_api/api.rb +25 -25
  9. data/lib/kashflow_api/api_call.rb +19 -55
  10. data/lib/kashflow_api/client.rb +24 -22
  11. data/lib/kashflow_api/models/customer.rb +41 -84
  12. data/lib/kashflow_api/models/invoice.rb +38 -36
  13. data/lib/kashflow_api/models/line.rb +46 -40
  14. data/lib/kashflow_api/models/nominal_code.rb +9 -8
  15. data/lib/kashflow_api/models/quote.rb +15 -11
  16. data/lib/kashflow_api/models/receipt.rb +35 -37
  17. data/lib/kashflow_api/models/supplier.rb +41 -58
  18. data/lib/kashflow_api/soap_object.rb +72 -30
  19. data/lib/kashflow_api/version.rb +1 -1
  20. data/{spec → spec-old}/kashflow_api_customer_balance_spec.rb +0 -0
  21. data/{spec → spec-old}/kashflow_api_customer_spec.rb +0 -0
  22. data/{spec → spec-old}/kashflow_api_nominal_codes_spec.rb +0 -0
  23. data/{spec → spec-old}/kashflow_api_quote_spec.rb +0 -0
  24. data/{spec → spec-old}/kashflow_api_receipt_spec.rb +0 -0
  25. data/{spec → spec-old}/kashflow_api_spec.rb +0 -0
  26. data/{spec → spec-old}/kashflow_api_supplier_spec.rb +0 -0
  27. data/spec-old/spec_helper.rb +21 -0
  28. data/{spec → spec-old}/test_data.example.yml +0 -0
  29. data/spec/core/api_call_spec.rb +25 -0
  30. data/spec/core/api_spec.rb +31 -0
  31. data/spec/core/client_spec.rb +23 -0
  32. data/spec/core/kashflow_api_spec.rb +14 -0
  33. data/spec/core/soap_object_spec.rb +25 -0
  34. data/spec/objects/customer_spec.rb +11 -0
  35. data/spec/objects/inherited_methods_spec.rb +45 -0
  36. data/spec/objects/invoice_spec.rb +6 -0
  37. data/spec/objects/line_spec.rb +5 -0
  38. data/spec/objects/nominal_code_spec.rb +5 -0
  39. data/spec/objects/quote_spec.rb +10 -0
  40. data/spec/objects/receipt_spec.rb +6 -0
  41. data/spec/objects/supplier_spec.rb +6 -0
  42. data/spec/spec_helper.rb +60 -15
  43. data/spec/support/macros.rb +53 -0
  44. metadata +49 -27
@@ -1,47 +1,49 @@
1
1
  module KashflowApi
2
2
  class Invoice < KashflowApi::SoapObject
3
- def self.find(search)
4
- result = KashflowApi.api.get_invoice(search)
5
- self.build_from_soap(result.hash[:envelope][:body][:get_invoice_response][:get_invoice_result])
6
- end
7
-
8
- def customer
9
- KashflowApi::Customer.find_by_customer_id(self.customerid)
10
- end
3
+ Keys = [
4
+ "InvoiceNumber", "InvoiceDate", "DueDate", "CustomerID", "CustomerReference", ["InvoiceDBID", "0"]
5
+ ]
11
6
 
12
- def save
13
- if @hash["InvoiceDBID"] == "0"
14
- insert_invoice
15
- else
16
- update_invoice
17
- end
7
+ Finds = [ "InvoiceNumber" ]
8
+
9
+ KFObject = { singular: "invoice", plural: "invoices" }
10
+
11
+ XMLKey = "InvoiceDBID"
12
+
13
+ define_methods
14
+
15
+ def self.build_arguments(action, object, field, argument)
16
+ if action == "get"
17
+ expects argument, String
18
+ return "<InvoiceNumber>#{argument}</InvoiceNumber>" if object == "invoice"
19
+ elsif action == "update" || action == "insert"
20
+ expects argument, KashflowApi::Invoice
21
+ return "<InvoiceID>#{argument.invoiceid}</InvoiceID><InvLine>#{argument.to_xml}</InvLine>" if field == "Line"
22
+ return "<InvoiceNumber>#{argument.invoicenumber}</InvoiceNumber><InvLine>#{argument.to_xml}</InvLine>" if field == "Number"
23
+ return "<Inv>#{argument.to_xml}</Inv>" if object == "invoice"
18
24
  end
25
+ end
26
+
27
+ def customer
28
+ KashflowApi::Customer.find_by_customer_id(self.customerid)
29
+ end
19
30
 
20
- def to_xml
21
- xml = []
22
- id_line = ""
23
- @hash.keys.each do |key|
24
- if key == "InvoiceDBID"
25
- id_line = "<#{key}>#{@hash[key]}</#{key}>" unless @hash[key] == "0"
26
- else
27
- xml.push("<#{key}>#{@hash[key]}</#{key}>")
28
- end
29
- end
30
- [id_line, xml.join].join
31
+ def save
32
+ if @hash["InvoiceDBID"] == "0"
33
+ insert_invoice
34
+ else
35
+ update_invoice
31
36
  end
37
+ end
32
38
 
33
- private
39
+ private
34
40
 
35
- def blank_object_hash
36
- { "InvoiceNumber" => "", "InvoiceDate" => "", "DueDate" => "", "CustomerID" => "", "CustomerReference" => "", "InvoiceDBID" => "0" }
37
- end
38
-
39
- def update_invoice
40
- KashflowApi.api.update_invoice(self)
41
- end
41
+ def update_invoice
42
+ KashflowApi.api.update_invoice(self)
43
+ end
42
44
 
43
- def insert_invoice
44
- KashflowApi.api.insert_invoice(self)
45
- end
45
+ def insert_invoice
46
+ KashflowApi.api.insert_invoice(self)
47
+ end
46
48
  end
47
49
  end
@@ -1,50 +1,56 @@
1
1
  module KashflowApi
2
2
  class Line < KashflowApi::SoapObject
3
- def save
4
- if @hash["ReceiptID"] != ""
5
- insert_receipt_line
6
- elsif @hash["InvoiceID"] != ""
7
- insert_invoice_line
8
- elsif @hash["InvoiceNumber"] != ""
9
- insert_invoice_number_line
10
- elsif @hash["ReceiptNumber"] != ""
11
- insert_receipt_number_line
12
- end
3
+ Keys = [
4
+ "Quantity", "Description", "ChargeType", "VatAmount", "VatRate", "Rate", "ReceiptID", "InvoiceID", "InvoiceNumber", "ReceiptNumber"
5
+ ]
6
+
7
+ Finds = []
8
+
9
+ KFObject = {singular: "line", plural: "lines"}
10
+
11
+ XMLKey = "LineID"
12
+
13
+ def save
14
+ if @hash["ReceiptID"] != ""
15
+ insert_receipt_line
16
+ elsif @hash["InvoiceID"] != ""
17
+ insert_invoice_line
18
+ elsif @hash["InvoiceNumber"] != ""
19
+ insert_invoice_number_line
20
+ elsif @hash["ReceiptNumber"] != ""
21
+ insert_receipt_number_line
13
22
  end
14
-
15
- def to_xml
16
- xml = []
17
- id_line = ""
18
- @hash.keys.each do |key|
19
- if key == "LineID"
20
- id_line = "<#{key}>#{@hash[key]}</#{key}>" unless @hash[key] == "0"
21
- elsif key != "ReceiptID" && key != "InvoiceID" && @hash[key] != ""
22
- xml.push("<#{key}>#{@hash[key]}</#{key}>")
23
- end
24
- end
25
- [id_line, xml.join].join
26
- end
27
-
28
- private
29
-
30
- def blank_object_hash
31
- {"Quantity" => "", "Description" => "", "ChargeType" => "", "VatAmount" => "", "VatRate" => "", "Rate" => "", "ReceiptID" => "", "InvoiceID" => "", "InvoiceNumber" => "", "ReceiptNumber" => "" }
23
+ end
24
+
25
+ def to_xml
26
+ xml = []
27
+ id_line = ""
28
+ @hash.keys.each do |key|
29
+ if key == "LineID"
30
+ id_line = "<#{key}>#{@hash[key]}</#{key}>" unless @hash[key] == "0"
31
+ elsif key != "ReceiptID" && key != "InvoiceID" && @hash[key] != ""
32
+ xml.push("<#{key}>#{@hash[key]}</#{key}>")
33
+ end
32
34
  end
35
+ [id_line, xml.join].join
36
+ end
33
37
 
34
- def insert_receipt_line
35
- KashflowApi.api.insert_receipt_line(self)
36
- end
38
+ private
39
+
40
+ def insert_receipt_line
41
+ KashflowApi.api.insert_receipt_line(self)
42
+ end
37
43
 
38
- def insert_invoice_line
39
- KashflowApi.api.insert_invoice_line(self)
40
- end
44
+ def insert_invoice_line
45
+ KashflowApi.api.insert_invoice_line(self)
46
+ end
41
47
 
42
- def insert_invoice_number_line
43
- KashflowApi.api.insert_invoice_line_with_invoice_number(self)
44
- end
48
+ def insert_invoice_number_line
49
+ KashflowApi.api.insert_invoice_line_with_invoice_number(self)
50
+ end
45
51
 
46
- def insert_receipt_number_line
47
- KashflowApi.api.insert_receipt_line_from_number(self)
48
- end
52
+ def insert_receipt_number_line
53
+ KashflowApi.api.insert_receipt_line_from_number(self)
54
+ end
49
55
  end
50
56
  end
@@ -1,12 +1,13 @@
1
1
  module KashflowApi
2
2
  class NominalCode < KashflowApi::SoapObject
3
- def self.all
4
- result = KashflowApi.api.get_nominal_codes
5
- codes = []
6
- result.hash[:envelope][:body][:get_nominal_codes_response][:get_nominal_codes_result][:nominal_code].each do |code|
7
- codes.push(self.build_from_soap(code))
8
- end
9
- codes
10
- end
3
+ Keys = [
4
+ "Code", "Name", "Debit", "Credit", "Balance"
5
+ ]
6
+
7
+ Finds = []
8
+
9
+ KFObject = { singular: "nominal_code", plural: "nominal_codes" }
10
+
11
+ XMLKey = "NominalCodeID"
11
12
  end
12
13
  end
@@ -1,14 +1,18 @@
1
1
  module KashflowApi
2
- class Quote < KashflowApi::Invoice
3
- def self.find_by_id(search)
4
- result = KashflowApi.api.get_quote_by_id(search)
5
- self.build_from_soap(result.hash[:envelope][:body][:get_quote_by_id_response][:get_quote_by_id_result])
6
- end
7
-
8
- private
9
-
10
- def blank_object_hash
11
- {}.merge(KashflowApi::Quote.find_by_id("0").hash)
12
- end
2
+ class Quote < KashflowApi::Invoice
3
+
4
+ KFObject = {singular: "quote", plural: "quotes"}
5
+
6
+ def self.build_arguments(action, object, field, argument)
7
+ if action == "get"
8
+ expects argument, String
9
+ return "<Quote#{field}>#{argument}</Quote#{field}>" if object == "quote"
10
+ elsif action == "update" || action == "insert"
11
+ expects argument, KashflowApi::Quote
12
+ return "<InvoiceID>#{argument.invoiceid}</InvoiceID><InvLine>#{argument.to_xml}</InvLine>" if field == "Line"
13
+ return "<InvoiceNumber>#{argument.invoicenumber}</InvoiceNumber><InvLine>#{argument.to_xml}</InvLine>" if field == "Number"
14
+ return "<Inv>#{argument.to_xml}</Inv>" if object == "quote"
15
+ end
13
16
  end
17
+ end
14
18
  end
@@ -1,43 +1,41 @@
1
1
  module KashflowApi
2
- class Receipt < KashflowApi::SoapObject
3
- def self.find(search)
4
- result = KashflowApi.api.get_receipt(search)
5
- self.build_from_soap(result.hash[:envelope][:body][:get_receipt_response][:get_receipt_result])
6
- end
7
-
8
- def save
9
- if @hash["InvoiceDBID"] == "0"
10
- insert_receipt
11
- else
12
- update_receipt
13
- end
14
- end
15
-
16
- def to_xml
17
- xml = []
18
- id_line = ""
19
- @hash.keys.each do |key|
20
- if key == "InvoiceDBID"
21
- id_line = "<#{key}>#{@hash[key]}</#{key}>" unless @hash[key] == "0"
22
- else
23
- xml.push("<#{key}>#{@hash[key]}</#{key}>")
24
- end
25
- end
26
- [id_line, xml.join].join
27
- end
28
-
29
- private
2
+ class Receipt < KashflowApi::SoapObject
3
+ Keys = KashflowApi::Invoice::Keys
4
+
5
+ Finds = KashflowApi::Invoice::Finds
6
+
7
+ KFObject = { singular: "receipt", plural: "receipts" }
8
+
9
+ XMLKey = "InvoiceDBID"
10
+
11
+ def self.build_arguments(action, object, field, argument)
12
+ if action == "get"
13
+ expects argument, String
14
+ return "<ReceiptNumber>#{argument}</ReceiptNumber>" if object == "receipt"
15
+ elsif action == "update" || action == "insert"
16
+ expects argument, KashflowApi::Receipt
17
+ return "<ReceiptID>#{argument.receiptid}</ReceiptID><InvLine>#{argument.to_xml}</InvLine>" if field == "Line"
18
+ return "<ReceiptNumber>#{argument.invoicenumber}</ReceiptNumber><InvLine>#{argument.to_xml}</InvLine>" if field == "Number"
19
+ return "<Inv>#{argument.to_xml}</Inv>" if object == "receipt"
20
+ end
21
+ end
22
+
23
+ def save
24
+ if @hash["InvoiceDBID"] == "0"
25
+ insert_receipt
26
+ else
27
+ update_receipt
28
+ end
29
+ end
30
30
 
31
- def blank_object_hash
32
- { "InvoiceNumber" => "", "InvoiceDate" => "", "DueDate" => "", "CustomerID" => "", "CustomerReference" => "", "InvoiceDBID" => "0" }
33
- end
31
+ private
34
32
 
35
- def update_receipt
36
- KashflowApi.api.update_receipt(self)
37
- end
33
+ def update_receipt
34
+ KashflowApi.api.update_receipt(self)
35
+ end
38
36
 
39
- def insert_receipt
40
- KashflowApi.api.insert_receipt(self)
41
- end
37
+ def insert_receipt
38
+ KashflowApi.api.insert_receipt(self)
42
39
  end
40
+ end
43
41
  end
@@ -1,62 +1,45 @@
1
1
  module KashflowApi
2
- class Supplier < KashflowApi::SoapObject
3
- def self.find(search)
4
- self.find_by_supplier_code(search)
5
- end
6
-
7
- def self.find_by_supplier_code(search)
8
- result = KashflowApi.api.get_supplier(search)
9
- self.build_from_soap(result.hash[:envelope][:body][:get_supplier_response][:get_supplier_result])
10
- end
11
-
12
- def self.find_by_supplier_id(search)
13
- result = KashflowApi.api.get_supplier_by_id(search)
14
- self.build_from_soap(result.hash[:envelope][:body][:get_supplier_by_id_response][:get_supplier_by_id_result])
15
- end
16
-
17
- def self.all
18
- result = KashflowApi.api.get_suppliers
19
- suppliers = []
20
- result.hash[:envelope][:body][:get_suppliers_response][:get_suppliers_result][:supplier].each do |supplier|
21
- suppliers.push self.build_from_soap supplier
22
- end
23
- suppliers.sort { |x, y| x.name <=> y.name }
24
- end
25
-
26
- def save
27
- if @hash["SupplierID"] == "0"
28
- insert_supplier
29
- else
30
- update_supplier
31
- end
32
- end
33
-
34
- def to_xml
35
- xml = []
36
- id_line = ""
37
- @hash.keys.each do |key|
38
- if key == "SupplierID"
39
- id_line = "<#{key}>#{@hash[key]}</#{key}>" unless @hash[key] == "0"
40
- else
41
- xml.push("<#{key}>#{@hash[key]}</#{key}>")
42
- end
43
- end
44
- [id_line, xml.join].join
45
- end
46
-
47
- private
48
-
49
- def blank_object_hash
50
- {"Code" => "", "Name" => "", "Contact" => "", "Telephone" => "", "Mobile" => "", "Fax" => "", "Email" => "", "Address1" => "", "Address2" => "",
51
- "Address3" => "", "Address4" => "", "Postcode" => "", "Website" => "", "VATNumber" => "", "Notes" => ""}.merge(KashflowApi::Supplier.find("").hash)
52
- end
53
-
54
- def update_supplier
55
- KashflowApi.api.update_supplier(self)
56
- end
2
+ class Supplier < KashflowApi::SoapObject
3
+ Keys = [
4
+ "Code", "Name", "Contact", "Telephone", "Mobile", "Fax", "Email", "Address1", "Address2", "Address3", "Address4", "Postcode", "Website", "VATNumber", "Notes"
5
+ ]
6
+
7
+ Finds = [ "code", "id" ]
8
+
9
+ KFObject = { singular: "supplier", plural: "suppliers" }
10
+
11
+ XMLKey = "SupplierID"
12
+
13
+ def self.build_arguments(action, object, field, argument)
14
+ if action == "get"
15
+ expects argument, String
16
+ return "<Supplier#{field}>#{argument}</Supplier#{field}>" if object == "supplier"
17
+ return "<#{field}>#{argument}</#{field}>" if object == "suppliers"
18
+ elsif action == "update"
19
+ expects argument, KashflowApi::Supplier
20
+ return "<sup>#{argument.to_xml}</sup>" if object == "supplier"
21
+ elsif action == "insert"
22
+ expects argument, KashflowApi::Supplier
23
+ return "<supl>#{argument.to_xml}</supl>" if object == "supplier"
24
+ end
25
+ end
26
+
27
+ def save
28
+ if @hash["SupplierID"] == "0"
29
+ insert_supplier
30
+ else
31
+ update_supplier
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def update_supplier
38
+ KashflowApi.api.update_supplier(self)
39
+ end
57
40
 
58
- def insert_supplier
59
- KashflowApi.api.insert_supplier(self)
60
- end
41
+ def insert_supplier
42
+ KashflowApi.api.insert_supplier(self)
61
43
  end
44
+ end
62
45
  end
@@ -1,40 +1,82 @@
1
1
  module KashflowApi
2
- class SoapObject
3
- attr_accessor :hash
4
-
5
- def initialize(hash = nil)
6
- if hash
7
- @hash = hash
8
- build_field_hash
9
- else
10
- @hash = blank_object_hash
11
- build_field_hash
12
- end
13
- end
2
+ class SoapObject
3
+ include Expects
4
+
5
+ attr_reader :hash
6
+
7
+ def initialize(hash = nil)
8
+ expects(hash, Hash) if hash
9
+ @hash = (hash || new_object_hash)
10
+ init_class
11
+ end
12
+
13
+ def self.inherited(klass)
14
+ KashflowApi.add_soap_object(klass)
15
+ end
16
+
17
+ def self.all
18
+ result = KashflowApi.api.send("get_#{self::KFObject[:plural]}".to_sym)
19
+ results = []
20
+ 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|
21
+ results.push self.new result
22
+ end
23
+ return results
24
+ end
25
+
26
+ def self.find(search)
27
+ self.send(find_method, search)
28
+ end
29
+
30
+ def self.find_method
31
+ "find_by_#{self::Finds.first}".to_sym
32
+ end
33
+
34
+ def self.define_methods
35
+ self::Finds.each do |find|
36
+ method_name = "find_by_#{find}".to_sym
37
+ result_name = find == self::Finds.first ? "get_#{self::KFObject[:singular]}" : "get_#{self::KFObject[:singular]}_by_#{find}"
14
38
 
15
- def method_missing(method, set = nil)
16
- if @fields.keys.include? method
17
- @hash[@fields[method]]
18
- elsif method.to_s.scan(/.$/).join == "="
19
- if @fields.keys.include? method.to_s.gsub(/\=/,'').to_sym
20
- @hash[@fields[method.to_s.gsub(/\=/,'').to_sym]] = set
21
- end
39
+ self.define_singleton_method(method_name, Proc.new { |search|
40
+ result = KashflowApi.api.send(result_name.to_sym, search)
41
+ hash = result.hash[:envelope][:body]["#{result_name}_response".to_sym]["#{result_name}_result".to_sym]
42
+ return self.new(hash)
43
+ })
44
+ end
45
+ end
46
+
47
+ def to_xml
48
+ xml = []
49
+ id_line = ""
50
+ @hash.keys.each do |key|
51
+ if key == self.class::XMLKey
52
+ id_line = "<#{key}>#{@hash[key]}</#{key}>" unless @hash[key] == "0"
22
53
  else
23
- super
54
+ xml.push("<#{key}>#{@hash[key]}</#{key}>")
24
55
  end
25
56
  end
57
+ [id_line, xml.join].join
58
+ end
26
59
 
27
- def self.build_from_soap(hash)
28
- self.new(hash)
60
+ private
61
+
62
+ def new_object_hash
63
+ hash = {}
64
+ self.class::Keys.each do |key|
65
+ key = [*key]
66
+ hash[key.first] = (key.last == key.first ? "" : key.last )
67
+ end
68
+ return hash
69
+ end
70
+
71
+ def init_class
72
+ @hash.keys.each do |key|
73
+ self.class.send(:define_method, key.downcase.to_sym) do
74
+ @hash[key]
29
75
  end
30
-
31
- private
32
-
33
- def build_field_hash
34
- @fields = {}
35
- @hash.keys.each do |key|
36
- @fields[key.downcase.to_sym] = key
37
- end
76
+ self.class.send(:define_method, "#{key.downcase}=".to_sym) do |set|
77
+ @hash[key] = set
38
78
  end
79
+ end
39
80
  end
81
+ end
40
82
  end