kashflow_api 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 80713598b9cc6087bc6f5c8fb1e8dddbc08b8638
4
- data.tar.gz: 6028013836e547246697247fc46c2158c6f285af
3
+ metadata.gz: 83aa2d3d9328488253625f5a8c757a8c15f284b2
4
+ data.tar.gz: 8165b45aeac0f6c38dde74a7000032ba4efb8dcb
5
5
  SHA512:
6
- metadata.gz: 444553516b639eb4b7b8df00a8bdedc21c27f53361c03f1c9ca2f4c9ca3a78f5230665e35f7baf651d57e4dca2cf2d3dffb0ecba5962bbc6887b3d40963c4443
7
- data.tar.gz: 91b2b8e5790b87199677dfe67b5b436a8424d591995917f8538c93f7d8fa70a3976d35dddf04ca27b355dd5a4b6abd1d4d0624d33d93aa8476023da41b4b6fd4
6
+ metadata.gz: f387a0516f4edd4600f49da3d8cdbd5dfbbaebffcb597a0f49e0d50c32fbcd254af25b951e55258303f63e0d468678a322737bd1a286ab2f8f81c981f475015a
7
+ data.tar.gz: 3a2fc5bad8acdcc8494fea8587002b8bae8b90ccdd2c0cf55da4fafe90d71d10a68f18093ab8b123a329a686134b8d3b68c14b42486e77b7526a5d065bff80ac
data/.gitignore CHANGED
@@ -4,3 +4,4 @@ Gemfile.lock
4
4
  pkg/*
5
5
  spec/test_data.yml
6
6
  coverage/*
7
+ spec-old/test_data.yml
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - rbx-19mode
6
+ - ruby-head
7
+ env:
8
+ - "rake=0.9"
9
+ script: "bundle exec rspec -c"
10
+ notifications:
11
+ email: false
12
+ matrix:
13
+ allow_failures:
14
+ - rvm: ruby-head
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source "http://rubygems.org"
2
2
 
3
+ gem 'coveralls', require: false
4
+
3
5
  # Specify your gem's dependencies in kashflow_api.gemspec
4
6
  gemspec
data/README.markdown CHANGED
@@ -1,23 +1,45 @@
1
1
  # KashflowApi
2
2
 
3
+ [![Build Status](https://travis-ci.org/Ed-ITSolutions/KashflowAPI.png?branch=master)](https://travis-ci.org/Ed-ITSolutions/KashflowAPI) [![Code Climate](https://codeclimate.com/github/Ed-ITSolutions/KashflowAPI.png)](https://codeclimate.com/github/Ed-ITSolutions/KashflowAPI) [![Coverage Status](https://coveralls.io/repos/Ed-ITSolutions/KashflowAPI/badge.png)](https://coveralls.io/r/Ed-ITSolutions/KashflowAPI)
4
+
3
5
  KashflowApi provides an Active Record like interface to the Kashflow API.
4
6
 
7
+ # Install
8
+
9
+ You can install KashflowApi by adding it yo your `Gemfile` like this:
10
+
11
+ ```ruby
12
+ gem 'kashflow_api'
13
+ ```
14
+
15
+ and running `bundle install`
16
+
5
17
  # Usage
6
18
 
7
19
  At the beginning of your program, or in an rails initializer call the configure block like so:
8
20
 
9
- KashflowApi.configure do |c|
10
- c.username = "Username"
11
- c.password = "Password"
12
- c.loggers = false
13
- end
21
+ ``` ruby
22
+ KashflowApi.configure do |c|
23
+ c.username = "Username"
24
+ c.password = "Password"
25
+ c.loggers = false
26
+ end
27
+ ```
14
28
 
15
29
  I recommend settings loggers to false so that you don't get all the soap exchanges echoed out.
16
30
 
17
31
  You can now call methods on the models e.g.
18
32
 
19
- KashflowApi::Customer.all
33
+ ``` ruby
34
+ KashflowApi::Customer.all
35
+ KashflowApi::Customer.find("Cust01")
36
+ KashflowApi::Customer.find_by_email("customer@domain.tld")
37
+ ```
38
+
39
+ ## Saving
40
+
41
+ All the Kashflow objects in the gem have a `.save` method which will either insert a new record or update the current one.
20
42
 
21
43
  # More Info
22
44
 
23
- For more info head over to the [wiki][https://github.com/Ed-ITSolutions/KashflowAPI/wiki]
45
+ For more info head over to the [wiki](https://github.com/Ed-ITSolutions/KashflowAPI/wiki)
data/kashflow_api.gemspec CHANGED
@@ -18,8 +18,8 @@ Gem::Specification.new do |s|
18
18
 
19
19
  # Development Dependencies
20
20
  s.add_development_dependency "rspec"
21
- s.add_development_dependency "simplecov"
22
21
 
23
22
  # Runtime Dependencies
24
23
  s.add_runtime_dependency "savon", "~> 2.3"
24
+ s.add_runtime_dependency "expects", "~> 0.0.3"
25
25
  end
data/lib/kashflow_api.rb CHANGED
@@ -1,5 +1,40 @@
1
1
  # Gems
2
+ require 'expects'
2
3
  require "savon"
4
+
5
+ module KashflowApi
6
+ def self.configure
7
+ @config = Config.new
8
+ yield @config
9
+ @api = Api.new
10
+ end
11
+
12
+ def self.config
13
+ @config
14
+ end
15
+
16
+ def self.api
17
+ @api
18
+ end
19
+
20
+ def self.api_methods
21
+ @api_methods ||= Api.method_list
22
+ end
23
+
24
+ def self.client
25
+ @client ||= Client.new(@config)
26
+ end
27
+
28
+ def self.soap_objects
29
+ @soap_objects ||= []
30
+ end
31
+
32
+ def self.add_soap_object(klass)
33
+ @soap_objects ||= []
34
+ @soap_objects.push klass
35
+ end
36
+ end
37
+
3
38
  # Kashflow Main Clases
4
39
  require "kashflow_api/soap_object" # Soap Object Class
5
40
  require "kashflow_api/config" # Config Class
@@ -18,27 +53,3 @@ require "kashflow_api/models/receipt" # Receipt Class
18
53
  require "kashflow_api/models/supplier" # Supplier
19
54
  # Version
20
55
  require "kashflow_api/version"
21
-
22
- module KashflowApi
23
- def self.configure
24
- @config = Config.new
25
- yield @config
26
- @api = Api.new
27
- end
28
-
29
- def self.config
30
- @config
31
- end
32
-
33
- def self.api
34
- @api
35
- end
36
-
37
- def self.api_methods
38
- @api_methods ||= Api.methods
39
- end
40
-
41
- def self.client
42
- @client ||= Client.new(@config)
43
- end
44
- end
@@ -1,29 +1,29 @@
1
1
  module KashflowApi
2
- class Api
3
- def initialize
4
- unless KashflowApi.config.username && KashflowApi.config.password
5
- raise "Username and Password required"
6
- end
7
- end
8
-
9
- def self.methods
10
- @methods ||= generate_method_list
11
- end
12
-
13
- # Main Handler
14
- def method_missing(method, argument = nil)
15
- methods = KashflowApi.api_methods
16
- if methods.include?(method)
17
- KashflowApi::ApiCall.new(method, argument).result
18
- else
19
- super
20
- end
21
- end
22
-
23
- private
24
-
25
- def self.generate_method_list
26
- KashflowApi.client.client.operations
2
+ class Api
3
+ def initialize
4
+ unless KashflowApi.config.username && KashflowApi.config.password
5
+ raise "Username and Password required"
6
+ end
7
+
8
+ init_class
9
+ end
10
+
11
+ def self.method_list
12
+ @methods ||= generate_method_list
13
+ end
14
+
15
+ private
16
+
17
+ def init_class
18
+ KashflowApi.api_methods.each do |method|
19
+ self.class.send(:define_method, method) do |argument = nil|
20
+ KashflowApi::ApiCall.new(method, argument).make_call
27
21
  end
22
+ end
23
+ end
24
+
25
+ def self.generate_method_list
26
+ KashflowApi.client.client.operations
28
27
  end
28
+ end
29
29
  end
@@ -6,8 +6,11 @@ module KashflowApi
6
6
  @method_sym = method.to_sym
7
7
  set_method(method)
8
8
  build_xml(argument)
9
- raise xml if @raise
10
- @result = make_call
9
+ return self
10
+ end
11
+
12
+ def make_call
13
+ KashflowApi.client.call(self)
11
14
  end
12
15
 
13
16
  private
@@ -48,60 +51,21 @@ module KashflowApi
48
51
  <Password>#{KashflowApi.config.password}</Password>\n"
49
52
  end
50
53
 
51
- def make_call
52
- KashflowApi.client.call(self)
53
- end
54
-
55
54
  def argument_xml(argument)
56
- out = ""
57
- if argument
58
- if @action == "get"
59
- if @object == "customer"
60
- out = "<Customer#{@field}>#{argument}</Customer#{@field}>"
61
- elsif @object == "customers"
62
- out = "<#{@field}>#{argument}</#{@field}>"
63
- elsif @object == "quote"
64
- out = "<Quote#{@field}>#{argument}</Quote#{@field}>"
65
- elsif @object == "supplier"
66
- out = "<Supplier#{@field}>#{argument}</Supplier#{@field}>"
67
- elsif @object == "receipt"
68
- out = "<ReceiptNumber>#{argument}</ReceiptNumber>"
69
- elsif @object == "invoice"
70
- out = "<InvoiceNumber>#{argument}</InvoiceNumber>"
71
- end
72
- elsif @action == "update" || @action == "insert"
73
- if @object == "customer"
74
- out = "<custr>#{argument.to_xml}</custr>"
75
- elsif @object == "supplier"
76
- if @action == "insert"
77
- out = "<supl>#{argument.to_xml}</supl>"
78
- else
79
- out = "<sup>#{argument.to_xml}</sup>"
80
- end
81
- elsif @object == "receipt"
82
- if @field == "Line"
83
- out = "<ReceiptID>#{argument.receiptid}</ReceiptID><InvLine>#{argument.to_xml}</InvLine>"
84
- elsif @field == "Number"
85
- out = "<ReceiptNumber>#{argument.receiptnumber}</ReceiptNumber><InvLine>#{argument.to_xml}</InvLine>"
86
- else
87
- out = "<Inv>#{argument.to_xml}</Inv>"
88
- end
89
- elsif @object == "invoice"
90
- if @field == "Line"
91
- out = "<InvoiceID>#{argument.invoiceid}</InvoiceID><InvLine>#{argument.to_xml}</InvLine>"
92
- elsif @field == "Number"
93
- out = "<InvoiceNumber>#{argument.invoicenumber}</InvoiceNumber><InvLine>#{argument.to_xml}</InvLine>"
94
- else
95
- out = "<Inv>#{argument.to_xml}</Inv>"
96
- end
97
- end
98
- elsif @action == "delete"
99
- if @object == "customer"
100
- out = "<CustomerID>#{argument}</CustomerID>"
101
- end
102
- end
103
- end
104
- out
55
+ case @object
56
+ when "customer" || "customers"
57
+ return KashflowApi::Customer.build_arguments(@action, @object, @field, argument)
58
+ when "invoice"
59
+ return KashflowApi::Invoice.build_arguments(@action, @object, @field, argument)
60
+ when "quote"
61
+ return KashflowApi::Quote.build_arguments(@action, @object, @field, argument)
62
+ when "receipt"
63
+ return KashflowApi::Receipt.build_arguments(@action, @object, @field, argument)
64
+ when "supplier"
65
+ return KashflowApi::Supplier.build_arguments(@action, @object, @field, argument)
66
+ else
67
+ return ""
68
+ end
105
69
  end
106
70
  end
107
71
  end
@@ -1,25 +1,27 @@
1
1
  module KashflowApi
2
- class Client
3
- def initialize(config)
4
- @config = config
5
- @url = "https://securedwebapp.com/api/service.asmx?WSDL"
6
-
7
- @client = Savon.client(wsdl: @url, log: @config.loggers)
8
- end
9
-
10
- def call(api_call)
11
- #"KashFlow/#{api_call.method}"
12
- #result = @client.xml_call(api_call.method_sym, api_call)
13
- result = @client.call(api_call.method_sym, xml: api_call.xml)
14
- raise "Incorrect username or password" if result.to_hash.first.last[:status_detail] == "Incorrect username or password"
15
- raise "Your IP Address is not in the access list!" if result.to_hash.first.last[:status_detail] =~ /The IP address of .*? is not in the access list/
16
- #raise api_call.xml if result.to_hash.first.last[:status] == "NO"
17
- raise "Kashflow Error: #{result.to_hash.first.last[:status_detail]}" if result.to_hash.first.last[:status] == "NO"
18
- return result
19
- end
20
-
21
- def client
22
- @client
23
- end
2
+ class Client
3
+ include Expects
4
+
5
+ attr_reader :client
6
+
7
+ def initialize(config)
8
+ expects config, KashflowApi::Config
9
+
10
+ @config = config
11
+ @url = "https://securedwebapp.com/api/service.asmx?WSDL"
12
+ @client = Savon.client(wsdl: @url, log: @config.loggers)
24
13
  end
14
+
15
+ def call(api_call)
16
+ result = @client.call(api_call.method_sym, xml: api_call.xml)
17
+ handle_errors(result.to_hash)
18
+ return result
19
+ end
20
+
21
+ def handle_errors(hash)
22
+ raise "Incorrect username or password" if hash.first.last[:status_detail] == "Incorrect username or password"
23
+ raise "Your IP Address is not in the access list!" if hash.first.last[:status_detail] =~ /The IP address of .*? is not in the access list/
24
+ raise "Kashflow Error: #{result.to_hash.first.last[:status_detail]}" if hash.first.last[:status] == "NO"
25
+ end
26
+ end
25
27
  end
@@ -1,86 +1,43 @@
1
1
  module KashflowApi
2
- class Customer < KashflowApi::SoapObject
3
- def self.find(search)
4
- self.find_by_customer_code(search)
5
- end
6
-
7
- def self.find_by_customer_code(search)
8
- result = KashflowApi.api.get_customer(search)
9
- self.build_from_soap(result.hash[:envelope][:body][:get_customer_response][:get_customer_result])
10
- end
11
-
12
- def self.find_by_customer_id(search)
13
- result = KashflowApi.api.get_customer_by_id(search)
14
- self.build_from_soap(result.hash[:envelope][:body][:get_customer_by_id_response][:get_customer_by_id_result])
15
- end
16
-
17
- def self.find_by_customer_email(search)
18
- result = KashflowApi.api.get_customer_by_email(search)
19
- self.build_from_soap(result.hash[:envelope][:body][:get_customer_by_email_response][:get_customer_by_email_result])
20
- end
21
-
22
- def self.find_by_postcode(search)
23
- result = KashflowApi.api.get_customers_by_postcode(search)
24
- self.build_from_soap(result.hash[:envelope][:body][:get_customers_by_postcode_response][:get_customers_by_postcode])
25
- end
26
-
27
- def self.all
28
- result = KashflowApi.api.get_customers
29
- customers = []
30
- result.hash[:envelope][:body][:get_customers_response][:get_customers_result][:customer].each do |customer|
31
- customers.push self.build_from_soap customer
32
- end
33
- customers
34
- end
35
-
36
- def save
37
- if @hash[:customer_id] == "0"
38
- insert_customer
39
- else
40
- update_customer
41
- end
42
- end
43
-
44
- def destroy
45
- KashflowApi.api.delete_customer(self.customer_id)
46
- end
47
-
48
- def balance
49
- KashflowApi::CustomerBalance.new(self)
50
- end
51
-
52
- def to_xml
53
- xml = []
54
- id_line = ""
55
- @hash.keys.each do |key|
56
- if key == :customer_id
57
- id_line = "<CustomerID>#{@hash[key]}</CustomerID>" unless @hash[key] == "0"
58
- else
59
- key_name = key.to_s.split('_').map{|e| e.capitalize}.join
60
- xml.push("<#{key_name}>#{@hash[key]}</#{key_name}>")
61
- end
62
- end
63
- [id_line, xml.join].join
64
- end
65
-
66
- private
67
-
68
- def blank_object_hash
69
- {"Code" => "", "Name" => "", "Contact" => "", "Telephone" => "", "Mobile" => "", "Fax" => "", "Email" => "", "Address1" => "", "Address2" => "",
70
- "Address3" => "", "Address4" => "", "Postcode" => "", "Website" => "", "Notes" => "", "ExtraText1" => "", "ExtraText2" => "",
71
- "ExtraText3" => "", "ExtraText4" => "", "ExtraText5" => "", "ExtraText6" => "", "ExtraText7" => "", "ExtraText8" => "", "ExtraText9" => "",
72
- "ExtraText10" => "", "ExtraText11" => "", "ExtraText12" => "", "ExtraText13" => "", "ExtraText14" => "", "ExtraText15" => "",
73
- "ExtraText16" => "", "ExtraText17" => "", "ExtraText18" => "", "ExtraText19" => "", "ExtraText20" => "", "ContactTitle" => "",
74
- "ContactFirstName" => "", "ContactLastName" => "", "CustHasDeliveryAddress" => "0", "DeliveryAddress1" => "",
75
- "DeliveryAddress2" => "", "DeliveryAddress3" => "", "DeliveryAddress4" => "", "DeliveryPostcode" => ""}.merge(KashflowApi::Customer.find("").hash)
76
- end
77
-
78
- def update_customer
79
- KashflowApi.api.update_customer(self)
80
- end
81
-
82
- def insert_customer
83
- KashflowApi.api.insert_customer(self)
84
- end
2
+ class Customer < KashflowApi::SoapObject
3
+
4
+ Keys = [
5
+ "Code", "Name", "Contact", "Telephone", "Mobile", "Fax", "Email", "Address1", "Address2", "Address3", "Address4", "Postcode",
6
+ "Website", "Notes", "ExtraText1", "ExtraText2", "ExtraText3", "ExtraText4", "ExtraText5", "ExtraText6", "ExtraText7", "ExtraText8",
7
+ "ExtraText9", "ExtraText10", "ExtraText11", "ExtraText12", "ExtraText13", "ExtraText14", "ExtraText15", "ExtraText16", "ExtraText17",
8
+ "ExtraText18", "ExtraText19", "ExtraText20", "ContactTitle", "ContactFirstName", "ContactLastName", ["CustHasDeliveryAddresss", "0"],
9
+ "DeliveryAddress1", "DeliveryAddress2", "DeliveryAddress3", "DeliveryAddress4", "DeliveryPostcode"
10
+ ]
11
+
12
+ Finds = [ "code", "id", "email", "postcode" ]
13
+
14
+ KFObject = { singular: "customer", plural: "customers" }
15
+
16
+ XMLKey = "CustomerID"
17
+
18
+ define_methods
19
+
20
+ def self.build_arguments(action, object, field, argument)
21
+ if action == "get"
22
+ expects argument, String
23
+ return "<Customer#{field}>#{argument}</Customer#{field}>" if object == "customer"
24
+ return "<#{field}>#{argument}</#{field}>" if object == "customers"
25
+ elsif action == "update" || action == "insert"
26
+ expects argument, KashflowApi::Customer
27
+ return "<custr>#{argument.to_xml}</custr>" if object == "customer"
28
+ end
29
+ end
30
+
31
+ def save
32
+ if @hash[:customer_id] == "0"
33
+ insert_customer
34
+ else
35
+ update_customer
36
+ end
37
+ end
38
+
39
+ def balance
40
+ KashflowApi::CustomerBalance.new(self)
85
41
  end
86
- end
42
+ end
43
+ end