business-central 1.0.5 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +45 -32
- data/lib/business_central/client.rb +7 -52
- data/lib/business_central/exceptions.rb +11 -29
- data/lib/business_central/object/{attachment.rb → attachments.rb} +9 -11
- data/lib/business_central/object/base.rb +42 -114
- data/lib/business_central/object/companies.rb +19 -0
- data/lib/business_central/object/filter_query.rb +2 -0
- data/lib/business_central/object/object_helper.rb +10 -37
- data/lib/business_central/object/picture.rb +4 -40
- data/lib/business_central/object/request.rb +14 -15
- data/lib/business_central/object/response.rb +5 -1
- data/lib/business_central/object/url_builder.rb +16 -12
- data/lib/business_central/object/url_helper.rb +16 -2
- data/lib/business_central/version.rb +1 -1
- data/lib/business_central/web_service.rb +2 -0
- data/lib/business_central.rb +4 -55
- data/lib/refinements/strings.rb +42 -0
- data/test/business_central/client_test.rb +24 -0
- data/test/business_central/exceptions_test.rb +40 -0
- data/test/business_central/object/{attachment_test.rb → attachments_test.rb} +5 -6
- data/test/business_central/object/base_test.rb +59 -22
- data/test/business_central/object/{company_test.rb → companies_test.rb} +3 -21
- data/test/business_central/object/picture_test.rb +7 -16
- data/test/business_central/object/response_test.rb +5 -0
- data/test/business_central/web_service_test.rb +7 -2
- metadata +38 -167
- data/lib/business_central/object/account.rb +0 -13
- data/lib/business_central/object/aged_account_payable.rb +0 -13
- data/lib/business_central/object/aged_account_receivable.rb +0 -13
- data/lib/business_central/object/argument_helper.rb +0 -22
- data/lib/business_central/object/balance_sheet.rb +0 -13
- data/lib/business_central/object/cash_flow_statement.rb +0 -13
- data/lib/business_central/object/company.rb +0 -18
- data/lib/business_central/object/company_information.rb +0 -14
- data/lib/business_central/object/country_region.rb +0 -26
- data/lib/business_central/object/currency.rb +0 -29
- data/lib/business_central/object/customer.rb +0 -34
- data/lib/business_central/object/customer_financial_detail.rb +0 -13
- data/lib/business_central/object/customer_payment.rb +0 -43
- data/lib/business_central/object/customer_payment_journal.rb +0 -25
- data/lib/business_central/object/customer_sale.rb +0 -13
- data/lib/business_central/object/default_dimension.rb +0 -51
- data/lib/business_central/object/dimension.rb +0 -20
- data/lib/business_central/object/dimension_line.rb +0 -37
- data/lib/business_central/object/dimension_value.rb +0 -28
- data/lib/business_central/object/employee.rb +0 -21
- data/lib/business_central/object/general_ledger_entry.rb +0 -13
- data/lib/business_central/object/income_statement.rb +0 -13
- data/lib/business_central/object/irs1099_code.rb +0 -16
- data/lib/business_central/object/item.rb +0 -47
- data/lib/business_central/object/item_category.rb +0 -16
- data/lib/business_central/object/journal.rb +0 -29
- data/lib/business_central/object/journal_line.rb +0 -48
- data/lib/business_central/object/payment_method.rb +0 -16
- data/lib/business_central/object/payment_term.rb +0 -16
- data/lib/business_central/object/purchase_invoice.rb +0 -69
- data/lib/business_central/object/purchase_invoice_line.rb +0 -37
- data/lib/business_central/object/retained_earning_statement.rb +0 -13
- data/lib/business_central/object/sales_credit_memo.rb +0 -20
- data/lib/business_central/object/sales_credit_memo_line.rb +0 -30
- data/lib/business_central/object/sales_invoice.rb +0 -62
- data/lib/business_central/object/sales_invoice_line.rb +0 -30
- data/lib/business_central/object/sales_order.rb +0 -20
- data/lib/business_central/object/sales_order_line.rb +0 -30
- data/lib/business_central/object/sales_quote.rb +0 -20
- data/lib/business_central/object/sales_quote_line.rb +0 -30
- data/lib/business_central/object/shipment_method.rb +0 -16
- data/lib/business_central/object/subscription.rb +0 -16
- data/lib/business_central/object/tax_area.rb +0 -16
- data/lib/business_central/object/tax_group.rb +0 -16
- data/lib/business_central/object/time_registration_entry.rb +0 -16
- data/lib/business_central/object/trial_balance.rb +0 -13
- data/lib/business_central/object/units_of_measure.rb +0 -16
- data/lib/business_central/object/validation.rb +0 -57
- data/lib/business_central/object/vendor.rb +0 -42
- data/lib/business_central/object/vendor_purchase.rb +0 -13
- data/lib/core_ext/string.rb +0 -31
- data/test/business_central/object/account_test.rb +0 -67
- data/test/business_central/object/aged_account_payable_test.rb +0 -61
- data/test/business_central/object/aged_account_receivable_test.rb +0 -61
- data/test/business_central/object/balance_sheet_test.rb +0 -61
- data/test/business_central/object/cash_flow_statement_test.rb +0 -61
- data/test/business_central/object/company_information_test.rb +0 -81
- data/test/business_central/object/country_region_test.rb +0 -129
- data/test/business_central/object/currency_test.rb +0 -150
- data/test/business_central/object/customer_financial_detail_test.rb +0 -68
- data/test/business_central/object/customer_payment_journal_test.rb +0 -134
- data/test/business_central/object/customer_payment_test.rb +0 -141
- data/test/business_central/object/customer_sale_test.rb +0 -72
- data/test/business_central/object/customer_test.rb +0 -168
- data/test/business_central/object/default_dimension_test.rb +0 -170
- data/test/business_central/object/dimension_line_test.rb +0 -165
- data/test/business_central/object/dimension_test.rb +0 -65
- data/test/business_central/object/dimension_value_test.rb +0 -68
- data/test/business_central/object/employee_test.rb +0 -141
- data/test/business_central/object/general_ledger_entry_test.rb +0 -82
- data/test/business_central/object/income_statement_test.rb +0 -82
- data/test/business_central/object/irs1099_code_test.rb +0 -124
- data/test/business_central/object/item_category_test.rb +0 -122
- data/test/business_central/object/item_test.rb +0 -121
- data/test/business_central/object/journal_line_test.rb +0 -152
- data/test/business_central/object/journal_test.rb +0 -144
- data/test/business_central/object/payment_method_test.rb +0 -134
- data/test/business_central/object/payment_term_test.rb +0 -134
- data/test/business_central/object/purchase_invoice_line_test.rb +0 -123
- data/test/business_central/object/purchase_invoice_test.rb +0 -119
- data/test/business_central/object/retained_earning_statement_test.rb +0 -52
- data/test/business_central/object/sales_credit_memo_line_test.rb +0 -134
- data/test/business_central/object/sales_credit_memo_test.rb +0 -146
- data/test/business_central/object/sales_invoice_line_test.rb +0 -164
- data/test/business_central/object/sales_invoice_test.rb +0 -156
- data/test/business_central/object/sales_order_line_test.rb +0 -134
- data/test/business_central/object/sales_order_test.rb +0 -146
- data/test/business_central/object/sales_quote_line_test.rb +0 -134
- data/test/business_central/object/sales_quote_test.rb +0 -146
- data/test/business_central/object/shipment_method_test.rb +0 -133
- data/test/business_central/object/subscription_test.rb +0 -126
- data/test/business_central/object/tax_area_test.rb +0 -126
- data/test/business_central/object/tax_group_test.rb +0 -126
- data/test/business_central/object/time_registration_entry_test.rb +0 -126
- data/test/business_central/object/trial_balance_test.rb +0 -82
- data/test/business_central/object/units_of_measure_test.rb +0 -126
- data/test/business_central/object/validation_test.rb +0 -62
- data/test/business_central/object/vendor_purchase_test.rb +0 -82
- data/test/business_central/object/vendor_test.rb +0 -119
@@ -3,6 +3,8 @@
|
|
3
3
|
module BusinessCentral
|
4
4
|
module Object
|
5
5
|
class Request
|
6
|
+
using Refinements::Strings
|
7
|
+
|
6
8
|
class << self
|
7
9
|
def get(client, url)
|
8
10
|
request(:get, client, url)
|
@@ -66,23 +68,20 @@ module BusinessCentral
|
|
66
68
|
response
|
67
69
|
elsif Response.success_no_content?(request.code.to_i)
|
68
70
|
true
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
71
|
+
elsif Response.unauthorized?(request.code.to_i)
|
72
|
+
raise UnauthorizedException
|
73
|
+
elsif Response.not_found?(request.code.to_i)
|
74
|
+
raise NotFoundException
|
75
|
+
elsif !response.fetch(:error, nil).nil?
|
76
|
+
case response[:error][:code]
|
77
|
+
when 'Internal_CompanyNotFound'
|
78
|
+
raise CompanyNotFoundException
|
74
79
|
else
|
75
|
-
|
76
|
-
|
77
|
-
when 'Internal_CompanyNotFound'
|
78
|
-
raise CompanyNotFoundException
|
79
|
-
else
|
80
|
-
raise ApiException, "#{request.code} - #{response[:error][:code]} #{response[:error][:message]}"
|
81
|
-
end
|
82
|
-
else
|
83
|
-
raise ApiException, "#{request.code} - API call failed"
|
84
|
-
end
|
80
|
+
raise ApiException,
|
81
|
+
"#{request.code} - #{response[:error][:code]} #{response[:error][:message]}"
|
85
82
|
end
|
83
|
+
else
|
84
|
+
raise ApiException, "#{request.code} - API call failed"
|
86
85
|
end
|
87
86
|
end
|
88
87
|
end
|
@@ -3,6 +3,8 @@
|
|
3
3
|
module BusinessCentral
|
4
4
|
module Object
|
5
5
|
class Response
|
6
|
+
using Refinements::Strings
|
7
|
+
|
6
8
|
class << self
|
7
9
|
def success?(status)
|
8
10
|
[200, 201].include?(status)
|
@@ -35,7 +37,9 @@ module BusinessCentral
|
|
35
37
|
private
|
36
38
|
|
37
39
|
def process
|
38
|
-
if @response.is_a?(
|
40
|
+
if @response.is_a?(String)
|
41
|
+
@results = @response
|
42
|
+
elsif @response.is_a?(Array)
|
39
43
|
@results = []
|
40
44
|
@response.each do |data|
|
41
45
|
@results << convert(data)
|
@@ -3,6 +3,8 @@
|
|
3
3
|
module BusinessCentral
|
4
4
|
module Object
|
5
5
|
class URLBuilder
|
6
|
+
using Refinements::Strings
|
7
|
+
|
6
8
|
extend URLHelper
|
7
9
|
|
8
10
|
class << self
|
@@ -14,12 +16,11 @@ module BusinessCentral
|
|
14
16
|
end
|
15
17
|
end
|
16
18
|
|
17
|
-
def initialize(base_url:,
|
19
|
+
def initialize(base_url:, object_path: [], object_id: '', object_code: '', filter: '')
|
18
20
|
@base_url = base_url.to_s
|
19
|
-
@
|
20
|
-
@
|
21
|
-
@
|
22
|
-
@child_code = child_code.to_s
|
21
|
+
@object_path = object_path || []
|
22
|
+
@object_id = object_id.to_s
|
23
|
+
@object_code = object_code.to_s
|
23
24
|
@filter = filter.to_s
|
24
25
|
end
|
25
26
|
|
@@ -34,18 +35,21 @@ module BusinessCentral
|
|
34
35
|
private
|
35
36
|
|
36
37
|
def build_parent_path
|
37
|
-
return '' if @
|
38
|
-
|
39
|
-
@
|
40
|
-
|
38
|
+
return '' if @object_path.empty?
|
39
|
+
|
40
|
+
@object_path.map do |parent|
|
41
|
+
if !parent[:id].nil?
|
42
|
+
"/#{parent[:path]}(#{parent[:id]})"
|
43
|
+
else
|
44
|
+
"/#{parent[:path]}"
|
45
|
+
end
|
41
46
|
end.join('')
|
42
47
|
end
|
43
48
|
|
44
49
|
def build_child_path
|
45
50
|
url = ''
|
46
|
-
url += "
|
47
|
-
url += "(#{@
|
48
|
-
url += "('#{odata_encode(@child_code)}')" if @child_code.present?
|
51
|
+
url += "(#{@object_id})" if @object_id.present?
|
52
|
+
url += "('#{odata_encode(@object_code)}')" if @object_code.present?
|
49
53
|
url
|
50
54
|
end
|
51
55
|
|
@@ -3,8 +3,10 @@
|
|
3
3
|
module BusinessCentral
|
4
4
|
module Object
|
5
5
|
module URLHelper
|
6
|
+
using Refinements::Strings
|
7
|
+
|
6
8
|
def encode_url_object(object)
|
7
|
-
URI.
|
9
|
+
URI::RFC2396_Parser.new.escape(object)
|
8
10
|
end
|
9
11
|
|
10
12
|
def encode_url_params(query)
|
@@ -13,7 +15,7 @@ module BusinessCentral
|
|
13
15
|
|
14
16
|
def odata_encode(value)
|
15
17
|
value = value.dup
|
16
|
-
value.gsub!(/'/, "''") if value =~ /'/
|
18
|
+
value.gsub!(/'/, "''") if value.instance_of?(String) && value =~ /'/
|
17
19
|
value.to_s
|
18
20
|
end
|
19
21
|
|
@@ -21,6 +23,18 @@ module BusinessCentral
|
|
21
23
|
|
22
24
|
def replace_template_with_value(query, values)
|
23
25
|
query = query.dup
|
26
|
+
return replace_template_with_hash_values(query, values) if values.first.is_a?(Hash)
|
27
|
+
replace_template_with_array_values(query, values)
|
28
|
+
end
|
29
|
+
|
30
|
+
def replace_template_with_hash_values(query, values)
|
31
|
+
values.first.each do |key, value|
|
32
|
+
query.gsub!(/:#{key}/, odata_encode(value))
|
33
|
+
end
|
34
|
+
query
|
35
|
+
end
|
36
|
+
|
37
|
+
def replace_template_with_array_values(query, values)
|
24
38
|
query.scan(/\?/).each_with_index do |_character, index|
|
25
39
|
character_position = query =~ /\?/
|
26
40
|
query[character_position] = odata_encode(values[index])
|
data/lib/business_central.rb
CHANGED
@@ -3,69 +3,18 @@ require 'oauth2/error'
|
|
3
3
|
require 'net/http'
|
4
4
|
require 'json'
|
5
5
|
|
6
|
-
require '
|
6
|
+
require 'refinements/strings'
|
7
7
|
|
8
|
-
require 'business_central/object/argument_helper'
|
9
8
|
require 'business_central/object/url_helper'
|
10
9
|
require 'business_central/object/object_helper'
|
11
|
-
|
12
|
-
require 'business_central/object/base'
|
13
|
-
require 'business_central/object/validation'
|
14
10
|
require 'business_central/object/response'
|
15
11
|
require 'business_central/object/request'
|
16
12
|
require 'business_central/object/filter_query'
|
17
13
|
require 'business_central/object/url_builder'
|
18
|
-
|
19
|
-
require 'business_central/object/
|
20
|
-
require 'business_central/object/aged_account_payable'
|
21
|
-
require 'business_central/object/aged_account_receivable'
|
22
|
-
require 'business_central/object/attachment'
|
23
|
-
require 'business_central/object/cash_flow_statement'
|
24
|
-
require 'business_central/object/balance_sheet'
|
25
|
-
require 'business_central/object/company'
|
26
|
-
require 'business_central/object/company_information'
|
27
|
-
require 'business_central/object/country_region'
|
28
|
-
require 'business_central/object/currency'
|
29
|
-
require 'business_central/object/customer'
|
30
|
-
require 'business_central/object/customer_financial_detail'
|
31
|
-
require 'business_central/object/customer_payment'
|
32
|
-
require 'business_central/object/customer_payment_journal'
|
33
|
-
require 'business_central/object/customer_sale'
|
34
|
-
require 'business_central/object/default_dimension'
|
35
|
-
require 'business_central/object/dimension'
|
36
|
-
require 'business_central/object/dimension_line'
|
37
|
-
require 'business_central/object/dimension_value'
|
38
|
-
require 'business_central/object/employee'
|
39
|
-
require 'business_central/object/general_ledger_entry'
|
40
|
-
require 'business_central/object/income_statement'
|
41
|
-
require 'business_central/object/irs1099_code'
|
42
|
-
require 'business_central/object/item'
|
43
|
-
require 'business_central/object/item_category'
|
44
|
-
require 'business_central/object/journal'
|
45
|
-
require 'business_central/object/journal_line'
|
46
|
-
require 'business_central/object/payment_method'
|
47
|
-
require 'business_central/object/payment_term'
|
14
|
+
require 'business_central/object/base'
|
15
|
+
require 'business_central/object/companies'
|
48
16
|
require 'business_central/object/picture'
|
49
|
-
require 'business_central/object/
|
50
|
-
require 'business_central/object/purchase_invoice_line'
|
51
|
-
require 'business_central/object/retained_earning_statement'
|
52
|
-
require 'business_central/object/sales_invoice'
|
53
|
-
require 'business_central/object/sales_invoice_line'
|
54
|
-
require 'business_central/object/sales_credit_memo'
|
55
|
-
require 'business_central/object/sales_credit_memo_line'
|
56
|
-
require 'business_central/object/sales_order'
|
57
|
-
require 'business_central/object/sales_order_line'
|
58
|
-
require 'business_central/object/sales_quote'
|
59
|
-
require 'business_central/object/sales_quote_line'
|
60
|
-
require 'business_central/object/shipment_method'
|
61
|
-
require 'business_central/object/subscription'
|
62
|
-
require 'business_central/object/tax_area'
|
63
|
-
require 'business_central/object/tax_group'
|
64
|
-
require 'business_central/object/time_registration_entry'
|
65
|
-
require 'business_central/object/trial_balance'
|
66
|
-
require 'business_central/object/units_of_measure'
|
67
|
-
require 'business_central/object/vendor'
|
68
|
-
require 'business_central/object/vendor_purchase'
|
17
|
+
require 'business_central/object/attachments'
|
69
18
|
|
70
19
|
require 'business_central/exceptions'
|
71
20
|
require 'business_central/client'
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Refinements
|
4
|
+
module Strings
|
5
|
+
refine String do
|
6
|
+
def blank?
|
7
|
+
empty? || /\A[[:space:]]*\z/.match?(self)
|
8
|
+
end
|
9
|
+
|
10
|
+
def present?
|
11
|
+
!blank?
|
12
|
+
end
|
13
|
+
|
14
|
+
# Convert string to CamelCase
|
15
|
+
def to_camel_case(uppercase_first_letter = false)
|
16
|
+
string = self
|
17
|
+
string = if uppercase_first_letter
|
18
|
+
string.sub(/^[a-z\d]*/, &:capitalize)
|
19
|
+
else
|
20
|
+
string.sub(/^(?:(?=\b|[A-Z_])|\w)/, &:downcase)
|
21
|
+
end
|
22
|
+
string.gsub(%r{(?:_|(/))([a-z\d]*)}) do
|
23
|
+
"#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}"
|
24
|
+
end.gsub('/', '::')
|
25
|
+
end
|
26
|
+
|
27
|
+
# Convert string to snake_case
|
28
|
+
def to_snake_case
|
29
|
+
gsub(/::/, '/')
|
30
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
31
|
+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
32
|
+
.tr('-', '_')
|
33
|
+
.downcase
|
34
|
+
end
|
35
|
+
|
36
|
+
# Convert to class symbol
|
37
|
+
def to_class_sym
|
38
|
+
to_camel_case(true).to_s.to_sym
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -112,6 +112,30 @@ class BusinessCentral::ClientTest < Minitest::Test
|
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
115
|
+
def test_refresh_token_throws_invalid_grant_exception
|
116
|
+
@client.authorize_from_token(
|
117
|
+
token: '123',
|
118
|
+
refresh_token: '456',
|
119
|
+
expires_at: Time.now + 3600,
|
120
|
+
expires_in: 3600
|
121
|
+
)
|
122
|
+
stub_request(:post, /#{BusinessCentral::Client::DEFAULT_LOGIN_URL}/)
|
123
|
+
.to_return(
|
124
|
+
status: 200,
|
125
|
+
headers: {
|
126
|
+
'Content-Type': 'application/json'
|
127
|
+
},
|
128
|
+
body: {
|
129
|
+
error: 'invalid_grant',
|
130
|
+
error_description: 'Invalid grant was because it was invalid'
|
131
|
+
}.to_json
|
132
|
+
)
|
133
|
+
|
134
|
+
assert_raises(BusinessCentral::InvalidGrantException) do
|
135
|
+
@client.refresh_token
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
115
139
|
def test_build_web_service_object
|
116
140
|
@client.web_service.object('Company/Vendors')
|
117
141
|
assert_equal @client.web_service.object_url, 'Company/Vendors'
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
# rake test TEST=test/business_central/exceptions_test.rb
|
5
|
+
|
6
|
+
class BusinessCentral::ExceptionsTest < Minitest::Test
|
7
|
+
def test_invalid_client_exception
|
8
|
+
exception = BusinessCentral::InvalidClientException.new
|
9
|
+
assert_equal('Invalid client setup', exception.message)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_company_not_found_exception
|
13
|
+
exception = BusinessCentral::CompanyNotFoundException.new
|
14
|
+
assert_equal('Company not found', exception.message)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_unathorized_exception
|
18
|
+
exception = BusinessCentral::UnauthorizedException.new
|
19
|
+
assert_equal('Unauthorized - The credentials provided are incorrect', exception.message)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_not_found_exception
|
23
|
+
exception = BusinessCentral::NotFoundException.new
|
24
|
+
assert_equal('Not Found - The URL provided cannot be found', exception.message)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_invalid_object_url_exception
|
28
|
+
exception = BusinessCentral::InvalidObjectURLException.new
|
29
|
+
assert_equal('Object URL missing for request', exception.message)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_invalid_grant_exception
|
33
|
+
exception = BusinessCentral::InvalidGrantException.new('Extra error details')
|
34
|
+
assert_equal(
|
35
|
+
'The provided grant has expired due to it being revoked, a fresh auth token is needed',
|
36
|
+
exception.message
|
37
|
+
)
|
38
|
+
assert_equal('Extra error details', exception.error_message)
|
39
|
+
end
|
40
|
+
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'test_helper'
|
4
|
-
# rake test TEST=test/business_central/object/
|
4
|
+
# rake test TEST=test/business_central/object/attachments_test.rb
|
5
5
|
|
6
|
-
class BusinessCentral::Object::
|
6
|
+
class BusinessCentral::Object::AttachmentsTest < Minitest::Test
|
7
7
|
def setup
|
8
8
|
@company_id = '123456'
|
9
9
|
@client = BusinessCentral::Client.new
|
10
|
-
@attachment = @client.
|
10
|
+
@attachment = @client.attachments(company_id: @company_id)
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_find_all
|
@@ -106,12 +106,11 @@ class BusinessCentral::Object::AttachmentTest < Minitest::Test
|
|
106
106
|
end
|
107
107
|
|
108
108
|
def test_delete
|
109
|
-
test_parent_id = '011124'
|
110
109
|
test_attachment_id = '11124'
|
111
110
|
|
112
|
-
stub_request(:delete, /attachments\(#{
|
111
|
+
stub_request(:delete, /attachments\(#{test_attachment_id}\)/)
|
113
112
|
.to_return(status: 204)
|
114
113
|
|
115
|
-
assert @attachment.destroy(
|
114
|
+
assert @attachment.destroy(test_attachment_id)
|
116
115
|
end
|
117
116
|
end
|
@@ -6,36 +6,73 @@ require 'test_helper'
|
|
6
6
|
class BusinessCentral::Object::BaseTest < Minitest::Test
|
7
7
|
def setup
|
8
8
|
@company_id = '123456'
|
9
|
-
@client = BusinessCentral::Client.new
|
9
|
+
@client = BusinessCentral::Client.new(
|
10
|
+
default_company_id: @company_id
|
11
|
+
)
|
10
12
|
end
|
11
13
|
|
12
|
-
def
|
13
|
-
|
14
|
-
set_object_method(base, [])
|
15
|
-
assert_raises(BusinessCentral::NoSupportedMethod) do
|
16
|
-
base.find_all
|
17
|
-
end
|
14
|
+
def test_client_responds_to_any_method_object
|
15
|
+
assert(@client.respond_to?(:random_object_that_does_not_exist))
|
18
16
|
end
|
19
17
|
|
20
|
-
def
|
21
|
-
|
22
|
-
set_object_method(base, [])
|
23
|
-
assert_raises(BusinessCentral::NoSupportedMethod) do
|
24
|
-
base.find_by_id('123')
|
25
|
-
end
|
18
|
+
def test_respond_to_chaining_objects
|
19
|
+
assert(@client.vendors.respond_to?(:random_object_that_does_not_exist))
|
26
20
|
end
|
27
21
|
|
28
|
-
def
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
22
|
+
def test_find_all_for_vendor_object
|
23
|
+
stub_request(:get, %r{companies\(#{@company_id}\)/vendors})
|
24
|
+
.to_return(
|
25
|
+
status: 200,
|
26
|
+
body: {
|
27
|
+
'value': [
|
28
|
+
{
|
29
|
+
displayName: 'vendor1'
|
30
|
+
}
|
31
|
+
]
|
32
|
+
}.to_json
|
33
|
+
)
|
34
|
+
|
35
|
+
response = @client.vendors.find_all
|
36
|
+
assert_equal response.first[:display_name], 'vendor1'
|
34
37
|
end
|
35
38
|
|
36
|
-
|
39
|
+
def test_find_all_for_vendors_default_dimensions
|
40
|
+
stub_request(:get, %r{companies\(#{@company_id}\)/vendors\(1\)/defaultDimensions})
|
41
|
+
.to_return(
|
42
|
+
status: 200,
|
43
|
+
body: {
|
44
|
+
'value': [
|
45
|
+
{
|
46
|
+
dimensionValueCode: 'SALES'
|
47
|
+
}
|
48
|
+
]
|
49
|
+
}.to_json
|
50
|
+
)
|
51
|
+
|
52
|
+
response = @client.vendors(id: 1).default_dimensions.find_all
|
53
|
+
assert_equal response.first[:dimension_value_code], 'SALES'
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_update_vendor_object
|
57
|
+
test_id = 1
|
58
|
+
stub_request(:get, %r{companies\(#{@company_id}\)/vendors\(#{test_id}\)})
|
59
|
+
.to_return(
|
60
|
+
status: 200,
|
61
|
+
body: {
|
62
|
+
etag: '111',
|
63
|
+
displayName: 'vendor1'
|
64
|
+
}.to_json
|
65
|
+
)
|
66
|
+
|
67
|
+
stub_request(:patch, %r{companies\(#{@company_id}\)/vendors\(#{test_id}\)})
|
68
|
+
.to_return(
|
69
|
+
status: 200,
|
70
|
+
body: {
|
71
|
+
displayName: 'vendor2'
|
72
|
+
}.to_json
|
73
|
+
)
|
37
74
|
|
38
|
-
|
39
|
-
|
75
|
+
response = @client.vendors.update(test_id, { display_name: 'vendor2' })
|
76
|
+
assert_equal response[:display_name], 'vendor2'
|
40
77
|
end
|
41
78
|
end
|
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'test_helper'
|
4
|
-
# rake test TEST=test/business_central/object/
|
4
|
+
# rake test TEST=test/business_central/object/companies_test.rb
|
5
5
|
|
6
|
-
class BusinessCentral::Object::
|
6
|
+
class BusinessCentral::Object::CompaniesTest < Minitest::Test
|
7
7
|
def setup
|
8
8
|
@client = BusinessCentral::Client.new
|
9
|
-
@company = @client.
|
9
|
+
@company = @client.companies
|
10
10
|
end
|
11
11
|
|
12
12
|
def test_find_all
|
@@ -39,22 +39,4 @@ class BusinessCentral::Object::CompanyTest < Minitest::Test
|
|
39
39
|
response = @company.find_by_id(test_company_id)
|
40
40
|
assert_equal response[:display_name], 'business2'
|
41
41
|
end
|
42
|
-
|
43
|
-
def test_create
|
44
|
-
assert_raises BusinessCentral::NoSupportedMethod do
|
45
|
-
@company.create({})
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_update
|
50
|
-
assert_raises BusinessCentral::NoSupportedMethod do
|
51
|
-
@company.update('123', {})
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_delete
|
56
|
-
assert_raises BusinessCentral::NoSupportedMethod do
|
57
|
-
@company.destroy('123')
|
58
|
-
end
|
59
|
-
end
|
60
42
|
end
|
@@ -7,11 +7,11 @@ class BusinessCentral::Object::PictureTest < Minitest::Test
|
|
7
7
|
def setup
|
8
8
|
@company_id = '123456'
|
9
9
|
@client = BusinessCentral::Client.new
|
10
|
-
@picture = @client.
|
10
|
+
@picture = @client.items(id: 123, company_id: @company_id).picture
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_find_all
|
14
|
-
stub_request(:get, /picture
|
14
|
+
stub_request(:get, %r{companies\(#{@company_id}\)/items\(123\)/picture})
|
15
15
|
.to_return(
|
16
16
|
status: 200,
|
17
17
|
body: {
|
@@ -25,21 +25,12 @@ class BusinessCentral::Object::PictureTest < Minitest::Test
|
|
25
25
|
]
|
26
26
|
}.to_json
|
27
27
|
)
|
28
|
-
|
29
28
|
response = @picture.find_all
|
30
29
|
assert_equal response.first[:content_type], 'image\jpeg'
|
31
30
|
end
|
32
31
|
|
33
|
-
def test_create
|
34
|
-
stub_request(:patch, /picture/)
|
35
|
-
.to_return(status: 204)
|
36
|
-
|
37
|
-
response = @picture.create('ImageData')
|
38
|
-
assert response
|
39
|
-
end
|
40
|
-
|
41
32
|
def test_update
|
42
|
-
stub_request(:get, /picture
|
33
|
+
stub_request(:get, %r{companies\(#{@company_id}\)/items\(123\)/picture})
|
43
34
|
.to_return(
|
44
35
|
status: 200,
|
45
36
|
body: {
|
@@ -48,16 +39,16 @@ class BusinessCentral::Object::PictureTest < Minitest::Test
|
|
48
39
|
}.to_json
|
49
40
|
)
|
50
41
|
|
51
|
-
stub_request(:patch, /picture/)
|
42
|
+
stub_request(:patch, %r{companies\(#{@company_id}\)/items\(123\)/picture\(1\)/content})
|
52
43
|
.to_return(status: 204)
|
53
44
|
|
54
|
-
response = @picture.update('ImageData')
|
45
|
+
response = @picture.update(1, 'ImageData')
|
55
46
|
assert response
|
56
47
|
end
|
57
48
|
|
58
49
|
def test_delete
|
59
50
|
test_id = 2
|
60
|
-
stub_request(:get, /picture
|
51
|
+
stub_request(:get, %r{companies\(#{@company_id}\)/items\(123\)/picture\(#{test_id}\)})
|
61
52
|
.to_return(
|
62
53
|
status: 200,
|
63
54
|
body: {
|
@@ -66,7 +57,7 @@ class BusinessCentral::Object::PictureTest < Minitest::Test
|
|
66
57
|
}.to_json
|
67
58
|
)
|
68
59
|
|
69
|
-
stub_request(:delete, /picture
|
60
|
+
stub_request(:delete, %r{companies\(#{@company_id}\)/items\(123\)/picture\(#{test_id}\)})
|
70
61
|
.to_return(status: 204)
|
71
62
|
|
72
63
|
assert @picture.destroy(test_id)
|
@@ -43,4 +43,9 @@ class BusinessCentral::Object::ResponseTest < Minitest::Test
|
|
43
43
|
request = BusinessCentral::Object::Response.new(params).results
|
44
44
|
assert request.key?(:item)
|
45
45
|
end
|
46
|
+
|
47
|
+
def test_string_response
|
48
|
+
response = BusinessCentral::Object::Response.new('{ "value": "OK - Action was successful" }')
|
49
|
+
assert_equal 'OK - Action was successful', response.results
|
50
|
+
end
|
46
51
|
end
|
@@ -14,11 +14,16 @@ class BusinessCentral::WebServiceTest < Minitest::Test
|
|
14
14
|
assert_equal @web_service.object_url, 'Company/Vendors'
|
15
15
|
end
|
16
16
|
|
17
|
-
def
|
17
|
+
def test_build_object_url_with_array_template_values
|
18
18
|
@web_service.object("Company('?')", 'business1')
|
19
19
|
assert_equal @web_service.object_url, "Company('business1')"
|
20
20
|
end
|
21
21
|
|
22
|
+
def test_build_object_url_with_hash_template_values
|
23
|
+
@web_service.object("Company(':business_name')", business_name: 'business1')
|
24
|
+
assert_equal @web_service.object_url, "Company('business1')"
|
25
|
+
end
|
26
|
+
|
22
27
|
def test_object_get_request
|
23
28
|
stub_request(:get, /Company/)
|
24
29
|
.to_return(
|
@@ -92,4 +97,4 @@ class BusinessCentral::WebServiceTest < Minitest::Test
|
|
92
97
|
|
93
98
|
assert @web_service.object("Company('?')", test_company_name).delete
|
94
99
|
end
|
95
|
-
end
|
100
|
+
end
|