apimatic_calculator_test_ruby 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +28 -0
- data/README.md +110 -0
- data/lib/apimatic_calculator/api_helper.rb +449 -0
- data/lib/apimatic_calculator/client.rb +35 -0
- data/lib/apimatic_calculator/configuration.rb +115 -0
- data/lib/apimatic_calculator/controllers/base_controller.rb +59 -0
- data/lib/apimatic_calculator/controllers/simple_calculator_controller.rb +45 -0
- data/lib/apimatic_calculator/exceptions/api_exception.rb +20 -0
- data/lib/apimatic_calculator/exceptions/validation_exception.rb +18 -0
- data/lib/apimatic_calculator/http/faraday_client.rb +98 -0
- data/lib/apimatic_calculator/http/http_call_back.rb +24 -0
- data/lib/apimatic_calculator/http/http_client.rb +123 -0
- data/lib/apimatic_calculator/http/http_method_enum.rb +13 -0
- data/lib/apimatic_calculator/http/http_request.rb +54 -0
- data/lib/apimatic_calculator/http/http_response.rb +29 -0
- data/lib/apimatic_calculator/models/base_model.rb +58 -0
- data/lib/apimatic_calculator/models/operation_type_enum.rb +23 -0
- data/lib/apimatic_calculator/utilities/date_time_helper.rb +156 -0
- data/lib/apimatic_calculator/utilities/file_wrapper.rb +16 -0
- data/lib/apimatic_calculator.rb +39 -0
- data/test/controllers/controller_test_base.rb +21 -0
- data/test/controllers/test_simple_calculator_controller.rb +34 -0
- data/test/http_response_catcher.rb +19 -0
- data/test/test_helper.rb +94 -0
- metadata +239 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 79053e5094308dacf4e12f3252302a012e284397825137d5a235c32c59cb6a51
|
4
|
+
data.tar.gz: f24eeae0f2b81f5b1ec01af5f6ba1991ae862ac476ba7dc65aabf816deb50305
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fc831f7bbfb988b4d93db9ebe9df617d870cae363fcda1a8453d03637d98994085e9d4cc6f1b432b44888b8f0c92bdfa8b662644ef2b39b78d48fb8b3b6b3d7f
|
7
|
+
data.tar.gz: 1da084ef7cc1126937fd05bcdf6d9fc07e3b0c1d6d66bc915b531b1955a94317958fdfb3291bf71d18d9d50f15c24b637a2e5308045e480496930a17f27c144e
|
data/LICENSE
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
License:
|
2
|
+
========
|
3
|
+
The MIT License (MIT)
|
4
|
+
http://opensource.org/licenses/MIT
|
5
|
+
|
6
|
+
Copyright (c) 2014 - 2023 APIMATIC Limited
|
7
|
+
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
10
|
+
in the Software without restriction, including without limitation the rights
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
13
|
+
furnished to do so, subject to the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be included in
|
16
|
+
all copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
24
|
+
THE SOFTWARE.
|
25
|
+
|
26
|
+
Trade Mark:
|
27
|
+
==========
|
28
|
+
APIMATIC is a trade mark for APIMATIC Limited
|
data/README.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
|
2
|
+
# Getting Started with APIMATIC Calculator
|
3
|
+
|
4
|
+
## Introduction
|
5
|
+
|
6
|
+
Simple calculator API hosted on APIMATIC
|
7
|
+
|
8
|
+
## Building
|
9
|
+
|
10
|
+
The generated code depends on a few Ruby gems. The references to these gems are added in the gemspec file. The easiest way to resolve the dependencies, build the gem and install it is through Rake:
|
11
|
+
|
12
|
+
1. Install Rake if not already installed: `gem install rake`
|
13
|
+
2. Install Bundler if not already installed: `gem install bundler`
|
14
|
+
3. From terminal/cmd navigate to the root directory of the SDK.
|
15
|
+
4. Invoke: `rake install`
|
16
|
+
|
17
|
+
Alternatively, you can build and install the gem manually:
|
18
|
+
|
19
|
+
1. From terminal/cmd navigate to the root directory of the SDK.
|
20
|
+
2. Run the build command: `gem build apimatic_calculator.gemspec`
|
21
|
+
3. Run the install command: `gem install apimatic_calculator-1.0.0.gem`
|
22
|
+
|
23
|
+

|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
The following section explains how to use the apimatic_calculator ruby gem in a new Rails project using RubyMine™. The basic workflow presented here is also applicable if you prefer using a different editor or IDE.
|
28
|
+
|
29
|
+
### 1. Starting a new project
|
30
|
+
|
31
|
+
Close any existing projects in RubyMine™ by selecting `File -> Close Project`. Next, click on `Create New Project` to create a new project from scratch.
|
32
|
+
|
33
|
+

|
34
|
+
|
35
|
+
Next, provide `TestApp` as the project name, choose `Rails Application` as the project type, and click `OK`.
|
36
|
+
|
37
|
+

|
38
|
+
|
39
|
+
In the next dialog make sure that the correct Ruby SDK is being used (>= 2.6 and < 3.1) and click `OK`.
|
40
|
+
|
41
|
+

|
42
|
+
|
43
|
+
### 2. Add reference of the gem
|
44
|
+
|
45
|
+
In order to use the Tester gem in the new project we must add a gem reference. Locate the `Gemfile` in the Project Explorer window under the `TestApp` project node. The file contains references to all gems being used in the project. Here, add the reference to the library gem by adding the following line: `gem 'apimatic_calculator', '1.0.0'`
|
46
|
+
|
47
|
+

|
48
|
+
|
49
|
+
### 3. Adding a new Rails Controller
|
50
|
+
|
51
|
+
Once the `TestApp` project is created, a folder named `controllers` will be visible in the *Project Explorer* under the following path: `TestApp > app > controllers`. Right click on this folder and select `New -> Run Rails Generator...`.
|
52
|
+
|
53
|
+

|
54
|
+
|
55
|
+
Selecting the said option will popup a small window where the generator names are displayed. Here, select the `controller` template.
|
56
|
+
|
57
|
+

|
58
|
+
|
59
|
+
Next, a popup window will ask you for a Controller name and included Actions. For controller name provide `Hello` and include an action named `Index` and click `OK`.
|
60
|
+
|
61
|
+

|
62
|
+
|
63
|
+
A new controller class named `HelloController` will be created in a file named `hello_controller.rb` containing a method named `Index`. In this method, add code for initialization and a sample for its usage.
|
64
|
+
|
65
|
+

|
66
|
+
|
67
|
+
## Test the SDK
|
68
|
+
|
69
|
+
To run the tests, navigate to the root directory of the SDK in your terminal and execute the following command:
|
70
|
+
|
71
|
+
```
|
72
|
+
rake
|
73
|
+
```
|
74
|
+
|
75
|
+
## Initialize the API Client
|
76
|
+
|
77
|
+
**_Note:_** Documentation for the client can be found [here.](doc/client.md)
|
78
|
+
|
79
|
+
The following parameters are configurable for the API Client:
|
80
|
+
|
81
|
+
| Parameter | Type | Description |
|
82
|
+
| --- | --- | --- |
|
83
|
+
| `environment` | Environment | The API environment. <br> **Default: `Environment.PRODUCTION`** |
|
84
|
+
| `connection` | `Faraday::Connection` | The Faraday connection object passed by the SDK user for making requests |
|
85
|
+
| `adapter` | `Faraday::Adapter` | The Faraday adapter object passed by the SDK user for performing http requests |
|
86
|
+
| `timeout` | `Float` | The value to use for connection timeout. <br> **Default: 60** |
|
87
|
+
| `max_retries` | `Integer` | The number of times to retry an endpoint call if it fails. <br> **Default: 0** |
|
88
|
+
| `retry_interval` | `Float` | Pause in seconds between retries. <br> **Default: 1** |
|
89
|
+
| `backoff_factor` | `Float` | The amount to multiply each successive retry's interval amount by in order to provide backoff. <br> **Default: 2** |
|
90
|
+
| `retry_statuses` | `Array` | A list of HTTP statuses to retry. <br> **Default: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524]** |
|
91
|
+
| `retry_methods` | `Array` | A list of HTTP methods to retry. <br> **Default: %i[get put]** |
|
92
|
+
|
93
|
+
The API client can be initialized as follows:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
client = ApimaticCalculator::Client.new(
|
97
|
+
environment: Environment::PRODUCTION,
|
98
|
+
)
|
99
|
+
```
|
100
|
+
|
101
|
+
## List of APIs
|
102
|
+
|
103
|
+
* [Simple Calculator](doc/controllers/simple-calculator.md)
|
104
|
+
|
105
|
+
## Classes Documentation
|
106
|
+
|
107
|
+
* [Utility Classes](doc/utility-classes.md)
|
108
|
+
* [HttpResponse](doc/http-response.md)
|
109
|
+
* [HttpRequest](doc/http-request.md)
|
110
|
+
|
@@ -0,0 +1,449 @@
|
|
1
|
+
# apimatic_calculator
|
2
|
+
#
|
3
|
+
# This file was automatically generated by APIMATIC v2.0
|
4
|
+
# ( https://apimatic.io ).
|
5
|
+
|
6
|
+
module ApimaticCalculator
|
7
|
+
# API utility class
|
8
|
+
class APIHelper
|
9
|
+
# Serializes an array parameter (creates key value pairs).
|
10
|
+
# @param [String] The name of the parameter.
|
11
|
+
# @param [Array] The value of the parameter.
|
12
|
+
# @param [String] The format of the serialization.
|
13
|
+
def self.serialize_array(key, array, formatting: 'indexed')
|
14
|
+
tuples = []
|
15
|
+
|
16
|
+
tuples += case formatting
|
17
|
+
when 'csv'
|
18
|
+
[[key, array.map { |element| CGI.escape(element.to_s) }.join(',')]]
|
19
|
+
when 'psv'
|
20
|
+
[[key, array.map { |element| CGI.escape(element.to_s) }.join('|')]]
|
21
|
+
when 'tsv'
|
22
|
+
[[key, array.map { |element| CGI.escape(element.to_s) }.join("\t")]]
|
23
|
+
else
|
24
|
+
array.map { |element| [key, element] }
|
25
|
+
end
|
26
|
+
tuples
|
27
|
+
end
|
28
|
+
|
29
|
+
# Replaces template parameters in the given url.
|
30
|
+
# @param [String] The query string builder to replace the template
|
31
|
+
# parameters.
|
32
|
+
# @param [Hash] The parameters to replace in the url.
|
33
|
+
def self.append_url_with_template_parameters(query_builder, parameters)
|
34
|
+
# perform parameter validation
|
35
|
+
unless query_builder.instance_of? String
|
36
|
+
raise ArgumentError, 'Given value for parameter \"query_builder\" is
|
37
|
+
invalid.'
|
38
|
+
end
|
39
|
+
|
40
|
+
# Return if there are no parameters to replace.
|
41
|
+
return query_builder if parameters.nil?
|
42
|
+
|
43
|
+
parameters.each do |key, val|
|
44
|
+
if val.nil?
|
45
|
+
replace_value = ''
|
46
|
+
elsif val['value'].instance_of? Array
|
47
|
+
if val['encode'] == true
|
48
|
+
val['value'].map! { |element| CGI.escape(element.to_s) }
|
49
|
+
else
|
50
|
+
val['value'].map!(&:to_s)
|
51
|
+
end
|
52
|
+
replace_value = val['value'].join('/')
|
53
|
+
else
|
54
|
+
replace_value = if val['encode'] == true
|
55
|
+
CGI.escape(val['value'].to_s)
|
56
|
+
else
|
57
|
+
val['value'].to_s
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Find the template parameter and replace it with its value.
|
62
|
+
query_builder = query_builder.gsub("{#{key}}", replace_value)
|
63
|
+
end
|
64
|
+
query_builder
|
65
|
+
end
|
66
|
+
|
67
|
+
# Appends the given set of parameters to the given query string.
|
68
|
+
# @param [String] The query string builder to add the query parameters to.
|
69
|
+
# @param [Hash] The parameters to append.
|
70
|
+
def self.append_url_with_query_parameters(query_builder, parameters)
|
71
|
+
# Perform parameter validation.
|
72
|
+
unless query_builder.instance_of? String
|
73
|
+
raise ArgumentError, 'Given value for parameter \"query_builder\"
|
74
|
+
is invalid.'
|
75
|
+
end
|
76
|
+
|
77
|
+
# Return if there are no parameters to replace.
|
78
|
+
return query_builder if parameters.nil?
|
79
|
+
|
80
|
+
array_serialization = 'indexed'
|
81
|
+
parameters = process_complex_types_parameters(parameters, array_serialization)
|
82
|
+
|
83
|
+
parameters.each do |key, value|
|
84
|
+
seperator = query_builder.include?('?') ? '&' : '?'
|
85
|
+
unless value.nil?
|
86
|
+
if value.instance_of? Array
|
87
|
+
value.compact!
|
88
|
+
APIHelper.serialize_array(
|
89
|
+
key, value, formatting: array_serialization
|
90
|
+
).each do |element|
|
91
|
+
seperator = query_builder.include?('?') ? '&' : '?'
|
92
|
+
query_builder += "#{seperator}#{element[0]}=#{element[1]}"
|
93
|
+
end
|
94
|
+
else
|
95
|
+
query_builder += "#{seperator}#{key}=#{CGI.escape(value.to_s)}"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
query_builder
|
100
|
+
end
|
101
|
+
|
102
|
+
# Validates and processes the given Url.
|
103
|
+
# @param [String] The given Url to process.
|
104
|
+
# @return [String] Pre-processed Url as string.
|
105
|
+
def self.clean_url(url)
|
106
|
+
# Perform parameter validation.
|
107
|
+
raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
|
108
|
+
|
109
|
+
# Ensure that the urls are absolute.
|
110
|
+
matches = url.match(%r{^(https?://[^/]+)})
|
111
|
+
raise ArgumentError, 'Invalid Url format.' if matches.nil?
|
112
|
+
|
113
|
+
# Get the http protocol match.
|
114
|
+
protocol = matches[1]
|
115
|
+
|
116
|
+
# Check if parameters exist.
|
117
|
+
index = url.index('?')
|
118
|
+
|
119
|
+
# Remove redundant forward slashes.
|
120
|
+
query = url[protocol.length...(!index.nil? ? index : url.length)]
|
121
|
+
query.gsub!(%r{//+}, '/')
|
122
|
+
|
123
|
+
# Get the parameters.
|
124
|
+
parameters = !index.nil? ? url[url.index('?')...url.length] : ''
|
125
|
+
|
126
|
+
# Return processed url.
|
127
|
+
protocol + query + parameters
|
128
|
+
end
|
129
|
+
|
130
|
+
# Parses JSON string.
|
131
|
+
# @param [String] A JSON string.
|
132
|
+
def self.json_deserialize(json)
|
133
|
+
JSON.parse(json)
|
134
|
+
rescue StandardError
|
135
|
+
raise TypeError, 'Server responded with invalid JSON.'
|
136
|
+
end
|
137
|
+
|
138
|
+
# Parses JSON string.
|
139
|
+
# @param [object] The object to serialize.
|
140
|
+
def self.json_serialize(obj)
|
141
|
+
serializable_types.map { |x| obj.is_a? x }.any? ? obj.to_s : obj.to_json
|
142
|
+
end
|
143
|
+
|
144
|
+
# Removes elements with empty values from a hash.
|
145
|
+
# @param [Hash] The hash to clean.
|
146
|
+
def self.clean_hash(hash)
|
147
|
+
hash.delete_if { |_key, value| value.to_s.strip.empty? }
|
148
|
+
end
|
149
|
+
|
150
|
+
# Form encodes a hash of parameters.
|
151
|
+
# @param [Hash] The hash of parameters to encode.
|
152
|
+
# @return [Hash] A hash with the same parameters form encoded.
|
153
|
+
def self.form_encode_parameters(form_parameters)
|
154
|
+
array_serialization = 'indexed'
|
155
|
+
encoded = {}
|
156
|
+
form_parameters.each do |key, value|
|
157
|
+
encoded.merge!(APIHelper.form_encode(value, key, formatting:
|
158
|
+
array_serialization))
|
159
|
+
end
|
160
|
+
encoded
|
161
|
+
end
|
162
|
+
|
163
|
+
# Process complex types in query_params.
|
164
|
+
# @param [Hash] The hash of query parameters.
|
165
|
+
# @return [Hash] A hash with the processed query parameters.
|
166
|
+
def self.process_complex_types_parameters(query_parameters, array_serialization)
|
167
|
+
processed_params = {}
|
168
|
+
query_parameters.each do |key, value|
|
169
|
+
processed_params.merge!(APIHelper.form_encode(value, key, formatting:
|
170
|
+
array_serialization))
|
171
|
+
end
|
172
|
+
processed_params
|
173
|
+
end
|
174
|
+
|
175
|
+
def self.custom_merge(a, b)
|
176
|
+
x = {}
|
177
|
+
a.each do |key, value_a|
|
178
|
+
b.each do |k, value_b|
|
179
|
+
next unless key == k
|
180
|
+
|
181
|
+
x[k] = []
|
182
|
+
if value_a.instance_of? Array
|
183
|
+
value_a.each do |v|
|
184
|
+
x[k].push(v)
|
185
|
+
end
|
186
|
+
else
|
187
|
+
x[k].push(value_a)
|
188
|
+
end
|
189
|
+
if value_b.instance_of? Array
|
190
|
+
value_b.each do |v|
|
191
|
+
x[k].push(v)
|
192
|
+
end
|
193
|
+
else
|
194
|
+
x[k].push(value_b)
|
195
|
+
end
|
196
|
+
a.delete(k)
|
197
|
+
b.delete(k)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
x.merge!(a)
|
201
|
+
x.merge!(b)
|
202
|
+
x
|
203
|
+
end
|
204
|
+
|
205
|
+
# Form encodes an object.
|
206
|
+
# @param [Dynamic] An object to form encode.
|
207
|
+
# @param [String] The name of the object.
|
208
|
+
# @return [Hash] A form encoded representation of the object in the form
|
209
|
+
# of a hash.
|
210
|
+
def self.form_encode(obj, instance_name, formatting: 'indexed')
|
211
|
+
retval = {}
|
212
|
+
|
213
|
+
# If this is a structure, resolve it's field names.
|
214
|
+
obj = obj.to_hash if obj.is_a? BaseModel
|
215
|
+
|
216
|
+
# Create a form encoded hash for this object.
|
217
|
+
if obj.nil?
|
218
|
+
nil
|
219
|
+
elsif obj.instance_of? Array
|
220
|
+
if formatting == 'indexed'
|
221
|
+
obj.each_with_index do |value, index|
|
222
|
+
retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{index}]"))
|
223
|
+
end
|
224
|
+
elsif APIHelper.serializable_types.map { |x| obj[0].is_a? x }.any?
|
225
|
+
obj.each do |value|
|
226
|
+
abc = if formatting == 'unindexed'
|
227
|
+
APIHelper.form_encode(value, "#{instance_name}[]",
|
228
|
+
formatting: formatting)
|
229
|
+
else
|
230
|
+
APIHelper.form_encode(value, instance_name,
|
231
|
+
formatting: formatting)
|
232
|
+
end
|
233
|
+
retval = APIHelper.custom_merge(retval, abc)
|
234
|
+
end
|
235
|
+
else
|
236
|
+
obj.each_with_index do |value, index|
|
237
|
+
retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{index}]",
|
238
|
+
formatting: formatting))
|
239
|
+
end
|
240
|
+
end
|
241
|
+
elsif obj.instance_of? Hash
|
242
|
+
obj.each do |key, value|
|
243
|
+
retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{key}]",
|
244
|
+
formatting: formatting))
|
245
|
+
end
|
246
|
+
elsif obj.instance_of? File
|
247
|
+
retval[instance_name] = UploadIO.new(
|
248
|
+
obj, 'application/octet-stream', File.basename(obj.path)
|
249
|
+
)
|
250
|
+
else
|
251
|
+
retval[instance_name] = obj
|
252
|
+
end
|
253
|
+
retval
|
254
|
+
end
|
255
|
+
|
256
|
+
# Retrieves a field from a Hash/Array based on an Array of keys/indexes
|
257
|
+
# @param [Hash, Array] The hash to extract data from
|
258
|
+
# @param [Array<String, Integer>] The keys/indexes to use
|
259
|
+
# @return [Object] The extracted value
|
260
|
+
def self.map_response(obj, keys)
|
261
|
+
val = obj
|
262
|
+
begin
|
263
|
+
keys.each do |key|
|
264
|
+
val = if val.is_a? Array
|
265
|
+
if key.to_i.to_s == key
|
266
|
+
val[key.to_i]
|
267
|
+
else
|
268
|
+
val = nil
|
269
|
+
end
|
270
|
+
else
|
271
|
+
val.fetch(key.to_sym)
|
272
|
+
end
|
273
|
+
end
|
274
|
+
rescue NoMethodError, TypeError, IndexError
|
275
|
+
val = nil
|
276
|
+
end
|
277
|
+
val
|
278
|
+
end
|
279
|
+
|
280
|
+
# Deserialize the value against the template (group of types).
|
281
|
+
# @param [String] The value to be deserialized.
|
282
|
+
# @param [String] The parameter indicates the type-combination
|
283
|
+
# against which the value will be mapped (oneOf(Integer, String)).
|
284
|
+
def self.deserialize(template, value)
|
285
|
+
decoded = APIHelper.json_deserialize(value)
|
286
|
+
map_types(decoded, template)
|
287
|
+
end
|
288
|
+
|
289
|
+
# Validates and processes the value against the template(group of types).
|
290
|
+
# @param [String] The value to be mapped against the template.
|
291
|
+
# @param [String] The parameter indicates the group of types (oneOf(Integer, String)).
|
292
|
+
# @param [String] The parameter indicates the group (oneOf|anyOf).
|
293
|
+
def self.map_types(value, template, group_name: nil)
|
294
|
+
result_value = nil
|
295
|
+
matches = 0
|
296
|
+
types = []
|
297
|
+
group_name = template.partition('(').first if group_name.nil? && template.match?(/anyOf|oneOf/)
|
298
|
+
|
299
|
+
return if value.nil?
|
300
|
+
|
301
|
+
if template.end_with?('{}') || template.end_with?('[]')
|
302
|
+
types = template.split(group_name, 2).last.gsub(/\s+/, '').split
|
303
|
+
else
|
304
|
+
template = template.split(group_name, 2).last.delete_prefix('(').delete_suffix(')')
|
305
|
+
types = template.scan(/(anyOf|oneOf)[(]([^[)]]*)[)]/).flatten.combination(2).map { |a, b| "#{a}(#{b})" }
|
306
|
+
types.each { |t| template = template.gsub(", #{t}", '') }
|
307
|
+
types = template.gsub(/\s+/, '').split(',').push(*types)
|
308
|
+
end
|
309
|
+
types.each do |element|
|
310
|
+
if element.match?(/^(oneOf|anyOf)[(].*$/)
|
311
|
+
begin
|
312
|
+
result_value = map_types(value, element, matches)
|
313
|
+
matches += 1
|
314
|
+
rescue ValidationException
|
315
|
+
next
|
316
|
+
end
|
317
|
+
elsif element.end_with?('{}')
|
318
|
+
result_value, matches = map_hash_type(value, element, group_name, matches)
|
319
|
+
elsif element.end_with?('[]')
|
320
|
+
result_value, matches = map_array_type(value, element, group_name, matches)
|
321
|
+
else
|
322
|
+
begin
|
323
|
+
result_value, matches = map_type(value, element, group_name, matches)
|
324
|
+
rescue StandardError
|
325
|
+
next
|
326
|
+
end
|
327
|
+
end
|
328
|
+
break if group_name == 'anyOf' && matches == 1
|
329
|
+
end
|
330
|
+
raise ValidationException.new(value, template) unless matches == 1
|
331
|
+
|
332
|
+
value = result_value unless result_value.nil?
|
333
|
+
value
|
334
|
+
end
|
335
|
+
|
336
|
+
# Validates and processes the value against the [Hash] type.
|
337
|
+
# @param [String] The value to be mapped against the type.
|
338
|
+
# @param [String] The possible type of the value.
|
339
|
+
# @param [String] The parameter indicates the group (oneOf|anyOf).
|
340
|
+
# @param [Integer] The parameter indicates the number of matches of value against types.
|
341
|
+
def self.map_hash_type(value, type, group_name, matches)
|
342
|
+
if value.instance_of? Hash
|
343
|
+
decoded = {}
|
344
|
+
value.each do |key, val|
|
345
|
+
type = type.chomp('{}').to_s
|
346
|
+
val = map_types(val, type, group_name: group_name)
|
347
|
+
decoded[key] = val unless type.empty?
|
348
|
+
rescue ValidationException
|
349
|
+
next
|
350
|
+
end
|
351
|
+
matches += 1 if decoded.length == value.length
|
352
|
+
value = decoded unless decoded.empty?
|
353
|
+
end
|
354
|
+
[value, matches]
|
355
|
+
end
|
356
|
+
|
357
|
+
# Validates and processes the value against the [Array] type.
|
358
|
+
# @param [String] The value to be mapped against the type.
|
359
|
+
# @param [String] The possible type of the value.
|
360
|
+
# @param [String] The parameter indicates the group (oneOf|anyOf).
|
361
|
+
# @param [Integer] The parameter indicates the number of matches of value against types.
|
362
|
+
def self.map_array_type(value, type, group_name, matches)
|
363
|
+
if value.instance_of? Array
|
364
|
+
decoded = []
|
365
|
+
value.each do |val|
|
366
|
+
type = type.chomp('[]').to_s
|
367
|
+
val = map_types(val, type, group_name: group_name)
|
368
|
+
decoded.append(val) unless type.empty?
|
369
|
+
rescue ValidationException
|
370
|
+
next
|
371
|
+
end
|
372
|
+
matches += 1 if decoded.length == value.length
|
373
|
+
value = decoded unless decoded.empty?
|
374
|
+
end
|
375
|
+
[value, matches]
|
376
|
+
end
|
377
|
+
|
378
|
+
# Validates and processes the value against the type.
|
379
|
+
# @param [String] The value to be mapped against the type.
|
380
|
+
# @param [String] The possible type of the value.
|
381
|
+
# @param [String] The parameter indicates the group (oneOf|anyOf).
|
382
|
+
# @param [Integer] The parameter indicates the number of matches of value against types.
|
383
|
+
def self.map_type(value, type, _group_name, matches)
|
384
|
+
if ApimaticCalculator.constants.select do |c|
|
385
|
+
ApimaticCalculator.const_get(c).to_s == "ApimaticCalculator::#{type}"
|
386
|
+
end.empty?
|
387
|
+
value, matches = map_data_type(value, type, matches)
|
388
|
+
else
|
389
|
+
value, matches = map_complex_type(value, type, matches)
|
390
|
+
end
|
391
|
+
[value, matches]
|
392
|
+
end
|
393
|
+
|
394
|
+
# Validates and processes the value against the complex types.
|
395
|
+
# @param [String] The value to be mapped against the type.
|
396
|
+
# @param [String] The possible type of the value.
|
397
|
+
# @param [Integer] The parameter indicates the number of matches of value against types.
|
398
|
+
def self.map_complex_type(value, type, matches)
|
399
|
+
obj = ApimaticCalculator.const_get(type)
|
400
|
+
value = if obj.respond_to? 'from_hash'
|
401
|
+
obj.send('from_hash', value)
|
402
|
+
else
|
403
|
+
obj.constants.find { |k| obj.const_get(k) == value }
|
404
|
+
end
|
405
|
+
matches += 1 unless value.nil?
|
406
|
+
[value, matches]
|
407
|
+
end
|
408
|
+
|
409
|
+
# Validates and processes the value against the data types.
|
410
|
+
# @param [String] The value to be mapped against the type.
|
411
|
+
# @param [String] The possible type of the value.
|
412
|
+
# @param [Integer] The parameter indicates the number of matches of value against types.
|
413
|
+
def self.map_data_type(value, element, matches)
|
414
|
+
element = element.split('|').map { |x| Object.const_get x }
|
415
|
+
matches += 1 if element.all? { |x| APIHelper.data_types.include?(x) } &&
|
416
|
+
element.any? { |x| (value.instance_of? x) || (value.class.ancestors.include? x) }
|
417
|
+
[value, matches]
|
418
|
+
end
|
419
|
+
|
420
|
+
# Validates the value against the template(group of types).
|
421
|
+
# @param [String] The value to be mapped against the type.
|
422
|
+
# @param [String] The parameter indicates the group of types (oneOf(Integer, String)).
|
423
|
+
def self.validate_types(value, template)
|
424
|
+
map_types(APIHelper.json_deserialize(value.to_json), template)
|
425
|
+
end
|
426
|
+
|
427
|
+
# Get content-type depending on the value
|
428
|
+
def self.get_content_type(value)
|
429
|
+
if serializable_types.map { |x| value.is_a? x }.any?
|
430
|
+
'text/plain; charset=utf-8'
|
431
|
+
else
|
432
|
+
'application/json; charset=utf-8'
|
433
|
+
end
|
434
|
+
end
|
435
|
+
|
436
|
+
# Array of serializable types
|
437
|
+
def self.serializable_types
|
438
|
+
[String, Numeric, TrueClass,
|
439
|
+
FalseClass, Date, DateTime]
|
440
|
+
end
|
441
|
+
|
442
|
+
# Array of supported data types
|
443
|
+
def self.data_types
|
444
|
+
[String, Float, Integer,
|
445
|
+
TrueClass, FalseClass, Date,
|
446
|
+
DateTime, Array, Hash, Object]
|
447
|
+
end
|
448
|
+
end
|
449
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# apimatic_calculator
|
2
|
+
#
|
3
|
+
# This file was automatically generated by APIMATIC v2.0
|
4
|
+
# ( https://apimatic.io ).
|
5
|
+
|
6
|
+
module ApimaticCalculator
|
7
|
+
# apimatic_calculator client class.
|
8
|
+
class Client
|
9
|
+
attr_reader :config
|
10
|
+
|
11
|
+
# Access to simple_calculator controller.
|
12
|
+
# @return [SimpleCalculatorController] Returns the controller instance.
|
13
|
+
def simple_calculator
|
14
|
+
@simple_calculator ||= SimpleCalculatorController.new config
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(connection: nil, adapter: :net_http_persistent, timeout: 60,
|
18
|
+
max_retries: 0, retry_interval: 1, backoff_factor: 2,
|
19
|
+
retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
|
20
|
+
retry_methods: %i[get put],
|
21
|
+
environment: Environment::PRODUCTION, config: nil)
|
22
|
+
@config = if config.nil?
|
23
|
+
Configuration.new(connection: connection, adapter: adapter,
|
24
|
+
timeout: timeout, max_retries: max_retries,
|
25
|
+
retry_interval: retry_interval,
|
26
|
+
backoff_factor: backoff_factor,
|
27
|
+
retry_statuses: retry_statuses,
|
28
|
+
retry_methods: retry_methods,
|
29
|
+
environment: environment)
|
30
|
+
else
|
31
|
+
config
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|