business-central 1.0.1 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +58 -15
  3. data/lib/business_central.rb +47 -2
  4. data/lib/business_central/client.rb +82 -39
  5. data/lib/business_central/exceptions.rb +30 -10
  6. data/lib/business_central/object/account.rb +4 -12
  7. data/lib/business_central/object/aged_account_payable.rb +4 -12
  8. data/lib/business_central/object/aged_account_receivable.rb +4 -12
  9. data/lib/business_central/object/argument_helper.rb +22 -0
  10. data/lib/business_central/object/attachment.rb +30 -0
  11. data/lib/business_central/object/balance_sheet.rb +4 -12
  12. data/lib/business_central/object/base.rb +100 -49
  13. data/lib/business_central/object/cash_flow_statement.rb +4 -12
  14. data/lib/business_central/object/company.rb +9 -2
  15. data/lib/business_central/object/company_information.rb +7 -15
  16. data/lib/business_central/object/country_region.rb +26 -0
  17. data/lib/business_central/object/currency.rb +29 -0
  18. data/lib/business_central/object/customer.rb +34 -0
  19. data/lib/business_central/object/customer_financial_detail.rb +13 -0
  20. data/lib/business_central/object/customer_payment.rb +43 -0
  21. data/lib/business_central/object/customer_payment_journal.rb +25 -0
  22. data/lib/business_central/object/customer_sale.rb +13 -0
  23. data/lib/business_central/object/default_dimension.rb +51 -0
  24. data/lib/business_central/object/dimension.rb +20 -0
  25. data/lib/business_central/object/dimension_line.rb +37 -0
  26. data/lib/business_central/object/dimension_value.rb +28 -0
  27. data/lib/business_central/object/employee.rb +21 -0
  28. data/lib/business_central/object/filter_query.rb +18 -0
  29. data/lib/business_central/object/general_ledger_entry.rb +13 -0
  30. data/lib/business_central/object/income_statement.rb +13 -0
  31. data/lib/business_central/object/irs1099_code.rb +16 -0
  32. data/lib/business_central/object/item.rb +16 -19
  33. data/lib/business_central/object/item_category.rb +16 -0
  34. data/lib/business_central/object/journal.rb +29 -0
  35. data/lib/business_central/object/journal_line.rb +48 -0
  36. data/lib/business_central/object/object_helper.rb +49 -0
  37. data/lib/business_central/object/payment_method.rb +16 -0
  38. data/lib/business_central/object/payment_term.rb +16 -0
  39. data/lib/business_central/object/picture.rb +53 -0
  40. data/lib/business_central/object/purchase_invoice.rb +8 -16
  41. data/lib/business_central/object/purchase_invoice_line.rb +14 -18
  42. data/lib/business_central/object/request.rb +61 -50
  43. data/lib/business_central/object/response.rb +31 -23
  44. data/lib/business_central/object/retained_earning_statement.rb +13 -0
  45. data/lib/business_central/object/sales_credit_memo.rb +20 -0
  46. data/lib/business_central/object/sales_credit_memo_line.rb +30 -0
  47. data/lib/business_central/object/sales_invoice.rb +62 -0
  48. data/lib/business_central/object/sales_invoice_line.rb +30 -0
  49. data/lib/business_central/object/sales_order.rb +20 -0
  50. data/lib/business_central/object/sales_order_line.rb +30 -0
  51. data/lib/business_central/object/sales_quote.rb +20 -0
  52. data/lib/business_central/object/sales_quote_line.rb +30 -0
  53. data/lib/business_central/object/shipment_method.rb +16 -0
  54. data/lib/business_central/object/subscription.rb +16 -0
  55. data/lib/business_central/object/tax_area.rb +16 -0
  56. data/lib/business_central/object/tax_group.rb +16 -0
  57. data/lib/business_central/object/time_registration_entry.rb +16 -0
  58. data/lib/business_central/object/trial_balance.rb +13 -0
  59. data/lib/business_central/object/units_of_measure.rb +16 -0
  60. data/lib/business_central/object/url_builder.rb +59 -0
  61. data/lib/business_central/object/url_helper.rb +44 -0
  62. data/lib/business_central/object/validation.rb +10 -7
  63. data/lib/business_central/object/vendor.rb +12 -15
  64. data/lib/business_central/object/vendor_purchase.rb +13 -0
  65. data/lib/business_central/version.rb +4 -2
  66. data/lib/business_central/web_service.rb +78 -0
  67. data/lib/core_ext/string.rb +18 -12
  68. data/test/business_central/client_test.rb +49 -7
  69. data/test/business_central/object/account_test.rb +6 -5
  70. data/test/business_central/object/aged_account_payable_test.rb +6 -5
  71. data/test/business_central/object/aged_account_receivable_test.rb +6 -5
  72. data/test/business_central/object/attachment_test.rb +117 -0
  73. data/test/business_central/object/balance_sheet_test.rb +6 -5
  74. data/test/business_central/object/base_test.rb +41 -0
  75. data/test/business_central/object/cash_flow_statement_test.rb +6 -5
  76. data/test/business_central/object/company_information_test.rb +9 -10
  77. data/test/business_central/object/company_test.rb +6 -5
  78. data/test/business_central/object/country_region_test.rb +129 -0
  79. data/test/business_central/object/currency_test.rb +150 -0
  80. data/test/business_central/object/customer_financial_detail_test.rb +68 -0
  81. data/test/business_central/object/customer_payment_journal_test.rb +134 -0
  82. data/test/business_central/object/customer_payment_test.rb +141 -0
  83. data/test/business_central/object/customer_sale_test.rb +72 -0
  84. data/test/business_central/object/customer_test.rb +168 -0
  85. data/test/business_central/object/default_dimension_test.rb +170 -0
  86. data/test/business_central/object/dimension_line_test.rb +165 -0
  87. data/test/business_central/object/dimension_test.rb +65 -0
  88. data/test/business_central/object/dimension_value_test.rb +68 -0
  89. data/test/business_central/object/employee_test.rb +141 -0
  90. data/test/business_central/object/filter_query_test.rb +36 -0
  91. data/test/business_central/object/general_ledger_entry_test.rb +82 -0
  92. data/test/business_central/object/income_statement_test.rb +82 -0
  93. data/test/business_central/object/irs1099_code_test.rb +124 -0
  94. data/test/business_central/object/item_category_test.rb +122 -0
  95. data/test/business_central/object/item_test.rb +16 -17
  96. data/test/business_central/object/journal_line_test.rb +152 -0
  97. data/test/business_central/object/journal_test.rb +144 -0
  98. data/test/business_central/object/payment_method_test.rb +134 -0
  99. data/test/business_central/object/payment_term_test.rb +134 -0
  100. data/test/business_central/object/picture_test.rb +74 -0
  101. data/test/business_central/object/purchase_invoice_line_test.rb +15 -15
  102. data/test/business_central/object/purchase_invoice_test.rb +15 -15
  103. data/test/business_central/object/request_test.rb +75 -12
  104. data/test/business_central/object/response_test.rb +34 -9
  105. data/test/business_central/object/retained_earning_statement_test.rb +52 -0
  106. data/test/business_central/object/sales_credit_memo_line_test.rb +134 -0
  107. data/test/business_central/object/sales_credit_memo_test.rb +146 -0
  108. data/test/business_central/object/sales_invoice_line_test.rb +164 -0
  109. data/test/business_central/object/sales_invoice_test.rb +156 -0
  110. data/test/business_central/object/sales_order_line_test.rb +134 -0
  111. data/test/business_central/object/sales_order_test.rb +146 -0
  112. data/test/business_central/object/sales_quote_line_test.rb +134 -0
  113. data/test/business_central/object/sales_quote_test.rb +146 -0
  114. data/test/business_central/object/shipment_method_test.rb +133 -0
  115. data/test/business_central/object/subscription_test.rb +126 -0
  116. data/test/business_central/object/tax_area_test.rb +126 -0
  117. data/test/business_central/object/tax_group_test.rb +126 -0
  118. data/test/business_central/object/time_registration_entry_test.rb +126 -0
  119. data/test/business_central/object/trial_balance_test.rb +82 -0
  120. data/test/business_central/object/units_of_measure_test.rb +126 -0
  121. data/test/business_central/object/validation_test.rb +5 -4
  122. data/test/business_central/object/vendor_purchase_test.rb +82 -0
  123. data/test/business_central/object/vendor_test.rb +16 -16
  124. data/test/business_central/web_service_test.rb +100 -0
  125. data/test/business_central_test.rb +3 -1
  126. data/test/test_helper.rb +16 -6
  127. metadata +188 -37
  128. data/lib/business_central/object/helper.rb +0 -15
@@ -1,21 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BusinessCentral
2
4
  module Object
3
5
  class Account < Base
4
- OBJECT = 'accounts'.freeze
6
+ OBJECT = 'accounts'
5
7
 
6
8
  OBJECT_METHODS = [
7
9
  :get
8
10
  ].freeze
9
-
10
- def initialize(client, company_id:)
11
- super(client, company_id: company_id)
12
- @parent_path = [
13
- {
14
- path: 'companies',
15
- id: company_id
16
- }
17
- ]
18
- end
19
11
  end
20
12
  end
21
- end
13
+ end
@@ -1,21 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BusinessCentral
2
4
  module Object
3
5
  class AgedAccountPayable < Base
4
- OBJECT = 'agedAccountsPayable'.freeze
6
+ OBJECT = 'agedAccountsPayable'
5
7
 
6
8
  OBJECT_METHODS = [
7
9
  :get
8
10
  ].freeze
9
-
10
- def initialize(client, company_id:)
11
- super(client, company_id: company_id)
12
- @parent_path = [
13
- {
14
- path: 'companies',
15
- id: company_id
16
- }
17
- ]
18
- end
19
11
  end
20
12
  end
21
- end
13
+ end
@@ -1,21 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BusinessCentral
2
4
  module Object
3
5
  class AgedAccountReceivable < Base
4
- OBJECT = 'agedAccountsReceivable'.freeze
6
+ OBJECT = 'agedAccountsReceivable'
5
7
 
6
8
  OBJECT_METHODS = [
7
9
  :get
8
10
  ].freeze
9
-
10
- def initialize(client, company_id:)
11
- super(client, company_id: company_id)
12
- @parent_path = [
13
- {
14
- path: 'companies',
15
- id: company_id
16
- }
17
- ]
18
- end
19
11
  end
20
12
  end
21
- end
13
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BusinessCentral
4
+ module Object
5
+ module ArgumentHelper
6
+ def id(argument)
7
+ return @id if instance_variable_defined?('@id') && !@id.nil?
8
+ return argument.fetch(:id, '') if !argument.nil?
9
+
10
+ nil
11
+ end
12
+
13
+ def company_id(argument)
14
+ return @client.default_company_id if !@client.default_company_id.nil?
15
+ return @company_id if instance_variable_defined?('@company_id') && !@company_id.nil?
16
+ return argument.fetch(:company_id, '') if !argument.nil?
17
+
18
+ nil
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BusinessCentral
4
+ module Object
5
+ class Attachment < Base
6
+ OBJECT = 'attachments'
7
+
8
+ OBJECT_METHODS = %i[
9
+ get
10
+ post
11
+ patch
12
+ delete
13
+ ].freeze
14
+
15
+ def update(parent_id:, attachment_id:, **params)
16
+ url = "#{build_url(child_path: OBJECT)}(parentId=#{parent_id},id=#{attachment_id})/content"
17
+ Request.call(:patch, @client, url, etag: '', params: {}) do |request|
18
+ request['Content-Type'] = 'application/json'
19
+ request['If-Match'] = 'application/json'
20
+ request.body = Request.convert(params)
21
+ end
22
+ end
23
+
24
+ def destroy(parent_id:, attachment_id:)
25
+ url = "#{build_url(child_path: OBJECT)}(#{parent_id},#{attachment_id})"
26
+ Request.call(:delete, @client, url, etag: '')
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,21 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BusinessCentral
2
4
  module Object
3
5
  class BalanceSheet < Base
4
- OBJECT = 'balanceSheet'.freeze
6
+ OBJECT = 'balanceSheet'
5
7
 
6
8
  OBJECT_METHODS = [
7
9
  :get
8
10
  ].freeze
9
-
10
- def initialize(client, company_id:)
11
- super(client, company_id: company_id)
12
- @parent_path = [
13
- {
14
- path: 'companies',
15
- id: company_id
16
- }
17
- ]
18
- end
19
11
  end
20
12
  end
21
- end
13
+ end
@@ -1,72 +1,127 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BusinessCentral
2
4
  module Object
3
5
  class Base
4
- attr_reader :client,
5
- :company_id,
6
- :parent_path,
7
- :path,
8
- :errors
6
+ include ArgumentHelper
7
+
8
+ attr_reader :client, :parent_path, :path
9
+
10
+ attr_writer :id, :company_id
9
11
 
10
12
  def initialize(client, args = {})
11
13
  @client = client
12
- @company_id = args[:company_id] if !args.nil?
13
- @parent_path = []
14
- @path = ''
15
- @errors = []
14
+ @id = id(args)
15
+ @company_id = company_id(args)
16
+ @parent_path = @company_id.nil? ? [] : [
17
+ {
18
+ path: 'companies',
19
+ id: @company_id
20
+ }
21
+ ]
16
22
  end
17
23
 
18
24
  def find_all
19
- if method_supported?(:get)
20
- Request.get(@client, build_url(parent_path: @parent_path, child_path: object_name))
21
- else
25
+ if !method_supported?(:get)
22
26
  raise BusinessCentral::NoSupportedMethod.new(:get, object_methods)
23
27
  end
28
+
29
+ Request.get(@client, build_url(parent_path: @parent_path, child_path: object_name))
24
30
  end
31
+ alias all find_all
25
32
 
26
33
  def find_by_id(id)
27
- if method_supported?(:get)
28
- Request.get(@client, build_url(parent_path: @parent_path, child_path: object_name, child_id: id))
29
- else
34
+ if !method_supported?(:get)
30
35
  raise BusinessCentral::NoSupportedMethod.new(:get, object_methods)
31
36
  end
37
+
38
+ Request.get(
39
+ @client,
40
+ build_url(
41
+ parent_path: @parent_path,
42
+ child_path: object_name,
43
+ child_id: id
44
+ )
45
+ )
32
46
  end
47
+ alias find find_by_id
33
48
 
34
- def where(query = '')
35
- if method_supported?(:get)
36
- Request.get(@client, build_url(parent_path: @parent_path, child_path: object_name, filter: query))
37
- else
49
+ def where(query = '', *values)
50
+ if !method_supported?(:get)
38
51
  raise BusinessCentral::NoSupportedMethod.new(:get, object_methods)
39
52
  end
53
+
54
+ Request.get(
55
+ @client,
56
+ build_url(
57
+ parent_path: @parent_path,
58
+ child_path: object_name,
59
+ filter: FilterQuery.sanitize(query, values)
60
+ )
61
+ )
40
62
  end
41
63
 
42
64
  def create(params = {})
43
- if method_supported?(:post)
44
- if Validation.new(object_validation, params).valid?
45
- Request.post(@client, build_url(parent_path: @parent_path, child_path: object_name), params)
46
- end
47
- else
65
+ if !method_supported?(:post)
48
66
  raise BusinessCentral::NoSupportedMethod.new(:post, object_methods)
49
67
  end
68
+
69
+ Validation.new(object_validation, params).valid?
70
+ Request.post(@client, build_url(parent_path: @parent_path, child_path: object_name), params)
50
71
  end
51
72
 
52
73
  def update(id, params = {})
53
- if method_supported?(:patch)
54
- object = find_by_id(id)
55
- if Validation.new(object_validation, params).valid?
56
- Request.patch(@client, build_url(parent_path: @parent_path, child_path: object_name, child_id: id), object[:etag], params)
57
- end
58
- else
74
+ if !method_supported?(:patch)
59
75
  raise BusinessCentral::NoSupportedMethod.new(:patch, object_methods)
60
76
  end
77
+
78
+ object = find_by_id(id).merge(params)
79
+ Validation.new(object_validation, object).valid?
80
+ Request.patch(
81
+ @client,
82
+ build_url(
83
+ parent_path: @parent_path,
84
+ child_path: object_name,
85
+ child_id: id
86
+ ),
87
+ object[:etag],
88
+ params
89
+ )
61
90
  end
62
91
 
63
92
  def destroy(id)
64
- if method_supported?(:delete)
65
- object = find_by_id(id)
66
- Request.delete(@client, build_url(parent_path: @parent_path, child_path: object_name, child_id: id), object[:etag])
67
- else
93
+ if !method_supported?(:delete)
68
94
  raise BusinessCentral::NoSupportedMethod.new(:delete, object_methods)
69
95
  end
96
+
97
+ object = find_by_id(id)
98
+ Request.delete(
99
+ @client,
100
+ build_url(
101
+ parent_path: @parent_path,
102
+ child_path: object_name,
103
+ child_id: id
104
+ ),
105
+ object[:etag]
106
+ )
107
+ end
108
+
109
+ protected
110
+
111
+ def valid_parent?(parent)
112
+ return true if object_parent_name.map(&:downcase).include?(parent.downcase)
113
+
114
+ raise InvalidArgumentException, "parents allowed: #{object_parent_name.join(', ')}"
115
+ end
116
+
117
+ def build_url(parent_path: [], child_path: '', child_id: '', filter: '')
118
+ URLBuilder.new(
119
+ base_url: client.url,
120
+ parent_path: parent_path,
121
+ child_path: child_path,
122
+ child_id: child_id,
123
+ filter: filter
124
+ ).build
70
125
  end
71
126
 
72
127
  private
@@ -76,30 +131,26 @@ module BusinessCentral
76
131
  end
77
132
 
78
133
  def object_validation
79
- self.class.const_defined?(:OBJECT_VALIDATION) ? self.class.const_get(:OBJECT_VALIDATION) : []
134
+ if self.class.const_defined?(:OBJECT_VALIDATION)
135
+ self.class.const_get(:OBJECT_VALIDATION)
136
+ else
137
+ []
138
+ end
80
139
  end
81
140
 
82
141
  def object_methods
83
142
  self.class.const_get(:OBJECT_METHODS)
84
143
  end
85
144
 
86
- def method_supported?(method)
87
- return true if object_methods.include?(method)
88
- return false
145
+ def object_parent_name
146
+ self.class.const_get(:OBJECT_PARENTS)
89
147
  end
90
148
 
91
- def build_url(parent_path: [], child_path: '', child_id: '', filter: '')
92
- url_builder(parent_path, child_path, child_id, filter)
93
- end
149
+ def method_supported?(method)
150
+ return true if object_methods.include?(method)
94
151
 
95
- def url_builder(parent_path = [], child_path = '', child_id = '', filter = '')
96
- url = @client.url
97
- url += parent_path.map { |parent| "/#{parent[:path]}(#{parent[:id]})" }.join('') if !parent_path.empty?
98
- url += "/#{child_path}" if !child_path.blank?
99
- url += "(#{child_id})" if !child_id.blank?
100
- url += "?$filter=#{filter}" if !filter.blank?
101
- return url
152
+ false
102
153
  end
103
154
  end
104
155
  end
105
- end
156
+ end
@@ -1,21 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BusinessCentral
2
4
  module Object
3
5
  class CashFlowStatement < Base
4
- OBJECT = 'cashFlowStatement'.freeze
6
+ OBJECT = 'cashFlowStatement'
5
7
 
6
8
  OBJECT_METHODS = [
7
9
  :get
8
10
  ].freeze
9
-
10
- def initialize(client, company_id:)
11
- super(client, company_id: company_id)
12
- @parent_path = [
13
- {
14
- path: 'companies',
15
- id: company_id
16
- }
17
- ]
18
- end
19
11
  end
20
12
  end
21
- end
13
+ end
@@ -1,11 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BusinessCentral
2
4
  module Object
3
5
  class Company < Base
4
- OBJECT = 'companies'.freeze
6
+ OBJECT = 'companies'
5
7
 
6
8
  OBJECT_METHODS = [
7
9
  :get
8
10
  ].freeze
11
+
12
+ def initialize(client, _args)
13
+ super(client)
14
+ @parent_path = []
15
+ end
9
16
  end
10
17
  end
11
- end
18
+ end
@@ -1,22 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BusinessCentral
2
4
  module Object
3
5
  class CompanyInformation < Base
4
- OBJECT = 'companyInformation'.freeze
6
+ OBJECT = 'companyInformation'
5
7
 
6
- OBJECT_METHODS = [
7
- :get,
8
- :patch
8
+ OBJECT_METHODS = %i[
9
+ get
10
+ patch
9
11
  ].freeze
10
-
11
- def initialize(client, company_id:)
12
- super(client, company_id: company_id)
13
- @parent_path = [
14
- {
15
- path: 'companies',
16
- id: company_id
17
- }
18
- ]
19
- end
20
12
  end
21
13
  end
22
- end
14
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BusinessCentral
4
+ module Object
5
+ class CountryRegion < Base
6
+ OBJECT = 'countriesRegions'
7
+
8
+ OBJECT_VALIDATION = {
9
+ code: {
10
+ required: true
11
+ },
12
+ display_name: {
13
+ required: true,
14
+ maximum_length: 100
15
+ }
16
+ }.freeze
17
+
18
+ OBJECT_METHODS = %i[
19
+ get
20
+ post
21
+ patch
22
+ delete
23
+ ].freeze
24
+ end
25
+ end
26
+ end