business-central 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/business_central.rb +35 -2
- data/lib/business_central/client.rb +65 -38
- data/lib/business_central/exceptions.rb +19 -9
- data/lib/business_central/object/account.rb +4 -2
- data/lib/business_central/object/aged_account_payable.rb +4 -2
- data/lib/business_central/object/aged_account_receivable.rb +4 -2
- data/lib/business_central/object/argument_helper.rb +22 -0
- data/lib/business_central/object/balance_sheet.rb +4 -2
- data/lib/business_central/object/base.rb +90 -56
- data/lib/business_central/object/cash_flow_statement.rb +4 -2
- data/lib/business_central/object/company.rb +5 -3
- data/lib/business_central/object/company_information.rb +7 -5
- data/lib/business_central/object/country_region.rb +8 -6
- data/lib/business_central/object/currency.rb +8 -6
- data/lib/business_central/object/customer.rb +15 -9
- data/lib/business_central/object/customer_financial_detail.rb +3 -1
- data/lib/business_central/object/customer_payment.rb +8 -6
- data/lib/business_central/object/customer_payment_journal.rb +8 -6
- data/lib/business_central/object/customer_sale.rb +3 -1
- data/lib/business_central/object/default_dimension.rb +17 -20
- data/lib/business_central/object/dimension.rb +20 -0
- data/lib/business_central/object/dimension_line.rb +37 -0
- data/lib/business_central/object/dimension_value.rb +28 -0
- data/lib/business_central/object/employee.rb +21 -0
- data/lib/business_central/object/filter_query.rb +18 -0
- data/lib/business_central/object/item.rb +17 -10
- data/lib/business_central/object/item_category.rb +16 -0
- data/lib/business_central/object/journal.rb +29 -0
- data/lib/business_central/object/journal_line.rb +48 -0
- data/lib/business_central/object/{helper.rb → object_helper.rb} +18 -10
- data/lib/business_central/object/payment_method.rb +16 -0
- data/lib/business_central/object/payment_term.rb +16 -0
- data/lib/business_central/object/picture.rb +53 -0
- data/lib/business_central/object/purchase_invoice.rb +8 -6
- data/lib/business_central/object/purchase_invoice_line.rb +10 -8
- data/lib/business_central/object/request.rb +60 -49
- data/lib/business_central/object/response.rb +28 -22
- data/lib/business_central/object/retained_earning_statement.rb +13 -0
- data/lib/business_central/object/sales_credit_memo.rb +20 -0
- data/lib/business_central/object/sales_credit_memo_line.rb +30 -0
- data/lib/business_central/object/sales_invoice.rb +62 -0
- data/lib/business_central/object/sales_invoice_line.rb +30 -0
- data/lib/business_central/object/sales_order.rb +20 -0
- data/lib/business_central/object/sales_order_line.rb +30 -0
- data/lib/business_central/object/sales_quote.rb +20 -0
- data/lib/business_central/object/sales_quote_line.rb +30 -0
- data/lib/business_central/object/shipment_method.rb +16 -0
- data/lib/business_central/object/subscription.rb +16 -0
- data/lib/business_central/object/tax_area.rb +16 -0
- data/lib/business_central/object/tax_group.rb +16 -0
- data/lib/business_central/object/time_registration_entry.rb +16 -0
- data/lib/business_central/object/trial_balance.rb +13 -0
- data/lib/business_central/object/units_of_measure.rb +16 -0
- data/lib/business_central/object/url_builder.rb +59 -0
- data/lib/business_central/object/url_helper.rb +32 -0
- data/lib/business_central/object/validation.rb +10 -7
- data/lib/business_central/object/vendor.rb +10 -7
- data/lib/business_central/object/vendor_purchase.rb +13 -0
- data/lib/business_central/version.rb +4 -2
- data/lib/business_central/web_service.rb +78 -0
- data/lib/core_ext/string.rb +18 -12
- data/test/business_central/client_test.rb +18 -11
- data/test/business_central/object/account_test.rb +6 -5
- data/test/business_central/object/aged_account_payable_test.rb +6 -5
- data/test/business_central/object/aged_account_receivable_test.rb +6 -5
- data/test/business_central/object/balance_sheet_test.rb +6 -5
- data/test/business_central/object/base_test.rb +4 -2
- data/test/business_central/object/cash_flow_statement_test.rb +6 -5
- data/test/business_central/object/company_information_test.rb +9 -10
- data/test/business_central/object/company_test.rb +6 -5
- data/test/business_central/object/country_region_test.rb +16 -17
- data/test/business_central/object/currency_test.rb +17 -18
- data/test/business_central/object/customer_financial_detail_test.rb +8 -6
- data/test/business_central/object/customer_payment_journal_test.rb +14 -15
- data/test/business_central/object/customer_payment_test.rb +15 -16
- data/test/business_central/object/customer_sale_test.rb +8 -6
- data/test/business_central/object/customer_test.rb +15 -16
- data/test/business_central/object/default_dimension_test.rb +15 -16
- data/test/business_central/object/dimension_line_test.rb +165 -0
- data/test/business_central/object/dimension_test.rb +65 -0
- data/test/business_central/object/dimension_value_test.rb +68 -0
- data/test/business_central/object/employee_test.rb +141 -0
- data/test/business_central/object/filter_query_test.rb +36 -0
- data/test/business_central/object/item_category_test.rb +122 -0
- data/test/business_central/object/item_test.rb +16 -17
- data/test/business_central/object/journal_line_test.rb +152 -0
- data/test/business_central/object/journal_test.rb +124 -0
- data/test/business_central/object/payment_method_test.rb +134 -0
- data/test/business_central/object/payment_term_test.rb +134 -0
- data/test/business_central/object/picture_test.rb +74 -0
- data/test/business_central/object/purchase_invoice_line_test.rb +15 -15
- data/test/business_central/object/purchase_invoice_test.rb +15 -15
- data/test/business_central/object/request_test.rb +17 -9
- data/test/business_central/object/response_test.rb +9 -7
- data/test/business_central/object/retained_earning_statement_test.rb +52 -0
- data/test/business_central/object/sales_credit_memo_line_test.rb +134 -0
- data/test/business_central/object/sales_credit_memo_test.rb +146 -0
- data/test/business_central/object/sales_invoice_line_test.rb +164 -0
- data/test/business_central/object/sales_invoice_test.rb +134 -0
- data/test/business_central/object/sales_order_line_test.rb +134 -0
- data/test/business_central/object/sales_order_test.rb +146 -0
- data/test/business_central/object/sales_quote_line_test.rb +134 -0
- data/test/business_central/object/sales_quote_test.rb +146 -0
- data/test/business_central/object/shipment_method_test.rb +133 -0
- data/test/business_central/object/subscription_test.rb +126 -0
- data/test/business_central/object/tax_area_test.rb +126 -0
- data/test/business_central/object/tax_group_test.rb +126 -0
- data/test/business_central/object/time_registration_entry_test.rb +126 -0
- data/test/business_central/object/trial_balance_test.rb +82 -0
- data/test/business_central/object/units_of_measure_test.rb +126 -0
- data/test/business_central/object/validation_test.rb +5 -4
- data/test/business_central/object/vendor_purchase_test.rb +82 -0
- data/test/business_central/object/vendor_test.rb +16 -16
- data/test/business_central/web_service_test.rb +95 -0
- data/test/business_central_test.rb +3 -1
- data/test/test_helper.rb +12 -6
- metadata +132 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 68fd68c7e0cf337b5f2c83ba5f08e21cdbab84b3c10a3515338a6d5241ab3c8d
|
4
|
+
data.tar.gz: 14b3bcbe75143b2457c7ca2606a33926de4e89fd7001323c97911d557037301e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bfe06ca5d2427eaed0f8bcb6826e87635d72f02e456e7f4dba47bb19645edb757cc85af0436038f611821975fe43e39387238e180172e3ec09b95720ab45edcb
|
7
|
+
data.tar.gz: 461aadc6e03b1a77bbe6737d69776d2c909baebd36c680a558320abc674f27824983cc1e3b1401b35f85b90fb9ba28f5ff7aaa93a0adbd79e50d8df828532fc6
|
data/lib/business_central.rb
CHANGED
@@ -5,11 +5,16 @@ require 'json'
|
|
5
5
|
|
6
6
|
require 'core_ext/string'
|
7
7
|
|
8
|
-
require 'business_central/object/
|
8
|
+
require 'business_central/object/argument_helper'
|
9
|
+
require 'business_central/object/url_helper'
|
10
|
+
require 'business_central/object/object_helper'
|
11
|
+
|
9
12
|
require 'business_central/object/base'
|
10
13
|
require 'business_central/object/validation'
|
11
14
|
require 'business_central/object/response'
|
12
15
|
require 'business_central/object/request'
|
16
|
+
require 'business_central/object/filter_query'
|
17
|
+
require 'business_central/object/url_builder'
|
13
18
|
|
14
19
|
require 'business_central/object/account'
|
15
20
|
require 'business_central/object/aged_account_payable'
|
@@ -26,10 +31,38 @@ require 'business_central/object/customer_payment'
|
|
26
31
|
require 'business_central/object/customer_payment_journal'
|
27
32
|
require 'business_central/object/customer_sale'
|
28
33
|
require 'business_central/object/default_dimension'
|
29
|
-
require 'business_central/object/
|
34
|
+
require 'business_central/object/dimension'
|
35
|
+
require 'business_central/object/dimension_line'
|
36
|
+
require 'business_central/object/dimension_value'
|
37
|
+
require 'business_central/object/employee'
|
30
38
|
require 'business_central/object/item'
|
39
|
+
require 'business_central/object/item_category'
|
40
|
+
require 'business_central/object/journal'
|
41
|
+
require 'business_central/object/journal_line'
|
42
|
+
require 'business_central/object/payment_method'
|
43
|
+
require 'business_central/object/payment_term'
|
44
|
+
require 'business_central/object/picture'
|
31
45
|
require 'business_central/object/purchase_invoice'
|
32
46
|
require 'business_central/object/purchase_invoice_line'
|
47
|
+
require 'business_central/object/retained_earning_statement'
|
48
|
+
require 'business_central/object/sales_invoice'
|
49
|
+
require 'business_central/object/sales_invoice_line'
|
50
|
+
require 'business_central/object/sales_credit_memo'
|
51
|
+
require 'business_central/object/sales_credit_memo_line'
|
52
|
+
require 'business_central/object/sales_order'
|
53
|
+
require 'business_central/object/sales_order_line'
|
54
|
+
require 'business_central/object/sales_quote'
|
55
|
+
require 'business_central/object/sales_quote_line'
|
56
|
+
require 'business_central/object/shipment_method'
|
57
|
+
require 'business_central/object/subscription'
|
58
|
+
require 'business_central/object/tax_area'
|
59
|
+
require 'business_central/object/tax_group'
|
60
|
+
require 'business_central/object/time_registration_entry'
|
61
|
+
require 'business_central/object/trial_balance'
|
62
|
+
require 'business_central/object/units_of_measure'
|
63
|
+
require 'business_central/object/vendor'
|
64
|
+
require 'business_central/object/vendor_purchase'
|
33
65
|
|
34
66
|
require 'business_central/exceptions'
|
35
67
|
require 'business_central/client'
|
68
|
+
require 'business_central/web_service'
|
@@ -1,10 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module BusinessCentral
|
2
4
|
class Client
|
3
|
-
extend BusinessCentral::Object::
|
5
|
+
extend BusinessCentral::Object::ObjectHelper
|
4
6
|
|
5
|
-
|
7
|
+
DEFAULT_LOGIN_URL = 'https://login.microsoftonline.com/common'
|
6
8
|
|
7
|
-
|
9
|
+
DEFAULT_URL = 'https://api.businesscentral.dynamics.com/v2.0/production/api/v1.0'
|
8
10
|
|
9
11
|
attr_reader :tenant_id,
|
10
12
|
:username,
|
@@ -12,11 +14,13 @@ module BusinessCentral
|
|
12
14
|
:application_id,
|
13
15
|
:secret_key,
|
14
16
|
:url,
|
17
|
+
:web_service_url,
|
15
18
|
:oauth2_login_url,
|
16
|
-
:
|
17
|
-
:default_company_id
|
19
|
+
:oauth2_access_token,
|
20
|
+
:default_company_id,
|
21
|
+
:debug
|
18
22
|
|
19
|
-
|
23
|
+
alias access_token oauth2_access_token
|
20
24
|
|
21
25
|
object :account
|
22
26
|
object :aged_account_payable
|
@@ -33,10 +37,37 @@ module BusinessCentral
|
|
33
37
|
object :customer_payment_journal
|
34
38
|
object :customer_sale
|
35
39
|
object :default_dimension
|
36
|
-
object :
|
40
|
+
object :dimension
|
41
|
+
object :dimension_line
|
42
|
+
object :dimension_value
|
43
|
+
object :employee
|
37
44
|
object :purchase_invoice
|
38
45
|
object :purchase_invoice_line
|
39
46
|
object :item
|
47
|
+
object :item_category
|
48
|
+
object :journal
|
49
|
+
object :journal_line
|
50
|
+
object :payment_method
|
51
|
+
object :payment_term
|
52
|
+
object :picture
|
53
|
+
object :retained_earning_statement
|
54
|
+
object :sales_invoice
|
55
|
+
object :sales_invoice_line
|
56
|
+
object :sales_credit_memo
|
57
|
+
object :sales_credit_memo_line
|
58
|
+
object :sales_order
|
59
|
+
object :sales_order_line
|
60
|
+
object :sales_quote
|
61
|
+
object :sales_quote_line
|
62
|
+
object :shipment_method
|
63
|
+
object :subscription
|
64
|
+
object :tax_area
|
65
|
+
object :tax_group
|
66
|
+
object :time_registration_entry
|
67
|
+
object :trial_balance
|
68
|
+
object :units_of_measure
|
69
|
+
object :vendor
|
70
|
+
object :vendor_purchase
|
40
71
|
|
41
72
|
def initialize(options = {})
|
42
73
|
opts = options.dup
|
@@ -44,70 +75,66 @@ module BusinessCentral
|
|
44
75
|
@username = opts.delete(:username)
|
45
76
|
@password = opts.delete(:password)
|
46
77
|
@url = opts.delete(:url) || DEFAULT_URL
|
78
|
+
@web_service_url = opts.delete(:web_service_url)
|
47
79
|
@application_id = opts.delete(:application_id)
|
48
80
|
@secret_key = opts.delete(:secret_key)
|
49
81
|
@oauth2_login_url = opts.delete(:oauth2_login_url) || DEFAULT_LOGIN_URL
|
50
82
|
@default_company_id = opts.delete(:default_company_id)
|
83
|
+
@debug = opts.delete(:debug) || false
|
51
84
|
end
|
52
85
|
|
53
86
|
def authorize(params = {}, oauth_authorize_callback: '')
|
54
87
|
params[:redirect_uri] = oauth_authorize_callback
|
55
88
|
begin
|
56
89
|
oauth2_client.auth_code.authorize_url(params)
|
57
|
-
rescue OAuth2::Error =>
|
58
|
-
handle_error(
|
90
|
+
rescue OAuth2::Error => e
|
91
|
+
handle_error(e)
|
59
92
|
end
|
60
93
|
end
|
61
94
|
|
62
95
|
def request_token(code = '', oauth_token_callback: '')
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
handle_error(error)
|
67
|
-
end
|
96
|
+
oauth2_client.auth_code.get_token(code, redirect_uri: oauth_token_callback)
|
97
|
+
rescue OAuth2::Error => e
|
98
|
+
handle_error(e)
|
68
99
|
end
|
69
100
|
|
70
101
|
def authorize_from_token(token: '', refresh_token: '', expires_at: nil, expires_in: nil)
|
71
|
-
@
|
102
|
+
@oauth2_access_token = OAuth2::AccessToken.new(
|
72
103
|
oauth2_client,
|
73
104
|
token,
|
74
105
|
refresh_token: refresh_token,
|
75
106
|
expires_at: expires_at,
|
76
|
-
expires_in: expires_in
|
107
|
+
expires_in: expires_in
|
77
108
|
)
|
78
109
|
end
|
79
110
|
|
80
111
|
def refresh_token
|
81
|
-
@
|
112
|
+
@oauth2_access_token.refresh!
|
113
|
+
end
|
114
|
+
|
115
|
+
def web_service
|
116
|
+
@web_service ||= BusinessCentral::WebService.new(client: self, url: web_service_url)
|
82
117
|
end
|
83
118
|
|
84
119
|
private
|
85
120
|
|
86
121
|
def oauth2_client
|
87
|
-
|
88
|
-
@
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
token_url: 'oauth2/token?resource=https://api.businesscentral.dynamics.com'
|
95
|
-
}
|
96
|
-
)
|
97
|
-
end
|
98
|
-
|
99
|
-
return @oauth2_client
|
122
|
+
OAuth2::Client.new(
|
123
|
+
@application_id,
|
124
|
+
@secret_key,
|
125
|
+
site: @oauth2_login_url,
|
126
|
+
authorize_url: 'oauth2/authorize?resource=https://api.businesscentral.dynamics.com',
|
127
|
+
token_url: 'oauth2/token?resource=https://api.businesscentral.dynamics.com'
|
128
|
+
)
|
100
129
|
end
|
101
130
|
|
102
131
|
def handle_error(error)
|
103
|
-
if
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
else
|
109
|
-
raise ApiException.new(error.message)
|
132
|
+
raise ApiException, error.message if error.code.nil?
|
133
|
+
|
134
|
+
case error.code
|
135
|
+
when 'invalid_client'
|
136
|
+
raise InvalidClientException
|
110
137
|
end
|
111
138
|
end
|
112
139
|
end
|
113
|
-
end
|
140
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module BusinessCentral
|
3
4
|
class BusinessCentralError < StandardError; end
|
4
5
|
|
5
6
|
class InvalidClientException < BusinessCentralError
|
@@ -13,9 +14,7 @@ module BusinessCentral
|
|
13
14
|
@message = message
|
14
15
|
end
|
15
16
|
|
16
|
-
|
17
|
-
@message
|
18
|
-
end
|
17
|
+
attr_reader :message
|
19
18
|
end
|
20
19
|
|
21
20
|
class CompanyNotFoundException < BusinessCentralError
|
@@ -30,15 +29,19 @@ module BusinessCentral
|
|
30
29
|
end
|
31
30
|
end
|
32
31
|
|
32
|
+
class NotFoundException < BusinessCentralError
|
33
|
+
def message
|
34
|
+
'Not Found - The URL provided cannot be found'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
33
38
|
class InvalidObjectException < BusinessCentralError
|
34
39
|
def initialize(errors)
|
35
40
|
@errors = errors
|
36
41
|
end
|
37
42
|
|
38
43
|
def message
|
39
|
-
@errors.each
|
40
|
-
"#{error[:field]} - #{error[:message]}"
|
41
|
-
end
|
44
|
+
@errors.each { |error| "#{error[:field]} - #{error[:message]}" }
|
42
45
|
end
|
43
46
|
end
|
44
47
|
|
@@ -49,7 +52,8 @@ module BusinessCentral
|
|
49
52
|
end
|
50
53
|
|
51
54
|
def message
|
52
|
-
|
55
|
+
methods_allowed = @allowed_methods.join(', ')
|
56
|
+
"#{@method} method is currently not support. Allowed methods are: #{methods_allowed}"
|
53
57
|
end
|
54
58
|
end
|
55
59
|
|
@@ -62,4 +66,10 @@ module BusinessCentral
|
|
62
66
|
"Invalid argument entered - #{@message}"
|
63
67
|
end
|
64
68
|
end
|
65
|
-
|
69
|
+
|
70
|
+
class InvalidObjectURLException < BusinessCentralError
|
71
|
+
def message
|
72
|
+
'Object URL missing for request'
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -1,11 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module BusinessCentral
|
2
4
|
module Object
|
3
5
|
class AgedAccountPayable < Base
|
4
|
-
OBJECT = 'agedAccountsPayable'
|
6
|
+
OBJECT = 'agedAccountsPayable'
|
5
7
|
|
6
8
|
OBJECT_METHODS = [
|
7
9
|
:get
|
8
10
|
].freeze
|
9
11
|
end
|
10
12
|
end
|
11
|
-
end
|
13
|
+
end
|
@@ -1,11 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module BusinessCentral
|
2
4
|
module Object
|
3
5
|
class AgedAccountReceivable < Base
|
4
|
-
OBJECT = 'agedAccountsReceivable'
|
6
|
+
OBJECT = 'agedAccountsReceivable'
|
5
7
|
|
6
8
|
OBJECT_METHODS = [
|
7
9
|
:get
|
8
10
|
].freeze
|
9
11
|
end
|
10
12
|
end
|
11
|
-
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
|
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module BusinessCentral
|
2
4
|
module Object
|
3
5
|
class Base
|
4
|
-
|
5
|
-
|
6
|
-
|
6
|
+
include ArgumentHelper
|
7
|
+
|
8
|
+
attr_reader :client, :parent_path, :path
|
7
9
|
|
8
10
|
attr_writer :id, :company_id
|
9
11
|
|
@@ -19,71 +21,107 @@ module BusinessCentral
|
|
19
21
|
]
|
20
22
|
end
|
21
23
|
|
22
|
-
def id(argument)
|
23
|
-
return @id if !@id.nil?
|
24
|
-
return argument.fetch(:id, '') if !argument.nil?
|
25
|
-
return nil
|
26
|
-
end
|
27
|
-
|
28
|
-
def company_id(argument)
|
29
|
-
return @client.default_company_id if !@client.default_company_id.nil?
|
30
|
-
return @company_id if !@company_id.nil?
|
31
|
-
return argument.fetch(:company_id, '') if !argument.nil?
|
32
|
-
return nil
|
33
|
-
end
|
34
|
-
|
35
24
|
def find_all
|
36
|
-
if method_supported?(:get)
|
37
|
-
Request.get(@client, build_url(parent_path: @parent_path, child_path: object_name))
|
38
|
-
else
|
25
|
+
if !method_supported?(:get)
|
39
26
|
raise BusinessCentral::NoSupportedMethod.new(:get, object_methods)
|
40
27
|
end
|
28
|
+
|
29
|
+
Request.get(@client, build_url(parent_path: @parent_path, child_path: object_name))
|
41
30
|
end
|
31
|
+
alias all find_all
|
42
32
|
|
43
33
|
def find_by_id(id)
|
44
|
-
if method_supported?(:get)
|
45
|
-
Request.get(@client, build_url(parent_path: @parent_path, child_path: object_name, child_id: id))
|
46
|
-
else
|
34
|
+
if !method_supported?(:get)
|
47
35
|
raise BusinessCentral::NoSupportedMethod.new(:get, object_methods)
|
48
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
|
+
)
|
49
46
|
end
|
47
|
+
alias find find_by_id
|
50
48
|
|
51
|
-
def where(query = '')
|
52
|
-
if method_supported?(:get)
|
53
|
-
Request.get(@client, build_url(parent_path: @parent_path, child_path: object_name, filter: query))
|
54
|
-
else
|
49
|
+
def where(query = '', *values)
|
50
|
+
if !method_supported?(:get)
|
55
51
|
raise BusinessCentral::NoSupportedMethod.new(:get, object_methods)
|
56
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
|
+
)
|
57
62
|
end
|
58
63
|
|
59
64
|
def create(params = {})
|
60
|
-
if method_supported?(:post)
|
61
|
-
if Validation.new(object_validation, params).valid?
|
62
|
-
Request.post(@client, build_url(parent_path: @parent_path, child_path: object_name), params)
|
63
|
-
end
|
64
|
-
else
|
65
|
+
if !method_supported?(:post)
|
65
66
|
raise BusinessCentral::NoSupportedMethod.new(:post, object_methods)
|
66
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)
|
67
71
|
end
|
68
72
|
|
69
73
|
def update(id, params = {})
|
70
|
-
if method_supported?(:patch)
|
71
|
-
object = find_by_id(id).merge(params)
|
72
|
-
if Validation.new(object_validation, object).valid?
|
73
|
-
Request.patch(@client, build_url(parent_path: @parent_path, child_path: object_name, child_id: id), object[:etag], object)
|
74
|
-
end
|
75
|
-
else
|
74
|
+
if !method_supported?(:patch)
|
76
75
|
raise BusinessCentral::NoSupportedMethod.new(:patch, object_methods)
|
77
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
|
+
)
|
78
90
|
end
|
79
91
|
|
80
92
|
def destroy(id)
|
81
|
-
if method_supported?(:delete)
|
82
|
-
object = find_by_id(id)
|
83
|
-
Request.delete(@client, build_url(parent_path: @parent_path, child_path: object_name, child_id: id), object[:etag])
|
84
|
-
else
|
93
|
+
if !method_supported?(:delete)
|
85
94
|
raise BusinessCentral::NoSupportedMethod.new(:delete, object_methods)
|
86
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
|
87
125
|
end
|
88
126
|
|
89
127
|
private
|
@@ -93,30 +131,26 @@ module BusinessCentral
|
|
93
131
|
end
|
94
132
|
|
95
133
|
def object_validation
|
96
|
-
self.class.const_defined?(:OBJECT_VALIDATION)
|
134
|
+
if self.class.const_defined?(:OBJECT_VALIDATION)
|
135
|
+
self.class.const_get(:OBJECT_VALIDATION)
|
136
|
+
else
|
137
|
+
[]
|
138
|
+
end
|
97
139
|
end
|
98
140
|
|
99
141
|
def object_methods
|
100
142
|
self.class.const_get(:OBJECT_METHODS)
|
101
143
|
end
|
102
144
|
|
103
|
-
def
|
104
|
-
|
105
|
-
return false
|
145
|
+
def object_parent_name
|
146
|
+
self.class.const_get(:OBJECT_PARENTS)
|
106
147
|
end
|
107
148
|
|
108
|
-
def
|
109
|
-
|
110
|
-
end
|
149
|
+
def method_supported?(method)
|
150
|
+
return true if object_methods.include?(method)
|
111
151
|
|
112
|
-
|
113
|
-
url = @client.url
|
114
|
-
url += parent_path.map { |parent| "/#{parent[:path]}(#{parent[:id]})" }.join('') if !parent_path.empty?
|
115
|
-
url += "/#{child_path}" if !child_path.to_s.blank?
|
116
|
-
url += "(#{child_id})" if !child_id.to_s.blank?
|
117
|
-
url += "?$filter=#{CGI::escape(filter)}" if !filter.to_s.blank?
|
118
|
-
return url
|
152
|
+
false
|
119
153
|
end
|
120
154
|
end
|
121
155
|
end
|
122
|
-
end
|
156
|
+
end
|