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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb391f19d6cbf2649ddbe7eb0cf937d2657c140de32d6f6a8c1f92483a353f57
4
- data.tar.gz: c9ad222424c8b46bd9cdc4e282e5b583b2e795d1e1917c942b7da259e8380c92
3
+ metadata.gz: 0bea7c31120cb14daee81aa200a050a427d8ea281c924c87d04d3655a8d17841
4
+ data.tar.gz: 1894167530217ffd7e30624729896ad8f2ae30ffb9eb2c3d305d6695cd962638
5
5
  SHA512:
6
- metadata.gz: 3413af99b793f31b5913c726dd7fc3f5c48328f39321809b35da46f62100a12b529996eadfd3191d1581c52a718d749fa7eb2b808eb3f063f7a2e9c05975967e
7
- data.tar.gz: 5e96d79316386187618b362f3ea365f2f510b50622a50f2436ec45feaa32d435e839fcbf4ac20b8c7bb8dc8b33e98a5a7295b2d9beeac7257bd4f1d5e6a4aca5
6
+ metadata.gz: 73b14fc71f142fae957f851631c84517bc42a56ba4492f1c5c19ec7f50ea8a5b01eaaf33abee424f5748e61808778c3a950756c31929ad221f8e3ece186bed95
7
+ data.tar.gz: b39dc96f9302397ab319d2c266f7dcedf8336e7972996fed4b7e44c8f17c8afd395b9f0fff02c2210debb590abc43b34eeb76bcebe3ed96836c70d60f3f4cb25
data/README.md CHANGED
@@ -22,9 +22,9 @@ Or install it yourself as:
22
22
 
23
23
  ## Basic Usage
24
24
 
25
- This gem supports both authentication methods:
25
+ This gem supports both basic and OAuth2 authentication methods:
26
26
 
27
- https://docs.microsoft.com/en-us/dynamics-nav/api-reference/v1.0/endpoints-apis-for-dynamics
27
+ https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/api-reference/v2.0/endpoints-apis-for-dynamics
28
28
 
29
29
  ```Ruby
30
30
  require 'business_central'
@@ -55,24 +55,6 @@ client = BusinessCentral::Client.new(
55
55
  url: '<url>',
56
56
  default_company_id: '11111111-2222-3333-4444-555555555555'
57
57
  )
58
-
59
- # Find all vendors
60
- vendors = client.vendor.all
61
-
62
- # Find vendor by ID
63
- vendor = client.vendor.find('3f445b08-2ffd-4f9d-81a0-b82f0d9714c4')
64
-
65
- # Query vendor by display name
66
- vendor = client.vendor.where("displayName eq 'First Up Consultants'")
67
-
68
- # Create a new vendor
69
- vendor = client.vendor.create({ display_name: 'hello testing new vendor' })
70
-
71
- # Update an existing vendor by ID
72
- vendor = client.vendor.update('3f445b08-2ffd-4f9d-81a0-b82f0d9714c4', { phone_number: '1112' })
73
-
74
- # Delete a vendor
75
- client.vendor.destroy('f0730ada-b315-ea11-a813-000d3ad21e99')
76
58
  ```
77
59
 
78
60
  ### Oauth2 Authentication
@@ -102,26 +84,57 @@ client.authorize_from_token(
102
84
  expires_at: DateTime.current + 3600,
103
85
  expires_in: 3600
104
86
  )
87
+ ```
105
88
 
106
- # Find all vendors
107
- vendors = client.vendor.all
89
+ ### Calling objects
108
90
 
109
- # Find vendor by ID
110
- vendor = client.vendor.find('3f445b08-2ffd-4f9d-81a0-b82f0d9714c4')
91
+ https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/api-reference/v2.0/
92
+
93
+ ```Ruby
94
+ begin
95
+ # Find all vendors
96
+ client.vendors.all
111
97
 
112
- # Query vendor by display name
113
- vendor = client.vendor.where("displayName eq 'First Up Consultants'")
98
+ # Find vendor by ID
99
+ client.vendors.find('557d12d7-25f5-ec11-82f1-000d3a299c8c')
114
100
 
115
- # Create a new vendor
116
- vendor = client.vendor.create({ display_name: 'hello testing' })
101
+ # Query vendor by display name
102
+ client.vendors.where("displayName eq 'First Up Consultants'")
117
103
 
118
- # Update an existing vendor by ID
119
- vendor = client.vendor.update('3f445b08-2ffd-4f9d-81a0-b82f0d9714c4', { phone_number: '1112' })
104
+ # Create a new vendor
105
+ client.vendors.create({ display_name: 'hello testing' })
120
106
 
121
- # Delete a vendor
122
- client.vendor.destroy('f0730ada-b315-ea11-a813-000d3ad21e99')
107
+ # Update an existing vendor by ID
108
+ client.vendors.update('39a7ef39-ba0f-ed11-90ed-00224895e26b', { phone_number: '1112' })
109
+
110
+ # Delete a vendor
111
+ client.vendors.destroy('39a7ef39-ba0f-ed11-90ed-00224895e26b')
112
+
113
+ # ---------
114
+
115
+ # Find all vendor default dimensions
116
+ client.vendors(id: '557d12d7-25f5-ec11-82f1-000d3a299c8c').default_dimensions.all
117
+
118
+ # Query vendor default dimensions by dimension code
119
+ client.vendors(id: '557d12d7-25f5-ec11-82f1-000d3a299c8c').default_dimensions.where("dimensionCode eq 'DEPARTMENT'")
120
+
121
+ # Create a new vendor default dimension
122
+ client.vendors(id: '557d12d7-25f5-ec11-82f1-000d3a299c8c').default_dimensions.create({ dimension_code: 'DEPARTMENT' })
123
+
124
+ # Update an existing vendor default dimension
125
+ client.vendors(id: '557d12d7-25f5-ec11-82f1-000d3a299c8c').default_dimensions.update('6576ecf4-25f5-ec11-82f1-000d3a299c8c', { dimension_code: 'NEW_DEPARTMENT' })
126
+
127
+ # Delete a vendor default dimension
128
+ client.vendors(id: '557d12d7-25f5-ec11-82f1-000d3a299c8c').default_dimensions.destroy('6576ecf4-25f5-ec11-82f1-000d3a299c8c')
129
+ rescue StandardError => e
130
+ puts e.message
131
+ rescue BusinessCentral::ApiException => e
132
+ puts e.message
133
+ end
123
134
  ```
124
135
 
136
+ All objects should be callable, assuming the endpoint exists.
137
+
125
138
  ### OData Web Services
126
139
 
127
140
  https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/webservices/odata-web-services
@@ -2,7 +2,9 @@
2
2
 
3
3
  module BusinessCentral
4
4
  class Client
5
- extend BusinessCentral::Object::ObjectHelper
5
+ using Refinements::Strings
6
+
7
+ include BusinessCentral::Object::ObjectHelper
6
8
 
7
9
  DEFAULT_LOGIN_URL = 'https://login.microsoftonline.com/common'
8
10
 
@@ -21,57 +23,6 @@ module BusinessCentral
21
23
 
22
24
  alias access_token oauth2_access_token
23
25
 
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
30
- object :company
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
47
- object :purchase_invoice
48
- object :purchase_invoice_line
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
74
-
75
26
  def initialize(options = {})
76
27
  opts = options.dup
77
28
  @username = opts.delete(:username)
@@ -112,6 +63,8 @@ module BusinessCentral
112
63
 
113
64
  def refresh_token
114
65
  @oauth2_access_token.refresh!
66
+ rescue OAuth2::Error => e
67
+ handle_error(e)
115
68
  end
116
69
 
117
70
  def web_service
@@ -136,6 +89,8 @@ module BusinessCentral
136
89
  case error.code
137
90
  when 'invalid_client'
138
91
  raise InvalidClientException
92
+ when 'invalid_grant'
93
+ raise InvalidGrantException, error.message
139
94
  end
140
95
  end
141
96
  end
@@ -3,15 +3,10 @@
3
3
  module BusinessCentral
4
4
  class BusinessCentralError < StandardError; end
5
5
 
6
- class InvalidClientException < BusinessCentralError
7
- def message
8
- 'Invalid client setup'
9
- end
10
- end
11
-
12
6
  class ApiException < BusinessCentralError
13
7
  def initialize(message)
14
8
  @message = message
9
+ super
15
10
  end
16
11
 
17
12
  attr_reader :message
@@ -35,41 +30,28 @@ module BusinessCentral
35
30
  end
36
31
  end
37
32
 
38
- class InvalidObjectException < BusinessCentralError
39
- def initialize(errors)
40
- @errors = errors
41
- end
42
-
33
+ class InvalidObjectURLException < BusinessCentralError
43
34
  def message
44
- @errors.each { |error| "#{error[:field]} - #{error[:message]}" }
35
+ 'Object URL missing for request'
45
36
  end
46
37
  end
47
38
 
48
- class NoSupportedMethod < BusinessCentralError
49
- def initialize(method, allowed_methods)
50
- @method = method
51
- @allowed_methods = allowed_methods
52
- end
53
-
39
+ class InvalidClientException < BusinessCentralError
54
40
  def message
55
- methods_allowed = @allowed_methods.join(', ')
56
- "#{@method} method is currently not support. Allowed methods are: #{methods_allowed}"
41
+ 'Invalid client setup'
57
42
  end
58
43
  end
59
44
 
60
- class InvalidArgumentException < BusinessCentralError
61
- def initialize(message)
62
- @message = message
45
+ class InvalidGrantException < BusinessCentralError
46
+ def initialize(error_message)
47
+ @error_message = error_message
48
+ super
63
49
  end
64
50
 
65
- def message
66
- "Invalid argument entered - #{@message}"
67
- end
68
- end
51
+ attr_reader :error_message
69
52
 
70
- class InvalidObjectURLException < BusinessCentralError
71
53
  def message
72
- 'Object URL missing for request'
54
+ 'The provided grant has expired due to it being revoked, a fresh auth token is needed'
73
55
  end
74
56
  end
75
57
  end
@@ -2,18 +2,17 @@
2
2
 
3
3
  module BusinessCentral
4
4
  module Object
5
- class Attachment < Base
5
+ class Attachments < Base
6
+ using Refinements::Strings
7
+
6
8
  OBJECT = 'attachments'
7
9
 
8
- OBJECT_METHODS = %i[
9
- get
10
- post
11
- patch
12
- delete
13
- ].freeze
10
+ def initialize(client, **args)
11
+ super(client, **args.merge!({ object_name: OBJECT }))
12
+ end
14
13
 
15
14
  def update(parent_id:, attachment_id:, **params)
16
- url = "#{build_url(child_path: OBJECT)}(parentId=#{parent_id},id=#{attachment_id})/content"
15
+ url = "#{build_url}(parentId=#{parent_id},id=#{attachment_id})/content"
17
16
  Request.call(:patch, @client, url, etag: '', params: {}) do |request|
18
17
  request['Content-Type'] = 'application/json'
19
18
  request['If-Match'] = 'application/json'
@@ -21,9 +20,8 @@ module BusinessCentral
21
20
  end
22
21
  end
23
22
 
24
- def destroy(parent_id:, attachment_id:)
25
- url = "#{build_url(child_path: OBJECT)}(#{parent_id},#{attachment_id})"
26
- Request.call(:delete, @client, url, etag: '')
23
+ def destroy(id)
24
+ Request.call(:delete, @client, build_url(object_id: id), etag: '')
27
25
  end
28
26
  end
29
27
  end
@@ -3,154 +3,82 @@
3
3
  module BusinessCentral
4
4
  module Object
5
5
  class Base
6
- include ArgumentHelper
6
+ using Refinements::Strings
7
7
 
8
- attr_reader :client, :parent_path, :path
8
+ attr_reader :client
9
9
 
10
- attr_writer :id, :company_id
11
-
12
- def initialize(client, args = {})
10
+ def initialize(client, **args)
13
11
  @client = client
14
- @id = id(args)
15
- @company_id = company_id(args)
16
- @parent_path = @company_id.nil? ? [] : [
17
- {
18
- path: 'companies',
19
- id: @company_id
20
- }
21
- ]
12
+ @object_path = args.fetch(
13
+ :object_path,
14
+ [
15
+ {
16
+ path: 'companies',
17
+ id: args.fetch(:company_id, client.default_company_id)
18
+ },
19
+ {
20
+ path: args.fetch(:object_name, '').to_s.to_camel_case,
21
+ id: args.fetch(:id, nil)
22
+ }
23
+ ]
24
+ )
22
25
  end
23
26
 
24
27
  def find_all
25
- if !method_supported?(:get)
26
- raise BusinessCentral::NoSupportedMethod.new(:get, object_methods)
27
- end
28
-
29
- Request.get(@client, build_url(parent_path: @parent_path, child_path: object_name))
28
+ Request.get(@client, build_url)
30
29
  end
31
30
  alias all find_all
32
31
 
33
32
  def find_by_id(id)
34
- if !method_supported?(:get)
35
- raise BusinessCentral::NoSupportedMethod.new(:get, object_methods)
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
- )
33
+ Request.get(@client, build_url(object_id: id))
46
34
  end
47
35
  alias find find_by_id
48
36
 
49
37
  def where(query = '', *values)
50
- if !method_supported?(:get)
51
- raise BusinessCentral::NoSupportedMethod.new(:get, object_methods)
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
- )
38
+ Request.get(@client, build_url(filter: FilterQuery.sanitize(query, values)))
62
39
  end
63
40
 
64
41
  def create(params = {})
65
- if !method_supported?(:post)
66
- raise BusinessCentral::NoSupportedMethod.new(:post, object_methods)
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)
42
+ Request.post(@client, build_url, params)
71
43
  end
72
44
 
73
45
  def update(id, params = {})
74
- if !method_supported?(:patch)
75
- raise BusinessCentral::NoSupportedMethod.new(:patch, object_methods)
76
- end
77
-
78
46
  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
- )
47
+ Request.patch(@client, build_url(object_id: id), object[:etag], params)
90
48
  end
91
49
 
92
50
  def destroy(id)
93
- if !method_supported?(:delete)
94
- raise BusinessCentral::NoSupportedMethod.new(:delete, object_methods)
95
- end
96
-
97
51
  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
- )
52
+ Request.delete(@client, build_url(object_id: id), object[:etag])
53
+ end
54
+ alias delete destroy
55
+
56
+ def method_missing(object_name, **params)
57
+ @object_path << {
58
+ path: object_name.to_s.to_camel_case,
59
+ id: params.fetch(:id, nil)
60
+ }
61
+ if BusinessCentral::Object.const_defined?(object_name.to_s.to_class_sym)
62
+ klass = BusinessCentral::Object.const_get(object_name.to_s.to_class_sym)
63
+ return klass.new(client, **params.merge!({ object_path: @object_path }))
64
+ end
65
+ self
107
66
  end
108
67
 
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(', ')}"
68
+ def respond_to_missing?(_object_name, *_params)
69
+ true
115
70
  end
116
71
 
117
- def build_url(parent_path: [], child_path: '', child_id: '', filter: '')
72
+ private
73
+
74
+ def build_url(object_id: '', filter: '')
118
75
  URLBuilder.new(
119
76
  base_url: client.url,
120
- parent_path: parent_path,
121
- child_path: child_path,
122
- child_id: child_id,
77
+ object_path: @object_path,
78
+ object_id: object_id,
123
79
  filter: filter
124
80
  ).build
125
81
  end
126
-
127
- private
128
-
129
- def object_name
130
- self.class.const_get(:OBJECT)
131
- end
132
-
133
- def object_validation
134
- if self.class.const_defined?(:OBJECT_VALIDATION)
135
- self.class.const_get(:OBJECT_VALIDATION)
136
- else
137
- []
138
- end
139
- end
140
-
141
- def object_methods
142
- self.class.const_get(:OBJECT_METHODS)
143
- end
144
-
145
- def object_parent_name
146
- self.class.const_get(:OBJECT_PARENTS)
147
- end
148
-
149
- def method_supported?(method)
150
- return true if object_methods.include?(method)
151
-
152
- false
153
- end
154
82
  end
155
83
  end
156
84
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BusinessCentral
4
+ module Object
5
+ class Companies < Base
6
+ using Refinements::Strings
7
+
8
+ OBJECT = 'companies'
9
+
10
+ def initialize(client, **args)
11
+ super(client, **args.merge!({ object_name: OBJECT }))
12
+ @object_path = [{
13
+ path: OBJECT,
14
+ id: nil
15
+ }]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -3,6 +3,8 @@
3
3
  module BusinessCentral
4
4
  module Object
5
5
  class FilterQuery
6
+ using Refinements::Strings
7
+
6
8
  extend URLHelper
7
9
 
8
10
  class << self
@@ -3,46 +3,19 @@
3
3
  module BusinessCentral
4
4
  module Object
5
5
  module ObjectHelper
6
- include ArgumentHelper
7
-
8
- def object(object_name, *_params)
9
- define_method(object_name) do |argument = nil|
10
- object = "@#{object_name}_cache".to_sym
11
- if argument.nil?
12
- if !instance_variable_defined?(object)
13
- instance_variable_set(
14
- object,
15
- BusinessCentral::Object.const_get(
16
- object_name.to_s.to_camel_case(true).to_s.to_sym
17
- ).new(self, argument)
18
- )
19
- else
20
- instance_variable_get(object)
21
- end
22
- else
23
- instance_variable_set(
24
- object,
25
- BusinessCentral::Object.const_get(
26
- object_name.to_s.to_camel_case(true).to_s.to_sym
27
- ).new(self, argument)
28
- )
29
- end
6
+ using Refinements::Strings
7
+
8
+ def method_missing(object_name, **params)
9
+ if BusinessCentral::Object.const_defined?(object_name.to_s.to_class_sym)
10
+ klass = BusinessCentral::Object.const_get(object_name.to_s.to_class_sym)
11
+ klass.new(self, **params)
12
+ else
13
+ BusinessCentral::Object::Base.new(self, **params.merge!({ object_name: object_name }))
30
14
  end
31
15
  end
32
16
 
33
- def navigation(object_name, *_params)
34
- define_method(object_name) do |argument = nil|
35
- object = "@#{object_name}_cache".to_sym
36
- instance_variable_set(
37
- object,
38
- BusinessCentral::Object.const_get(object_name.to_s.to_camel_case(true).to_s.to_sym).new(
39
- client,
40
- company_id: company_id(argument),
41
- parent: self.class.const_get(:OBJECT),
42
- parent_id: id(argument)
43
- )
44
- )
45
- end
17
+ def respond_to_missing?(_object_name, _include_all)
18
+ true
46
19
  end
47
20
  end
48
21
  end
@@ -3,47 +3,11 @@
3
3
  module BusinessCentral
4
4
  module Object
5
5
  class Picture < Base
6
- OBJECT = 'picture'
6
+ using Refinements::Strings
7
7
 
8
- OBJECT_METHODS = %i[
9
- get
10
- post
11
- patch
12
- delete
13
- ].freeze
14
-
15
- OBJECT_PARENTS = %w[
16
- items
17
- customers
18
- vendors
19
- employees
20
- ].freeze
21
-
22
- def initialize(client, parent:, parent_id:, **args)
23
- return if !valid_parent?(parent)
24
-
25
- super(client, args)
26
- @parent_path << {
27
- path: parent,
28
- id: parent_id
29
- }
30
- @parent_id = parent_id
31
- end
32
-
33
- def create(data)
34
- url = "#{build_url(parent_path: @parent_path, child_path: object_name)}(#{@parent_id})/content"
35
- Request.call(:patch, @client, url, etag: '', params: {}) do |request|
36
- request['Content-Type'] = 'application/octet-stream'
37
- request['If-Match'] = '*'
38
- request.body = data
39
- end
40
- end
41
-
42
- def update(data)
43
- url = "#{build_url(parent_path: @parent_path, child_path: object_name)}(#{@parent_id})"
44
- object = Request.get(@client, url)
45
- url = "#{build_url(parent_path: @parent_path, child_path: object_name)}(#{@parent_id})/content"
46
- Request.call(:patch, @client, url, etag: object[:etag], params: {}) do |request|
8
+ def update(id, data)
9
+ object = find_all
10
+ Request.call(:patch, @client, "#{build_url(object_id: id)}/content", etag: object[:etag], params: {}) do |request|
47
11
  request['Content-Type'] = 'application/octet-stream'
48
12
  request.body = data
49
13
  end