business-central 1.0.0 → 1.0.5

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 +52 -2
  4. data/lib/business_central/client.rb +87 -39
  5. data/lib/business_central/exceptions.rb +30 -10
  6. data/lib/business_central/object/account.rb +4 -2
  7. data/lib/business_central/object/aged_account_payable.rb +13 -0
  8. data/lib/business_central/object/aged_account_receivable.rb +13 -0
  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 +13 -0
  12. data/lib/business_central/object/base.rb +100 -49
  13. data/lib/business_central/object/cash_flow_statement.rb +13 -0
  14. data/lib/business_central/object/company.rb +9 -2
  15. data/lib/business_central/object/company_information.rb +14 -0
  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 +28 -22
  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 +32 -0
  62. data/lib/business_central/object/validation.rb +11 -8
  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 +24 -5
  70. data/test/business_central/object/aged_account_payable_test.rb +61 -0
  71. data/test/business_central/object/aged_account_receivable_test.rb +61 -0
  72. data/test/business_central/object/attachment_test.rb +117 -0
  73. data/test/business_central/object/balance_sheet_test.rb +61 -0
  74. data/test/business_central/object/base_test.rb +41 -0
  75. data/test/business_central/object/cash_flow_statement_test.rb +61 -0
  76. data/test/business_central/object/company_information_test.rb +81 -0
  77. data/test/business_central/object/company_test.rb +24 -11
  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 -23
  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 -21
  102. data/test/business_central/object/purchase_invoice_test.rb +15 -21
  103. data/test/business_central/object/request_test.rb +75 -18
  104. data/test/business_central/object/response_test.rb +29 -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 -22
  124. data/test/business_central/web_service_test.rb +95 -0
  125. data/test/business_central_test.rb +3 -1
  126. data/test/test_helper.rb +16 -6
  127. metadata +199 -33
  128. data/lib/business_central/object/helper.rb +0 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aa79fbf2f11dba355ca4486b9bed998dc3a481aa2c52f66d311bf1525e3f8620
4
- data.tar.gz: 13910cff1f4af8e26fe89921c1021e2799f074134ebb797163aff3aac863e510
3
+ metadata.gz: eb391f19d6cbf2649ddbe7eb0cf937d2657c140de32d6f6a8c1f92483a353f57
4
+ data.tar.gz: c9ad222424c8b46bd9cdc4e282e5b583b2e795d1e1917c942b7da259e8380c92
5
5
  SHA512:
6
- metadata.gz: 67aa6439003bd9b3ec1599a97cfd2b235af9c68c140cac188609c0e3d496aac0a67447548c7acb9eef4182d7888a14cf5996bfbb48584db08ed9fb7ac97a8c4e
7
- data.tar.gz: b5dfd537dd2c023f71196d8960b9bcc5b8ae88115f62bb53c1840531862312689eaba558b5903d0979b2c87070203c80a85294b69078efc014eb0caadf3d8dd2
6
+ metadata.gz: 3413af99b793f31b5913c726dd7fc3f5c48328f39321809b35da46f62100a12b529996eadfd3191d1581c52a718d749fa7eb2b808eb3f063f7a2e9c05975967e
7
+ data.tar.gz: 5e96d79316386187618b362f3ea365f2f510b50622a50f2436ec45feaa32d435e839fcbf4ac20b8c7bb8dc8b33e98a5a7295b2d9beeac7257bd4f1d5e6a4aca5
data/README.md CHANGED
@@ -26,6 +26,22 @@ This gem supports both authentication methods:
26
26
 
27
27
  https://docs.microsoft.com/en-us/dynamics-nav/api-reference/v1.0/endpoints-apis-for-dynamics
28
28
 
29
+ ```Ruby
30
+ require 'business_central'
31
+
32
+ client = BusinessCentral::Client.new(
33
+ username: '<username>', # Basic authentication username
34
+ password: '<password>', # Basic authentication password
35
+ url: '<url>', # URL for API defaults to https://api.businesscentral.dynamics.com/v2.0/production/api/v1.0
36
+ web_service_url: '<url>', # URL for custom web services defaults to https://api.businesscentral.dynamics.com/v2.0/production/ODataV4
37
+ application_id: '<application_id>', # Oauth2 authentication application ID
38
+ secret_key: '<application_secret_key>', # OAuth2 authentication application secret key
39
+ oauth2_login_url: '<url>', # OAuth2 authentication login URL defaults to https://login.microsoftonline.com/common
40
+ default_company_id: '<company_id>', # Default company ID used in all requests (if required)
41
+ debug: false # Output requests to console
42
+ )
43
+ ```
44
+
29
45
  ### Basic Authentication:
30
46
 
31
47
  https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-develop-connect-apps#setting-up-basic-authentication
@@ -36,26 +52,27 @@ require 'business_central'
36
52
  client = BusinessCentral::Client.new(
37
53
  username: '<username>',
38
54
  password: '<password>',
39
- url: '<url>'
55
+ url: '<url>',
56
+ default_company_id: '11111111-2222-3333-4444-555555555555'
40
57
  )
41
58
 
42
59
  # Find all vendors
43
- vendors = client.vendor(company_id: '3a502065-2a08-4c3b-9468-fb83642d3d3a').find_all
60
+ vendors = client.vendor.all
44
61
 
45
62
  # Find vendor by ID
46
- vendor = client.vendor(company_id: '3a502065-2a08-4c3b-9468-fb83642d3d3a').find_by_id('3f445b08-2ffd-4f9d-81a0-b82f0d9714c4')
63
+ vendor = client.vendor.find('3f445b08-2ffd-4f9d-81a0-b82f0d9714c4')
47
64
 
48
65
  # Query vendor by display name
49
- vendor = client.vendor(company_id: '3a502065-2a08-4c3b-9468-fb83642d3d3a').where("displayName eq 'First Up Consultants'")
66
+ vendor = client.vendor.where("displayName eq 'First Up Consultants'")
50
67
 
51
68
  # Create a new vendor
52
- vendor = client.vendor(company_id: '3a502065-2a08-4c3b-9468-fb83642d3d3a').create({ display_name: 'hello testing new vendor' })
69
+ vendor = client.vendor.create({ display_name: 'hello testing new vendor' })
53
70
 
54
71
  # Update an existing vendor by ID
55
- vendor = client.vendor(company_id: '3a502065-2a08-4c3b-9468-fb83642d3d3a').update('3f445b08-2ffd-4f9d-81a0-b82f0d9714c4', { phone_number: '1112' })
72
+ vendor = client.vendor.update('3f445b08-2ffd-4f9d-81a0-b82f0d9714c4', { phone_number: '1112' })
56
73
 
57
74
  # Delete a vendor
58
- client.vendor(company_id: '3a502065-2a08-4c3b-9468-fb83642d3d3a').destroy('f0730ada-b315-ea11-a813-000d3ad21e99')
75
+ client.vendor.destroy('f0730ada-b315-ea11-a813-000d3ad21e99')
59
76
  ```
60
77
 
61
78
  ### Oauth2 Authentication
@@ -67,10 +84,10 @@ require 'business_central'
67
84
 
68
85
  # Create client - used to connect to the API
69
86
  client = BusinessCentral::Client.new(
70
- tenant_id: '<tenant_id>',
71
87
  application_id: '<application_id>',
72
88
  secret_key: '<application_secret_key>',
73
- url: '<url>'
89
+ url: '<url>',
90
+ default_company_id: '11111111-2222-3333-4444-555555555555'
74
91
  )
75
92
 
76
93
  # Controller endpoint 1
@@ -87,22 +104,48 @@ client.authorize_from_token(
87
104
  )
88
105
 
89
106
  # Find all vendors
90
- vendors = client.vendor(company_id: '3a502065-2a08-4c3b-9468-fb83642d3d3a').find_all
107
+ vendors = client.vendor.all
91
108
 
92
109
  # Find vendor by ID
93
- vendor = client.vendor(company_id: '3a502065-2a08-4c3b-9468-fb83642d3d3a').find_by_id('3f445b08-2ffd-4f9d-81a0-b82f0d9714c4')
110
+ vendor = client.vendor.find('3f445b08-2ffd-4f9d-81a0-b82f0d9714c4')
94
111
 
95
112
  # Query vendor by display name
96
- vendor = client.vendor(company_id: '3a502065-2a08-4c3b-9468-fb83642d3d3a').where("displayName eq 'First Up Consultants'")
113
+ vendor = client.vendor.where("displayName eq 'First Up Consultants'")
97
114
 
98
115
  # Create a new vendor
99
- vendor = client.vendor(company_id: '3a502065-2a08-4c3b-9468-fb83642d3d3a').create({ display_name: 'hello testing' })
116
+ vendor = client.vendor.create({ display_name: 'hello testing' })
100
117
 
101
118
  # Update an existing vendor by ID
102
- vendor = client.vendor(company_id: '3a502065-2a08-4c3b-9468-fb83642d3d3a').update('3f445b08-2ffd-4f9d-81a0-b82f0d9714c4', { phone_number: '1112' })
119
+ vendor = client.vendor.update('3f445b08-2ffd-4f9d-81a0-b82f0d9714c4', { phone_number: '1112' })
103
120
 
104
121
  # Delete a vendor
105
- client.vendor(company_id: '3a502065-2a08-4c3b-9468-fb83642d3d3a').destroy('f0730ada-b315-ea11-a813-000d3ad21e99')
122
+ client.vendor.destroy('f0730ada-b315-ea11-a813-000d3ad21e99')
123
+ ```
124
+
125
+ ### OData Web Services
126
+
127
+ https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/webservices/odata-web-services
128
+
129
+ ```Ruby
130
+ require 'business_central'
131
+
132
+ client = BusinessCentral::Client.new(
133
+ username: '<username>',
134
+ password: '<password>',
135
+ web_service_url: '<url>'
136
+ )
137
+
138
+ # Query a record
139
+ company = client.web_service.object("Company('?')/Purchase_Order", 'My Company').get
140
+
141
+ # Create a record
142
+ client.web_service.object("Company('?')/Purchase_Order", 'My Company').post({})
143
+
144
+ # Update a record
145
+ client.web_service.object("Company('?')/Purchase_Order", 'My Company').patch({})
146
+
147
+ # Delete a record
148
+ client.web_service.object("Company('?')/Purchase_Order", 'My Company').delete
106
149
  ```
107
150
 
108
151
  ## Development
@@ -5,18 +5,68 @@ require 'json'
5
5
 
6
6
  require 'core_ext/string'
7
7
 
8
- require 'business_central/object/helper'
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'
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'
15
25
  require 'business_central/object/company'
16
- require 'business_central/object/vendor'
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'
17
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'
48
+ require 'business_central/object/picture'
18
49
  require 'business_central/object/purchase_invoice'
19
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'
20
69
 
21
70
  require 'business_central/exceptions'
22
71
  require 'business_central/client'
72
+ require 'business_central/web_service'
@@ -1,94 +1,142 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BusinessCentral
2
4
  class Client
3
- extend BusinessCentral::Object::Helper
5
+ extend BusinessCentral::Object::ObjectHelper
6
+
7
+ DEFAULT_LOGIN_URL = 'https://login.microsoftonline.com/common'
4
8
 
5
- DEFAULT_URL = 'https://api.businesscentral.dynamics.com/v1.0/api/beta'.freeze
9
+ DEFAULT_URL = 'https://api.businesscentral.dynamics.com/v2.0/production/api/v1.0'
6
10
 
7
- attr_reader :tenant_id,
8
- :username,
11
+ attr_reader :username,
9
12
  :password,
10
13
  :application_id,
11
14
  :secret_key,
12
15
  :url,
13
- :oauth2_client
16
+ :web_service_url,
17
+ :oauth2_login_url,
18
+ :oauth2_access_token,
19
+ :default_company_id,
20
+ :debug
14
21
 
15
- alias_method :access_token, :oauth2_client
22
+ alias access_token oauth2_access_token
16
23
 
17
24
  object :account
25
+ object :aged_account_payable
26
+ object :aged_account_receivable
27
+ object :attachment
28
+ object :balance_sheet
29
+ object :cash_flow_statement
18
30
  object :company
19
- object :vendor
31
+ object :company_information
32
+ object :country_region
33
+ object :currency
34
+ object :customer
35
+ object :customer_financial_detail
36
+ object :customer_payment
37
+ object :customer_payment_journal
38
+ object :customer_sale
39
+ object :default_dimension
40
+ object :dimension
41
+ object :dimension_line
42
+ object :dimension_value
43
+ object :employee
44
+ object :general_ledger_entry
45
+ object :income_statement
46
+ object :irs1099_code
20
47
  object :purchase_invoice
21
48
  object :purchase_invoice_line
22
49
  object :item
50
+ object :item_category
51
+ object :journal
52
+ object :journal_line
53
+ object :payment_method
54
+ object :payment_term
55
+ object :picture
56
+ object :retained_earning_statement
57
+ object :sales_invoice
58
+ object :sales_invoice_line
59
+ object :sales_credit_memo
60
+ object :sales_credit_memo_line
61
+ object :sales_order
62
+ object :sales_order_line
63
+ object :sales_quote
64
+ object :sales_quote_line
65
+ object :shipment_method
66
+ object :subscription
67
+ object :tax_area
68
+ object :tax_group
69
+ object :time_registration_entry
70
+ object :trial_balance
71
+ object :units_of_measure
72
+ object :vendor
73
+ object :vendor_purchase
23
74
 
24
75
  def initialize(options = {})
25
76
  opts = options.dup
26
- @tenant_id = opts.delete(:tenant_id)
27
77
  @username = opts.delete(:username)
28
78
  @password = opts.delete(:password)
29
79
  @url = opts.delete(:url) || DEFAULT_URL
80
+ @web_service_url = opts.delete(:web_service_url)
30
81
  @application_id = opts.delete(:application_id)
31
82
  @secret_key = opts.delete(:secret_key)
83
+ @oauth2_login_url = opts.delete(:oauth2_login_url) || DEFAULT_LOGIN_URL
84
+ @default_company_id = opts.delete(:default_company_id)
85
+ @debug = opts.delete(:debug) || false
32
86
  end
33
87
 
34
88
  def authorize(params = {}, oauth_authorize_callback: '')
35
89
  params[:redirect_uri] = oauth_authorize_callback
36
90
  begin
37
91
  oauth2_client.auth_code.authorize_url(params)
38
- rescue Oauth2::Error => error
39
- handle_error(error)
92
+ rescue OAuth2::Error => e
93
+ handle_error(e)
40
94
  end
41
95
  end
42
96
 
43
97
  def request_token(code = '', oauth_token_callback: '')
44
- begin
45
- oauth2_client.auth_code.get_token(code, redirect_uri: oauth_token_callback)
46
- rescue OAuth2::Error => error
47
- handle_error(error)
48
- end
98
+ oauth2_client.auth_code.get_token(code, redirect_uri: oauth_token_callback)
99
+ rescue OAuth2::Error => e
100
+ handle_error(e)
49
101
  end
50
102
 
51
103
  def authorize_from_token(token: '', refresh_token: '', expires_at: nil, expires_in: nil)
52
- @oauth2_client = OAuth2::AccessToken.new(
104
+ @oauth2_access_token = OAuth2::AccessToken.new(
53
105
  oauth2_client,
54
106
  token,
55
107
  refresh_token: refresh_token,
56
108
  expires_at: expires_at,
57
- expires_in: expires_in,
109
+ expires_in: expires_in
58
110
  )
59
111
  end
60
112
 
61
113
  def refresh_token
62
- @oauth2_client.refresh!
114
+ @oauth2_access_token.refresh!
115
+ end
116
+
117
+ def web_service
118
+ @web_service ||= BusinessCentral::WebService.new(client: self, url: web_service_url)
63
119
  end
64
120
 
65
121
  private
66
122
 
67
123
  def oauth2_client
68
- if @oauth2_client.nil?
69
- @oauth2_client = OAuth2::Client.new(
70
- @application_id,
71
- @secret_key,
72
- {
73
- site: "https://login.windows.net/#{@tenant_id}",
74
- authorize_url: 'oauth2/authorize?resource=https://api.businesscentral.dynamics.com',
75
- token_url: 'oauth2/token?resource=https://api.businesscentral.dynamics.com'
76
- }
77
- )
78
- end
79
-
80
- return @oauth2_client
124
+ OAuth2::Client.new(
125
+ @application_id,
126
+ @secret_key,
127
+ site: @oauth2_login_url,
128
+ authorize_url: 'oauth2/authorize?resource=https://api.businesscentral.dynamics.com',
129
+ token_url: 'oauth2/token?resource=https://api.businesscentral.dynamics.com'
130
+ )
81
131
  end
82
132
 
83
133
  def handle_error(error)
84
- if error.code.present?
85
- case error.code
86
- when 'invalid_client'
87
- raise InvalidClientException.new
88
- end
89
- else
90
- raise ApiException.new(error.message)
134
+ raise ApiException, error.message if error.code.nil?
135
+
136
+ case error.code
137
+ when 'invalid_client'
138
+ raise InvalidClientException
91
139
  end
92
140
  end
93
141
  end
94
- end
142
+ end
@@ -1,10 +1,11 @@
1
- module BusinessCentral
1
+ # frozen_string_literal: true
2
2
 
3
+ module BusinessCentral
3
4
  class BusinessCentralError < StandardError; end
4
5
 
5
6
  class InvalidClientException < BusinessCentralError
6
7
  def message
7
- 'Invalid application setup'
8
+ 'Invalid client setup'
8
9
  end
9
10
  end
10
11
 
@@ -13,9 +14,7 @@ module BusinessCentral
13
14
  @message = message
14
15
  end
15
16
 
16
- def message
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 do |error|
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,24 @@ module BusinessCentral
49
52
  end
50
53
 
51
54
  def message
52
- "#{method} method is currently not support. Allowed methods are: #{allowed_methods.join(', ')}"
55
+ methods_allowed = @allowed_methods.join(', ')
56
+ "#{@method} method is currently not support. Allowed methods are: #{methods_allowed}"
57
+ end
58
+ end
59
+
60
+ class InvalidArgumentException < BusinessCentralError
61
+ def initialize(message)
62
+ @message = message
63
+ end
64
+
65
+ def message
66
+ "Invalid argument entered - #{@message}"
67
+ end
68
+ end
69
+
70
+ class InvalidObjectURLException < BusinessCentralError
71
+ def message
72
+ 'Object URL missing for request'
53
73
  end
54
74
  end
55
- end
75
+ end