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.
Files changed (126) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +45 -32
  3. data/lib/business_central/client.rb +7 -52
  4. data/lib/business_central/exceptions.rb +11 -29
  5. data/lib/business_central/object/{attachment.rb → attachments.rb} +9 -11
  6. data/lib/business_central/object/base.rb +42 -114
  7. data/lib/business_central/object/companies.rb +19 -0
  8. data/lib/business_central/object/filter_query.rb +2 -0
  9. data/lib/business_central/object/object_helper.rb +10 -37
  10. data/lib/business_central/object/picture.rb +4 -40
  11. data/lib/business_central/object/request.rb +14 -15
  12. data/lib/business_central/object/response.rb +5 -1
  13. data/lib/business_central/object/url_builder.rb +16 -12
  14. data/lib/business_central/object/url_helper.rb +16 -2
  15. data/lib/business_central/version.rb +1 -1
  16. data/lib/business_central/web_service.rb +2 -0
  17. data/lib/business_central.rb +4 -55
  18. data/lib/refinements/strings.rb +42 -0
  19. data/test/business_central/client_test.rb +24 -0
  20. data/test/business_central/exceptions_test.rb +40 -0
  21. data/test/business_central/object/{attachment_test.rb → attachments_test.rb} +5 -6
  22. data/test/business_central/object/base_test.rb +59 -22
  23. data/test/business_central/object/{company_test.rb → companies_test.rb} +3 -21
  24. data/test/business_central/object/picture_test.rb +7 -16
  25. data/test/business_central/object/response_test.rb +5 -0
  26. data/test/business_central/web_service_test.rb +7 -2
  27. metadata +38 -167
  28. data/lib/business_central/object/account.rb +0 -13
  29. data/lib/business_central/object/aged_account_payable.rb +0 -13
  30. data/lib/business_central/object/aged_account_receivable.rb +0 -13
  31. data/lib/business_central/object/argument_helper.rb +0 -22
  32. data/lib/business_central/object/balance_sheet.rb +0 -13
  33. data/lib/business_central/object/cash_flow_statement.rb +0 -13
  34. data/lib/business_central/object/company.rb +0 -18
  35. data/lib/business_central/object/company_information.rb +0 -14
  36. data/lib/business_central/object/country_region.rb +0 -26
  37. data/lib/business_central/object/currency.rb +0 -29
  38. data/lib/business_central/object/customer.rb +0 -34
  39. data/lib/business_central/object/customer_financial_detail.rb +0 -13
  40. data/lib/business_central/object/customer_payment.rb +0 -43
  41. data/lib/business_central/object/customer_payment_journal.rb +0 -25
  42. data/lib/business_central/object/customer_sale.rb +0 -13
  43. data/lib/business_central/object/default_dimension.rb +0 -51
  44. data/lib/business_central/object/dimension.rb +0 -20
  45. data/lib/business_central/object/dimension_line.rb +0 -37
  46. data/lib/business_central/object/dimension_value.rb +0 -28
  47. data/lib/business_central/object/employee.rb +0 -21
  48. data/lib/business_central/object/general_ledger_entry.rb +0 -13
  49. data/lib/business_central/object/income_statement.rb +0 -13
  50. data/lib/business_central/object/irs1099_code.rb +0 -16
  51. data/lib/business_central/object/item.rb +0 -47
  52. data/lib/business_central/object/item_category.rb +0 -16
  53. data/lib/business_central/object/journal.rb +0 -29
  54. data/lib/business_central/object/journal_line.rb +0 -48
  55. data/lib/business_central/object/payment_method.rb +0 -16
  56. data/lib/business_central/object/payment_term.rb +0 -16
  57. data/lib/business_central/object/purchase_invoice.rb +0 -69
  58. data/lib/business_central/object/purchase_invoice_line.rb +0 -37
  59. data/lib/business_central/object/retained_earning_statement.rb +0 -13
  60. data/lib/business_central/object/sales_credit_memo.rb +0 -20
  61. data/lib/business_central/object/sales_credit_memo_line.rb +0 -30
  62. data/lib/business_central/object/sales_invoice.rb +0 -62
  63. data/lib/business_central/object/sales_invoice_line.rb +0 -30
  64. data/lib/business_central/object/sales_order.rb +0 -20
  65. data/lib/business_central/object/sales_order_line.rb +0 -30
  66. data/lib/business_central/object/sales_quote.rb +0 -20
  67. data/lib/business_central/object/sales_quote_line.rb +0 -30
  68. data/lib/business_central/object/shipment_method.rb +0 -16
  69. data/lib/business_central/object/subscription.rb +0 -16
  70. data/lib/business_central/object/tax_area.rb +0 -16
  71. data/lib/business_central/object/tax_group.rb +0 -16
  72. data/lib/business_central/object/time_registration_entry.rb +0 -16
  73. data/lib/business_central/object/trial_balance.rb +0 -13
  74. data/lib/business_central/object/units_of_measure.rb +0 -16
  75. data/lib/business_central/object/validation.rb +0 -57
  76. data/lib/business_central/object/vendor.rb +0 -42
  77. data/lib/business_central/object/vendor_purchase.rb +0 -13
  78. data/lib/core_ext/string.rb +0 -31
  79. data/test/business_central/object/account_test.rb +0 -67
  80. data/test/business_central/object/aged_account_payable_test.rb +0 -61
  81. data/test/business_central/object/aged_account_receivable_test.rb +0 -61
  82. data/test/business_central/object/balance_sheet_test.rb +0 -61
  83. data/test/business_central/object/cash_flow_statement_test.rb +0 -61
  84. data/test/business_central/object/company_information_test.rb +0 -81
  85. data/test/business_central/object/country_region_test.rb +0 -129
  86. data/test/business_central/object/currency_test.rb +0 -150
  87. data/test/business_central/object/customer_financial_detail_test.rb +0 -68
  88. data/test/business_central/object/customer_payment_journal_test.rb +0 -134
  89. data/test/business_central/object/customer_payment_test.rb +0 -141
  90. data/test/business_central/object/customer_sale_test.rb +0 -72
  91. data/test/business_central/object/customer_test.rb +0 -168
  92. data/test/business_central/object/default_dimension_test.rb +0 -170
  93. data/test/business_central/object/dimension_line_test.rb +0 -165
  94. data/test/business_central/object/dimension_test.rb +0 -65
  95. data/test/business_central/object/dimension_value_test.rb +0 -68
  96. data/test/business_central/object/employee_test.rb +0 -141
  97. data/test/business_central/object/general_ledger_entry_test.rb +0 -82
  98. data/test/business_central/object/income_statement_test.rb +0 -82
  99. data/test/business_central/object/irs1099_code_test.rb +0 -124
  100. data/test/business_central/object/item_category_test.rb +0 -122
  101. data/test/business_central/object/item_test.rb +0 -121
  102. data/test/business_central/object/journal_line_test.rb +0 -152
  103. data/test/business_central/object/journal_test.rb +0 -144
  104. data/test/business_central/object/payment_method_test.rb +0 -134
  105. data/test/business_central/object/payment_term_test.rb +0 -134
  106. data/test/business_central/object/purchase_invoice_line_test.rb +0 -123
  107. data/test/business_central/object/purchase_invoice_test.rb +0 -119
  108. data/test/business_central/object/retained_earning_statement_test.rb +0 -52
  109. data/test/business_central/object/sales_credit_memo_line_test.rb +0 -134
  110. data/test/business_central/object/sales_credit_memo_test.rb +0 -146
  111. data/test/business_central/object/sales_invoice_line_test.rb +0 -164
  112. data/test/business_central/object/sales_invoice_test.rb +0 -156
  113. data/test/business_central/object/sales_order_line_test.rb +0 -134
  114. data/test/business_central/object/sales_order_test.rb +0 -146
  115. data/test/business_central/object/sales_quote_line_test.rb +0 -134
  116. data/test/business_central/object/sales_quote_test.rb +0 -146
  117. data/test/business_central/object/shipment_method_test.rb +0 -133
  118. data/test/business_central/object/subscription_test.rb +0 -126
  119. data/test/business_central/object/tax_area_test.rb +0 -126
  120. data/test/business_central/object/tax_group_test.rb +0 -126
  121. data/test/business_central/object/time_registration_entry_test.rb +0 -126
  122. data/test/business_central/object/trial_balance_test.rb +0 -82
  123. data/test/business_central/object/units_of_measure_test.rb +0 -126
  124. data/test/business_central/object/validation_test.rb +0 -62
  125. data/test/business_central/object/vendor_purchase_test.rb +0 -82
  126. 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
- else
70
- if Response.unauthorized?(request.code.to_i)
71
- raise UnauthorizedException
72
- elsif Response.not_found?(request.code.to_i)
73
- raise NotFoundException
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
- if !response.fetch(:error, nil).nil?
76
- case response[:error][:code]
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?(Array)
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:, parent_path: [], child_path: '', child_id: '', child_code: '', filter: '')
19
+ def initialize(base_url:, object_path: [], object_id: '', object_code: '', filter: '')
18
20
  @base_url = base_url.to_s
19
- @parent_path = parent_path || []
20
- @child_path = child_path.to_s
21
- @child_id = child_id.to_s
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 @parent_path.empty?
38
-
39
- @parent_path.map do |parent|
40
- "/#{parent[:path]}(#{parent[:id]})" if !parent[:id].nil?
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 += "/#{@child_path}" if @child_path.present?
47
- url += "(#{@child_id})" if @child_id.present?
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.encode(object)
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])
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BusinessCentral
4
- VERSION = '1.0.5'
4
+ VERSION = '2.0.0'
5
5
  end
@@ -2,6 +2,8 @@
2
2
 
3
3
  module BusinessCentral
4
4
  class WebService
5
+ using Refinements::Strings
6
+
5
7
  DEFAULT_URL = 'https://api.businesscentral.dynamics.com/v2.0/production/ODataV4'
6
8
 
7
9
  attr_reader :url, :object_url
@@ -3,69 +3,18 @@ require 'oauth2/error'
3
3
  require 'net/http'
4
4
  require 'json'
5
5
 
6
- require 'core_ext/string'
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/account'
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/purchase_invoice'
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/attachment_test.rb
4
+ # rake test TEST=test/business_central/object/attachments_test.rb
5
5
 
6
- class BusinessCentral::Object::AttachmentTest < Minitest::Test
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.attachment(company_id: @company_id)
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\(#{test_parent_id},#{test_attachment_id}\)/)
111
+ stub_request(:delete, /attachments\(#{test_attachment_id}\)/)
113
112
  .to_return(status: 204)
114
113
 
115
- assert @attachment.destroy(parent_id: test_parent_id, attachment_id: test_attachment_id)
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 test_no_method_supported_for_find_all
13
- base = BusinessCentral::Object::Base.new(@client, {})
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 test_no_method_supported_for_find_by_id
21
- base = BusinessCentral::Object::Base.new(@client, {})
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 test_no_method_supported_for_where_query
29
- base = BusinessCentral::Object::Base.new(@client, {})
30
- set_object_method(base, [])
31
- assert_raises(BusinessCentral::NoSupportedMethod) do
32
- base.where("displayName eq '123'")
33
- end
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
- private
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
- def set_object_method(base, value)
39
- base.class.const_set('OBJECT_METHODS', value) if !base.class.const_defined?('OBJECT_METHODS')
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/company_test.rb
4
+ # rake test TEST=test/business_central/object/companies_test.rb
5
5
 
6
- class BusinessCentral::Object::CompanyTest < Minitest::Test
6
+ class BusinessCentral::Object::CompaniesTest < Minitest::Test
7
7
  def setup
8
8
  @client = BusinessCentral::Client.new
9
- @company = @client.company
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.picture(parent: 'vendors', parent_id: '123', company_id: @company_id)
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 test_build_object_url_with_template_values
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