apimatic_core 0.1.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c581361453ae63c619ca7b7bcc5b2e4cb5cd2afe4ab1f7584462a0698ab8be3d
4
- data.tar.gz: 51c745d2ac8600da653324628f1cf4e00e2059ceb12e22f150f49ec2ed3a9f03
3
+ metadata.gz: d29b21091f739c5f0023b01fd70b174a41b6b25637248a02f5be3283874a25eb
4
+ data.tar.gz: f21de623d02c62af2818602f81657983883d0885187836e609b16ddf890d0d3c
5
5
  SHA512:
6
- metadata.gz: 1a87d5f62193c6f46e4fda2289d5c4a8cae5d716ee35723f030d6ad97ffc569ffec23019e89065a5882ea806aec791ea121bc8f0e326496a923a49feb0c1b484
7
- data.tar.gz: 44acde48930a6e621608d30fb646e9f467e0fba89c07520016e55e2b017b17f239ede9bcc0de1e79537e5303da469b91f1fd2f1844a212f10bd92a40b48cd103
6
+ metadata.gz: abd28994bd548acfd533bcdd1879d2d8c5ebf67198b97d3172f559d537a1ea9e688c0b5ff076849c4d818d32389814f0b5e42090d11aaf3866de73e626ff1ea6
7
+ data.tar.gz: 011162e182da68a14d8e820ddf3ca2932f2658afc0e2a15836f042a02d104476888d90c4ce0932f6d47ee8e3df5c5bc23b489bd16bf9f628551ff9183492dcf9
data/LICENSE CHANGED
@@ -1,26 +1,21 @@
1
- APIMATIC REFERENCE SOURCE LICENSE
2
-
3
- This license governs the use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.
4
-
5
- 1. Definitions
6
- The terms "reproduce," "reproduction" and "distribution" have the same meaning here as under U.S. copyright law.
7
-
8
- "You" means the licensee of the software.
9
-
10
- "Your company" means the company you worked for when you downloaded the software.
11
-
12
- "Reference use" means the use of the software within your company as a reference, in read-only form, for the sole purposes of debugging your products, maintaining your products, or enhancing the interoperability of your products with the software, and specifically excludes the right to distribute the software outside of your company.
13
-
14
- "Licensed patents" means any Licensor patent claims which read directly on the software as distributed by the Licensor under this license.
15
-
16
- 2. Grant of Rights
17
- (A) Copyright Grant- Subject to the terms of this license, the Licensor grants you a non-transferable, non-exclusive, worldwide, royalty-free copyright license to reproduce the software for reference use.
18
-
19
- (B) Patent Grant- Subject to the terms of this license, the Licensor grants you a non-transferable, non-exclusive, worldwide, royalty-free patent license under licensed patents for reference use.
20
-
21
- 3. Limitations
22
- (A) No Trademark License- This license does not grant you any rights to use the Licensor's name, logo, or trademarks.
23
-
24
- (B) If you begin patent litigation against the Licensor over patents that you think may apply to the software (including a cross-claim or counterclaim in a lawsuit), your license to the software ends automatically.
25
-
26
- (C) The software is licensed "as-is." You bear the risk of using it. The Licensor gives no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the Licensor excludes the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
1
+ MIT License
2
+
3
+ Copyright (c) 2023 APIMatic Ltd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # apimatic-core
2
- [//]: # ([![PyPI][rubygems-version]][rubygems-apimatic-faraday-client-adapter-url])
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/apimatic_core.svg)](https://badge.fury.io/rb/apimatic_core)
3
4
  [![Tests][test-badge]][test-url]
4
5
  [![Linting][lint-badge]][lint-url]
5
- [![Maintainability][maintainability-url]][code-climate-url]
6
6
  [![Test Coverage][test-coverage-url]][code-climate-url]
7
7
  [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop)
8
8
  [![Licence][license-badge]][license-url]
@@ -86,10 +86,8 @@ gem 'apimatic_core'
86
86
  | [`XmlHelper`](lib/apimatic-core/utilities/xml_helper.rb ) | A Helper class that holds utility methods for xml serialization and deserialization. |
87
87
 
88
88
  ## Links
89
- * [apimatic_core_interfaces](link here)
89
+ * [apimatic_core_interfaces](https://rubygems.org/gems/apimatic_core_interfaces)
90
90
 
91
- [rubygems-version]: https://img.shields.io/pypi/v/apimatic-requests-client-adapter
92
- [rubygems-apimatic-faraday-client-adapter-url]: https://pypi.org/project/apimatic-requests-client-adapter/
93
91
  [test-badge]: https://github.com/apimatic/core-lib-ruby/actions/workflows/test-runner.yml/badge.svg
94
92
  [test-url]: https://github.com/apimatic/core-lib-ruby/actions/workflows/test-runner.yml
95
93
  [lint-badge]: https://github.com/apimatic/core-lib-ruby/actions/workflows/lint-runner.yml/badge.svg
@@ -97,5 +95,5 @@ gem 'apimatic_core'
97
95
  [code-climate-url]: https://codeclimate.com/github/apimatic/core-lib-ruby
98
96
  [maintainability-url]: https://api.codeclimate.com/v1/badges/85d658cd4e879f057e7d/maintainability
99
97
  [test-coverage-url]: https://api.codeclimate.com/v1/badges/85d658cd4e879f057e7d/test_coverage
100
- [license-badge]: https://img.shields.io/badge/licence-APIMATIC-blue
98
+ [license-badge]: https://img.shields.io/badge/licence-MIT-blue
101
99
  [license-url]: LICENSE
@@ -7,6 +7,9 @@ module CoreLibrary
7
7
  ApiCall.new(@global_configuration, logger: @endpoint_logger.logger)
8
8
  end
9
9
 
10
+ # Initializes a new instance of ApiCall.
11
+ # @param [GlobalConfiguration] global_configuration An instance of GlobalConfiguration.
12
+ # @param logger An optional logger to log execution of program.
10
13
  def initialize(global_configuration, logger: nil)
11
14
  @global_configuration = global_configuration
12
15
  @request_builder = RequestBuilder.new
@@ -1,6 +1,8 @@
1
1
  module CoreLibrary
2
2
  # This class is responsible for adding authentication in request header parameter.
3
3
  class HeaderAuth < Authentication
4
+ # Initializes a new instance of HeaderAuth.
5
+ # @param [Hash] auth_params Auth params for header auth.
4
6
  def initialize(auth_params)
5
7
  @auth_params = auth_params
6
8
  @error_message = nil
@@ -7,6 +7,8 @@ module CoreLibrary
7
7
  @error_messages.join(' and ')
8
8
  end
9
9
 
10
+ # Initializes a new instance of And.
11
+ # @param [String | AuthGroup] auth_group AuthGroup instance or string.
10
12
  def initialize(*auth_group)
11
13
  super auth_group
12
14
  @is_valid_group = true
@@ -4,6 +4,8 @@ module CoreLibrary
4
4
  class AuthGroup < Authentication
5
5
  attr_accessor :auth_participants, :mapped_group, :error_messages, :is_valid_group
6
6
 
7
+ # Initializes a new instance of AuthGroup.
8
+ # @param [String | AuthGroup] auth_group AuthGroup instance or string.
7
9
  def initialize(auth_group)
8
10
  @auth_participants = []
9
11
  auth_group.each do |auth_participant|
@@ -7,6 +7,8 @@ module CoreLibrary
7
7
  @error_messages.join(' or ')
8
8
  end
9
9
 
10
+ # Initializes a new instance of Or.
11
+ # @param [String | AuthGroup] auth_group AuthGroup instance or string.
10
12
  def initialize(*auth_group)
11
13
  super auth_group
12
14
  @is_valid_group = false
@@ -7,6 +7,8 @@ module CoreLibrary
7
7
  "[#{@error_message}]"
8
8
  end
9
9
 
10
+ # Initializes a new instance of Single.
11
+ # @param [String] auth_participant Auth participant name.
10
12
  def initialize(auth_participant)
11
13
  @auth_participant = auth_participant
12
14
  @mapped_auth = nil
@@ -1,6 +1,8 @@
1
1
  module CoreLibrary
2
2
  # This class is responsible for adding authentication in request query parameter.
3
3
  class QueryAuth < Authentication
4
+ # Initializes a new instance of QueryAuth.
5
+ # @param [Hash] auth_params Auth params for query auth.
4
6
  def initialize(auth_params)
5
7
  @auth_params = auth_params
6
8
  @error_message = nil
@@ -3,6 +3,8 @@ module CoreLibrary
3
3
  class GlobalConfiguration
4
4
  attr_reader :client_configuration
5
5
 
6
+ # Initializes a new instance of GlobalConfiguration.
7
+ # @param [HttpClientConfiguration] client_configuration Current HttpClientConfiguration.
6
8
  def initialize(client_configuration: HttpClientConfiguration.new)
7
9
  @client_configuration = client_configuration
8
10
  @global_errors = {}
@@ -29,11 +31,14 @@ module CoreLibrary
29
31
  end
30
32
 
31
33
  # Sets the current SDK module core library is being used for.
34
+ # @return [GlobalConfiguration] Current Instance.
32
35
  def sdk_module(sdk_module)
33
36
  @sdk_module = sdk_module
34
37
  self
35
38
  end
36
39
 
40
+ # Getter for the current SDK module the core library is being used for.
41
+ # @return [Module] Current SDK module.
37
42
  def get_sdk_module
38
43
  @sdk_module
39
44
  end
@@ -3,6 +3,19 @@ module CoreLibrary
3
3
  class HttpClientConfiguration < ClientConfiguration
4
4
  attr_reader :http_client, :http_callback
5
5
 
6
+ # Initializes a new instance of HttpClientConfiguration.
7
+ # @param connection Connection information
8
+ # @param adapter Adapter configuration
9
+ # @param [Integer] timeout Timeout value
10
+ # @param [Integer] max_retries Max retries values
11
+ # @param [Integer] retry_interval Retry interval value, in seconds
12
+ # @param [Integer] backoff_factor Backoff factor
13
+ # @param [Array] retry_statuses An integer array of http status codes
14
+ # @param [Hash] retry_methods A string array of methods
15
+ # @param [Boolean] cache Should cache be enabled
16
+ # @param [Boolean] verify Should verification be enabled.
17
+ # @param http_callback A method to be used as http callback
18
+ # @param [HttpClient] http_client An instance of HttpClient
6
19
  def initialize(
7
20
  connection: nil, adapter: :net_http_persistent, timeout: 60,
8
21
  max_retries: 0, retry_interval: 1, backoff_factor: 2,
@@ -3,6 +3,8 @@ module CoreLibrary
3
3
  class EndpointLogger
4
4
  attr_reader :logger
5
5
 
6
+ # Initializes a new instance of EndpointLogger.
7
+ # @param logger A logger with methods info, debug and error.
6
8
  def initialize(logger)
7
9
  @logger = logger
8
10
  end
@@ -1,6 +1,7 @@
1
1
  module CoreLibrary
2
2
  # This class is the builder of the http request for an API call.
3
3
  class RequestBuilder
4
+ # Creates an instance of RequestBuilder.
4
5
  def initialize
5
6
  @server = nil
6
7
  @path = nil
@@ -1,6 +1,7 @@
1
1
  module CoreLibrary
2
- # This class is for handling of the http response for an API call.
2
+ # Creates an instance of ResponseHandler.
3
3
  class ResponseHandler
4
+ # Creates an instance of ResponseHandler.
4
5
  def initialize
5
6
  @deserializer = nil
6
7
  @convertor = nil
@@ -44,13 +45,27 @@ module CoreLibrary
44
45
  self
45
46
  end
46
47
 
47
- # Sets local_errors hash key value.
48
+ # Registers an entry with error message in the local errors hash.
48
49
  # @param [String] error_code The error code to check against.
49
- # @param [String] description The reason for the exception.
50
+ # @param [String] error_message The reason for the exception.
50
51
  # @param [ApiException] exception_type The type of the exception to raise.
51
52
  # @return [ResponseHandler] An updated instance of ResponseHandler.
52
- def local_error(error_code, description, exception_type)
53
- @local_errors[error_code.to_s] = ErrorCase.new.description(description).exception_type(exception_type)
53
+ def local_error(error_code, error_message, exception_type)
54
+ @local_errors[error_code.to_s] = ErrorCase.new
55
+ .error_message(error_message)
56
+ .exception_type(exception_type)
57
+ self
58
+ end
59
+
60
+ # Registers an entry with error template in the local errors hash.
61
+ # @param [String] error_code The error code to check against.
62
+ # @param [String] error_message_template The reason template for the exception.
63
+ # @param [ApiException] exception_type The type of the exception to raise.
64
+ # @return [ResponseHandler] An updated instance of ResponseHandler.
65
+ def local_error_template(error_code, error_message_template, exception_type)
66
+ @local_errors[error_code.to_s] = ErrorCase.new
67
+ .error_message_template(error_message_template)
68
+ .exception_type(exception_type)
54
69
  self
55
70
  end
56
71
 
@@ -186,34 +201,16 @@ module CoreLibrary
186
201
  end
187
202
  # rubocop:enable Style/OptionalBooleanParameter
188
203
 
189
- # Validates the response provided and throws an error from global_errors if it fails.
190
- # @param response The received response.
191
- # @param global_errors Global errors hash.
204
+ # Validates the response provided and throws an error against the configured status code.
205
+ # @param [HttpResponse] response The received response.
206
+ # @param [Hash] global_errors Global errors hash.
207
+ # @raise [ApiException] Throws the exception when the response contains errors.
192
208
  def validate(response, global_errors)
193
- return unless response.status_code < 200 || response.status_code > 208
194
-
195
- actual_status_code = response.status_code.to_s
196
-
197
- contains_local_errors = (!@local_errors.nil? and !@local_errors[actual_status_code].nil?)
198
- if contains_local_errors
199
- error_case = @local_errors[actual_status_code]
200
- raise error_case.get_exception_type.new error_case.get_description, response
201
- end
202
-
203
- contains_local_default_error = (!@local_errors.nil? and !@local_errors['default'].nil?)
204
- if contains_local_default_error
205
- error_case = @local_errors['default']
206
- raise error_case.get_exception_type.new error_case.get_description, response
207
- end
209
+ return unless response.status_code < 200 || response.status_code > 299
208
210
 
209
- contains_global_errors = (!global_errors.nil? and !global_errors[actual_status_code].nil?)
210
- if contains_global_errors
211
- error_case = global_errors[actual_status_code]
212
- raise error_case.get_exception_type.new error_case.get_description, response
213
- end
211
+ validate_against_error_cases(response, @local_errors)
214
212
 
215
- error_case = global_errors['default']
216
- raise error_case.get_exception_type.new error_case.get_description, response unless error_case.nil?
213
+ validate_against_error_cases(response, global_errors)
217
214
  end
218
215
 
219
216
  # Applies xml deserializer to the response.
@@ -265,5 +262,31 @@ module CoreLibrary
265
262
 
266
263
  deserialized_value
267
264
  end
265
+
266
+ # Validates the response against the provided error cases hash, if matches, it raises the exception.
267
+ # @param [HttpResponse] response The received response.
268
+ # @param [Hash] error_cases The error cases hash.
269
+ # @raise [ApiException] Raises the APIException when configured error code matches.
270
+ def validate_against_error_cases(response, error_cases)
271
+ actual_status_code = response.status_code.to_s
272
+
273
+ # Handling error case when configured as explicit error code
274
+ error_case = error_cases[actual_status_code]
275
+ error_case&.raise_exception(response)
276
+
277
+ # Handling error case when configured as explicit error codes range
278
+ default_range_entry = error_cases&.filter do |error_code, _|
279
+ error_code.match?("^#{actual_status_code[0]}XX$")
280
+ end
281
+
282
+ default_range_error_case = default_range_entry&.map { |_, error_case_instance| error_case_instance }
283
+
284
+ default_range_error_case[0].raise_exception(response) unless
285
+ default_range_error_case.nil? || default_range_error_case.empty?
286
+
287
+ # Handling default error case if configured
288
+ default_error_case = error_cases['default']
289
+ default_error_case&.raise_exception(response)
290
+ end
268
291
  end
269
292
  end
@@ -1,23 +1,27 @@
1
1
  module CoreLibrary
2
2
  # This data class represents the expected errors to be handled after the API call.
3
3
  class ErrorCase
4
+ # Initializes a new instance of ErrorCase.
4
5
  def initialize
5
- @description = nil
6
+ @error_message = nil
7
+ @error_message_template = nil
6
8
  @exception_type = nil
7
9
  end
8
10
 
9
11
  # The setter for the description of the error message.
10
- # @param [String] description The description of the error message.
12
+ # @param [String] error_message The error message.
11
13
  # @return [ErrorCase] An updated instance of ErrorCase.
12
- def description(description)
13
- @description = description
14
+ def error_message(error_message)
15
+ @error_message = error_message
14
16
  self
15
17
  end
16
18
 
17
- # The getter for the description of the error message.
18
- # @return [String] The description of the error message.
19
- def get_description
20
- @description
19
+ # The setter for the description of the error message.
20
+ # @param [String] error_message_template The error message template.
21
+ # @return [ErrorCase] An updated instance of ErrorCase.
22
+ def error_message_template(error_message_template)
23
+ @error_message_template = error_message_template
24
+ self
21
25
  end
22
26
 
23
27
  # The setter for the type of the exception to be thrown.
@@ -28,10 +32,48 @@ module CoreLibrary
28
32
  self
29
33
  end
30
34
 
31
- # The getter for the type of the exception to be thrown.
32
- # @return [Object] The type of the exception to be thrown.
33
- def get_exception_type
34
- @exception_type
35
+ # Getter for the error message for the exception case. This considers both error message
36
+ # and error template message. Error message template has the higher precedence over an error message.
37
+ # @param response The received http response.
38
+ # @return [String] The resolved exception message.
39
+ def get_error_message(response)
40
+ return _get_resolved_error_message_template(response) unless @error_message_template.nil?
41
+
42
+ @error_message
43
+ end
44
+
45
+ # Raises the exception for the current error case type.
46
+ # @param response The received response.
47
+ def raise_exception(response)
48
+ raise @exception_type.new get_error_message(response), response
49
+ end
50
+
51
+ # Updates all placeholders in the given message template with provided value.
52
+ # @param response The received http response.
53
+ # @return [String] The resolved template message.
54
+ def _get_resolved_error_message_template(response)
55
+ placeholders = @error_message_template.scan(/{\$.*?\}/)
56
+
57
+ status_code_placeholder = placeholders.select { |element| element == '{$statusCode}' }.uniq
58
+ header_placeholders = placeholders.select { |element| element.start_with?('{$response.header') }.uniq
59
+ body_placeholders = placeholders.select { |element| element.start_with?('{$response.body') }.uniq
60
+
61
+ # Handling response code placeholder
62
+ error_message_template = ApiHelper.resolve_template_placeholders(status_code_placeholder,
63
+ response.status_code.to_s,
64
+ @error_message_template)
65
+
66
+ # Handling response header placeholder
67
+ error_message_template = ApiHelper.resolve_template_placeholders(header_placeholders, response.headers,
68
+ error_message_template)
69
+
70
+ # Handling response body placeholder
71
+ response_payload = ApiHelper.json_deserialize(response.raw_body, true)
72
+ error_message_template = ApiHelper.resolve_template_placeholders_using_json_pointer(body_placeholders,
73
+ response_payload,
74
+ error_message_template)
75
+
76
+ error_message_template
35
77
  end
36
78
  end
37
79
  end
@@ -1,6 +1,7 @@
1
1
  module CoreLibrary
2
2
  # This data class represents the parameter to be sent in the request.
3
3
  class Parameter
4
+ # Initializes a new instance of Parameter.
4
5
  def initialize
5
6
  @key = nil
6
7
  @value = nil
@@ -87,12 +88,16 @@ module CoreLibrary
87
88
  @default_content_type
88
89
  end
89
90
 
90
- # Template in case of oneOf or anyOf
91
+ # Template setter in case of oneOf or anyOf.
92
+ # @param [String] template Template for the parameter.
93
+ # @return [Parameter] An updated instance of Parameter.
91
94
  def template(template)
92
95
  @template = template
93
96
  self
94
97
  end
95
98
 
99
+ # Template getter in case a template is set.
100
+ # @return [Parameter] Returns template for the parameter.
96
101
  def get_template
97
102
  @template
98
103
  end
@@ -3,6 +3,9 @@ module CoreLibrary
3
3
  class FileWrapper
4
4
  attr_reader :content_type, :file
5
5
 
6
+ # Initializes a new instance of FileWrapper.
7
+ # @param [File] file File to enclose within file wrapper.
8
+ # @param [String] content_type Content type of file.
6
9
  def initialize(file, content_type: 'application/octet-stream')
7
10
  @file = file
8
11
  @content_type = content_type
@@ -1,35 +1,45 @@
1
1
  module CoreLibrary
2
2
  # The class to hold the configuration for XML parameter in request and response.
3
3
  class XmlAttributes
4
+ # Initializes a new instance of XmlAttributes.
4
5
  def initialize
5
6
  @value = nil
6
7
  @root_element_name = nil
7
8
  @array_item_name = nil
8
9
  end
9
10
 
11
+ # Value setter for XML parameter.
12
+ # @return [XmlAttributes] An updated instance of XmlAttributes.
10
13
  def value(value)
11
14
  @value = value
12
15
  self
13
16
  end
14
17
 
18
+ # Setter for root_element_name of XmlAttributes.
19
+ # @return [XmlAttributes] An updated instance of XmlAttributes.
15
20
  def root_element_name(root_element_name)
16
21
  @root_element_name = root_element_name
17
22
  self
18
23
  end
19
24
 
25
+ # Setter for array item name in XmlAttributes.
26
+ # @return [XmlAttributes] An updated instance of XmlAttributes.
20
27
  def array_item_name(array_item_name)
21
28
  @array_item_name = array_item_name
22
29
  self
23
30
  end
24
31
 
32
+ # Getter for root element of XmlAttributes.
25
33
  def get_root_element_name
26
34
  @root_element_name
27
35
  end
28
36
 
37
+ # Getter for value of XmlAttributes.
29
38
  def get_value
30
39
  @value
31
40
  end
32
41
 
42
+ # Getter for the set array item name in XmlAttributes.
33
43
  def get_array_item_name
34
44
  @array_item_name
35
45
  end
@@ -1,4 +1,6 @@
1
1
  require 'erb'
2
+ require 'json-pointer'
3
+
2
4
  module CoreLibrary
3
5
  # API utility class involved in executing an API
4
6
  class ApiHelper
@@ -230,9 +232,13 @@ module CoreLibrary
230
232
  # @param [String] json A JSON string.
231
233
  # rubocop:disable Style/OptionalBooleanParameter
232
234
  def self.json_deserialize(json, should_symbolize = false)
233
- JSON.parse(json, symbolize_names: should_symbolize)
234
- rescue StandardError
235
- raise TypeError, 'Server responded with invalid JSON.'
235
+ return if json.nil?
236
+
237
+ begin
238
+ JSON.parse(json, symbolize_names: should_symbolize)
239
+ rescue StandardError
240
+ raise TypeError, 'Server responded with invalid JSON.'
241
+ end
236
242
  end
237
243
  # rubocop:enable Style/OptionalBooleanParameter
238
244
 
@@ -547,5 +553,54 @@ module CoreLibrary
547
553
  TrueClass, FalseClass, Date,
548
554
  DateTime, Array, Hash, Object]
549
555
  end
556
+
557
+ # Updates all placeholders in the given message template with provided value.
558
+ # @param [String] placeholders The placeholders that need to be searched and replaced in the given template value.
559
+ # @param [String] value The dictionary containing the actual values to replace with.
560
+ # @param [String] template The template string containing placeholders.
561
+ # @@return [String] The resolved template value.
562
+ def self.resolve_template_placeholders_using_json_pointer(placeholders, value, template)
563
+ placeholders.each do |placeholder|
564
+ extracted_value = ''
565
+ if placeholder.include? '#'
566
+ # pick the 2nd chunk then remove the last character (i.e. `}`) of the string value
567
+ node_pointer = placeholder.split('#')[1].delete_suffix('}')
568
+ value_pointer = JsonPointer.new(value, node_pointer, symbolize_keys: true)
569
+ extracted_value = json_serialize(value_pointer.value) if value_pointer.exists?
570
+ elsif !value.nil?
571
+ extracted_value = json_serialize(value)
572
+ end
573
+ template.gsub!(placeholder, extracted_value)
574
+ end
575
+
576
+ template
577
+ end
578
+
579
+ # Updates all placeholders in the given message template with provided value.
580
+ # @param [List] placeholders The placeholders that need to be searched and replaced in the given template value.
581
+ # @param [Hash|String] values The value which refers to the actual values to replace with.
582
+ # @param [String] template The template string containing placeholders.
583
+ # @@return [String] The resolved template value.
584
+ def self.resolve_template_placeholders(placeholders, values, template)
585
+ values = values.map { |key, value| [key.to_s, value.to_s] }.to_h if values.is_a? Hash
586
+
587
+ placeholders.each do |placeholder|
588
+ extracted_value = ''
589
+ if values.is_a? Hash
590
+ # pick the last chunk then strip the last character (i.e. `}`) of the string value
591
+ key = if placeholder.include? '.'
592
+ placeholder.split('.')[-1].delete_suffix('}')
593
+ else
594
+ placeholder.delete_prefix('{').delete_suffix('}')
595
+ end
596
+ extracted_value = values[key] unless values[key].nil?
597
+ else
598
+ extracted_value = values unless values.nil?
599
+ end
600
+ template.gsub!(placeholder, extracted_value.to_s)
601
+ end
602
+
603
+ template
604
+ end
550
605
  end
551
606
  end
@@ -2,7 +2,7 @@ module CoreLibrary
2
2
  # A utility that perform the comparison of the Response body and headers.
3
3
  class ComparisonHelper
4
4
 
5
- # Class method to compare the received headers with the expected headers.
5
+ # Compares the received headers with the expected headers.
6
6
  # @param [Hash] expected_headers A hash of expected headers (keys in lower case).
7
7
  # @param [Hash] actual_headers A hash of actual headers.
8
8
  # @param [Boolean, optional] allow_extra A flag which determines if we allow extra headers.
@@ -24,7 +24,7 @@ module CoreLibrary
24
24
  return true
25
25
  end
26
26
 
27
- # Class method to compare the received body with the expected body.
27
+ # Compares the received body with the expected body.
28
28
  # @param [Dynamic] expected_body The expected body.
29
29
  # @param [Dynamic] actual_body The actual body.
30
30
  # @param [Boolean, optional] check_values A flag which determines if we check values in dictionaries.
@@ -4,9 +4,10 @@ module CoreLibrary
4
4
  # A utility class for handling xml parsing.
5
5
  class XmlHelper
6
6
  class << self
7
- # Serialized the provided value to XML.
8
- # @param root_element_name Root element for the XML provided.
9
- # @param value Value to convert to XML.
7
+ # Serializes the provided value to XML.
8
+ # @param [String] root_element_name Root element for the XML provided.
9
+ # @param [String] value Value to convert to XML.
10
+ # @param [CoreLibrary::DateTimeFormat] datetime_format The format to convert the date time into.
10
11
  def serialize_to_xml(root_element_name, value, datetime_format: nil)
11
12
  doc = Nokogiri::XML::Document.new
12
13
  add_as_subelement(doc, doc, root_element_name, value,
@@ -14,10 +15,11 @@ module CoreLibrary
14
15
  doc.to_xml
15
16
  end
16
17
 
17
- # Serialized the provided array value to XML.
18
- # @param root_element_name Root element for the xml provided.
19
- # @param item_name Item name for XML.
18
+ # Serializes the provided array value to XML.
19
+ # @param [String] root_element_name Root element for the xml provided.
20
+ # @param [String] item_name Item name for XML.
20
21
  # @param value Value to convert to XML.
22
+ # @param [CoreLibrary::DateTimeFormat] datetime_format The format to convert the date time into.
21
23
  def serialize_array_to_xml(root_element_name, item_name, value,
22
24
  datetime_format: nil)
23
25
  doc = Nokogiri::XML::Document.new
@@ -27,8 +29,10 @@ module CoreLibrary
27
29
  doc.to_xml
28
30
  end
29
31
 
30
- # Serialized the provided hash to XML.
31
- # @param root_element_name Root element for the XML provided.
32
+ # Serializes the provided hash to XML.
33
+ # @param [string] root_element_name Root element for the XML provided.
34
+ # @param [Hash] entries Entries to convert to XML.
35
+ # @param [CoreLibrary::DateTimeFormat] datetime_format The format to convert the date time into.
32
36
  def serialize_hash_to_xml(root_element_name, entries,
33
37
  datetime_format: nil)
34
38
  doc = Nokogiri::XML::Document.new
@@ -38,6 +42,10 @@ module CoreLibrary
38
42
  end
39
43
 
40
44
  # Adds the value as an attribute.
45
+ # @param [REXML::Element] root Root element of the XML to add the attribute to.
46
+ # @param [string] name Attribute name.
47
+ # @param [string] value Attribute value.
48
+ # @param [CoreLibrary::DateTimeFormat] datetime_format The format to convert the date time into.
41
49
  def add_as_attribute(root, name, value, datetime_format: nil)
42
50
  return if value.nil?
43
51
 
@@ -47,6 +55,11 @@ module CoreLibrary
47
55
  end
48
56
 
49
57
  # Adds hash as a sub-element.
58
+ # @param [Nokogiri::XML::Document] doc Document to add the hash to.
59
+ # @param [REXML::Element] root Root element of the XML to add the attribute to.
60
+ # @param [string] name Attribute name.
61
+ # @param [Hash] entries Hash to add as value.
62
+ # @param [CoreLibrary::DateTimeFormat] datetime_format The format to convert the date time into.
50
63
  def add_hash_as_subelement(doc, root, name, entries,
51
64
  datetime_format: nil)
52
65
  return if entries.nil?
@@ -61,6 +74,12 @@ module CoreLibrary
61
74
  end
62
75
 
63
76
  # Adds array as a sub-element.
77
+ # @param [Nokogiri::XML::Document] doc Document to add the hash to.
78
+ # @param [REXML::Element] root Root element of the XML to add the attribute to.
79
+ # @param [String] item_name Individual array item names.
80
+ # @param [Array] items Array of items.
81
+ # @param [String] wrapping_element_name Main array item name.
82
+ # @param [CoreLibrary::DateTimeFormat] datetime_format The format to convert the date time into.
64
83
  def add_array_as_subelement(doc, root, item_name, items,
65
84
  wrapping_element_name: nil,
66
85
  datetime_format: nil)
@@ -80,6 +99,11 @@ module CoreLibrary
80
99
  end
81
100
 
82
101
  # Adds as a sub-element.
102
+ # @param [Nokogiri::XML::Document] doc Document to add the hash to.
103
+ # @param [REXML::Element] root Root element of the XML to add the attribute to.
104
+ # @param [String] name Item name.
105
+ # @param [Array] value Item value.
106
+ # @param [CoreLibrary::DateTimeFormat] datetime_format The format to convert the date time into.
83
107
  def add_as_subelement(doc, root, name, value, datetime_format: nil)
84
108
  return if value.nil?
85
109
 
@@ -95,8 +119,8 @@ module CoreLibrary
95
119
  end
96
120
 
97
121
  # Converts datetime to string of a specific format.
98
- # @param value Value to convert to string.
99
- # @param datetime_format Datetime format for the converted string.
122
+ # @param [DateTime] value Value to convert to string.
123
+ # @param [CoreLibrary::DateTimeFormat] datetime_format Datetime format to use for conversion..
100
124
  def datetime_to_s(value, datetime_format)
101
125
  case datetime_format
102
126
  when 'UnixDateTime'
@@ -109,9 +133,10 @@ module CoreLibrary
109
133
  end
110
134
 
111
135
  # Deserializes XML to a specific class.
112
- # @param xml The XML value to deserialize.
113
- # @param root_element_name Root element name for the XML provided.
114
- # @param clazz The class to convert the XML into.
136
+ # @param [String] xml The XML value to deserialize.
137
+ # @param [String] root_element_name Root element name for the XML provided.
138
+ # @param [Class] clazz The class to convert the XML into.
139
+ # @param [CoreLibrary::DateTimeFormat] datetime_format Datetime format to use for conversion..
115
140
  def deserialize_xml(xml, root_element_name, clazz, datetime_format = nil)
116
141
  doc = Nokogiri::XML::Document.parse xml
117
142
  from_element(doc, root_element_name, clazz,
@@ -119,10 +144,10 @@ module CoreLibrary
119
144
  end
120
145
 
121
146
  # Deserializes XML to an array of a specific class.
122
- # @param xml The XML value to deserialize.
123
- # @param root_element_name Root element name for the XML provided.
124
- # @param item_name Item name for XML.
125
- # @param clazz The class to convert the XML into.
147
+ # @param [String] xml The XML value to deserialize.
148
+ # @param [String] root_element_name Root element name for the XML provided.
149
+ # @param [String] item_name Item name for XML.
150
+ # @param [Class] clazz The class to convert the XML into.
126
151
  def deserialize_xml_to_array(xml, root_element_name, item_name, clazz,
127
152
  datetime_format = nil)
128
153
  doc = Nokogiri::XML::Document.parse xml
@@ -132,8 +157,10 @@ module CoreLibrary
132
157
  end
133
158
 
134
159
  # Deserializes XML to an array of a specific class.
135
- # @param xml The XML value to deserialize.
136
- # @param root_element_name Root element name for the XML provided.
160
+ # @param [String] xml The XML value to deserialize.
161
+ # @param [String] root_element_name Root element name for the XML provided.
162
+ # @param [Class] clazz The class to convert the XML into.
163
+ # @param [CoreLibrary::DateTimeFormat] datetime_format Datetime format to use for conversion..
137
164
  def deserialize_xml_to_hash(xml, root_element_name, clazz,
138
165
  datetime_format = nil)
139
166
  doc = Nokogiri::XML::Document.parse xml
@@ -142,6 +169,10 @@ module CoreLibrary
142
169
  end
143
170
 
144
171
  # Converts attribute to a specific class.
172
+ # @param [String] parent Parent XML.
173
+ # @param [String] name Attribute name.
174
+ # @param [Class] clazz Attribute class.
175
+ # @param [CoreLibrary::DateTimeFormat] datetime_format Datetime format to use for conversion..
145
176
  def from_attribute(parent, name, clazz, datetime_format: nil)
146
177
  attribute = parent[name]
147
178
  return nil if attribute.nil?
@@ -150,6 +181,10 @@ module CoreLibrary
150
181
  end
151
182
 
152
183
  # Converts element to a specific class.
184
+ # @param [String] parent Parent XML.
185
+ # @param [String] name Attribute name.
186
+ # @param [Class] clazz Attribute class.
187
+ # @param [CoreLibrary::DateTimeFormat] datetime_format Datetime format to use for conversion..
153
188
  def from_element(parent, name, clazz, datetime_format: nil)
154
189
  element = parent.at_xpath(name)
155
190
  return nil if element.nil?
@@ -159,6 +194,11 @@ module CoreLibrary
159
194
  end
160
195
 
161
196
  # Converts element to an array.
197
+ # @param [String] parent Parent XML.
198
+ # @param [String] item_name Element name.
199
+ # @param [Class] clazz Element class.
200
+ # @param [String] wrapping_element_name Main element name.
201
+ # @param [CoreLibrary::DateTimeFormat] datetime_format Datetime format to use for conversion..
162
202
  def from_element_to_array(parent, item_name, clazz,
163
203
  wrapping_element_name: nil,
164
204
  datetime_format: nil)
@@ -182,6 +222,11 @@ module CoreLibrary
182
222
  end
183
223
 
184
224
  # Converts element to hash.
225
+ # @param [Nokogiri::XML::Document] parent Parent document.
226
+ # @param [String] name Element name.
227
+ # @param [Class] clazz Hash class to convert to.
228
+ # @param [CoreLibrary::DateTimeFormat] datetime_format Datetime format to use for conversion.
229
+ # @return [Object] Converted value.
185
230
  def from_element_to_hash(parent, name, clazz,
186
231
  datetime_format: nil)
187
232
  entries = parent.at_xpath(name)
@@ -197,6 +242,10 @@ module CoreLibrary
197
242
  end
198
243
 
199
244
  # Basic convert method.
245
+ # @param [String] value Value to convert type of.
246
+ # @param [Class] clazz Type to convert into.
247
+ # @param [CoreLibrary::DateTimeFormat] datetime_format Datetime format to use for conversion..
248
+ # @return [Object] Converted value.
200
249
  def convert(value, clazz, datetime_format)
201
250
  if clazz == DateTime
202
251
  return DateTime.rfc3339(value) if datetime_format == 'RFC3339DateTime'
data/lib/apimatic_core.rb CHANGED
@@ -3,6 +3,7 @@ require 'json'
3
3
  require 'certifi'
4
4
  require 'apimatic_core_interfaces'
5
5
  require 'cgi'
6
+ require 'json-pointer'
6
7
 
7
8
  require_relative 'apimatic-core/request_builder'
8
9
  require_relative 'apimatic-core/response_handler'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apimatic_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - APIMatic Ltd.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-28 00:00:00.000000000 Z
11
+ date: 2023-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: apimatic_core_interfaces
@@ -78,6 +78,20 @@ dependencies:
78
78
  - - "~>"
79
79
  - !ruby/object:Gem::Version
80
80
  version: '1.0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: json-pointer
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
81
95
  - !ruby/object:Gem::Dependency
82
96
  name: faraday
83
97
  requirement: !ruby/object:Gem::Requirement
@@ -190,7 +204,7 @@ files:
190
204
  - lib/apimatic_core.rb
191
205
  homepage: https://apimatic.io
192
206
  licenses:
193
- - APIMATIC REFERENCE SOURCE LICENSE
207
+ - MIT
194
208
  metadata: {}
195
209
  post_install_message:
196
210
  rdoc_options: []
@@ -207,7 +221,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
207
221
  - !ruby/object:Gem::Version
208
222
  version: '0'
209
223
  requirements: []
210
- rubygems_version: 3.4.1
224
+ rubygems_version: 3.4.6
211
225
  signing_key:
212
226
  specification_version: 4
213
227
  summary: A library that contains apimatic-apimatic-core logic and utilities for consuming