calpackage 1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d78303ce45125c16b3963756734b5631108a7cc9e4b387588dca2bfe6254519b
4
+ data.tar.gz: '08ca1f8085f4e6d91fe2ab33cab43deb929145bb0eca127e3bd62fc3e36aa1cd'
5
+ SHA512:
6
+ metadata.gz: 581e7834c860033f208037036bd05f37342ef05313a6ee716cc2a9e761b49019d1659df9f69a66d8858b450ceff17fce0e59a40942c859da3e26b84fffc0245c
7
+ data.tar.gz: 701ddf51dccefdde5704166082091b2cb4a9f00c9ea83cf6d878fc1fbbe59acab9cb6f59a4eb7de8c28a00d1c6c1711815a5673a2e124aebd2835db8d87785b8
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 - 2020 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,65 @@
1
+
2
+ # Getting Started with APIMATIC Calculator
3
+
4
+ ## Introduction
5
+
6
+ Simple calculator API hosted on APIMATIC
7
+
8
+ ## Install the Package
9
+
10
+ Install the gem from the command line:
11
+
12
+ ```ruby
13
+ gem install calpackage -v 1.0
14
+ ```
15
+
16
+ Or add the gem to your Gemfile and run `bundle`:
17
+
18
+ ```ruby
19
+ gem 'calpackage', '1.0'
20
+ ```
21
+
22
+ For additional gem details, see the [RubyGems page for the calpackage gem](https://rubygems.org/gems/calpackage/versions/1.0).
23
+
24
+ ## Test the SDK
25
+
26
+ To run the tests, navigate to the root directory of the SDK in your terminal and execute the following command:
27
+
28
+ ```
29
+ rake
30
+ ```
31
+
32
+ ## Initialize the API Client
33
+
34
+ **_Note:_** Documentation for the client can be found [here.](/doc/client.md)
35
+
36
+ The following parameters are configurable for the API Client:
37
+
38
+ | Parameter | Type | Description |
39
+ | --- | --- | --- |
40
+ | `environment` | Environment | The API environment. <br> **Default: `Environment.PRODUCTION`** |
41
+ | `timeout` | `Float` | The value to use for connection timeout. <br> **Default: 60** |
42
+ | `max_retries` | `Integer` | The number of times to retry an endpoint call if it fails. <br> **Default: 0** |
43
+ | `retry_interval` | `Float` | Pause in seconds between retries. <br> **Default: 1** |
44
+ | `backoff_factor` | `Float` | The amount to multiply each successive retry's interval amount by in order to provide backoff. <br> **Default: 2** |
45
+ | `retry_statuses` | `Array` | A list of HTTP statuses to retry. <br> **Default: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524]** |
46
+ | `retry_methods` | `Array` | A list of HTTP methods to retry. <br> **Default: %i[get put]** |
47
+
48
+ The API client can be initialized as follows:
49
+
50
+ ```ruby
51
+ client = ApimaticCalculator::Client.new(
52
+ environment: Environment::PRODUCTION,
53
+ )
54
+ ```
55
+
56
+ ## List of APIs
57
+
58
+ * [Simple Calculator](/doc/controllers/simple-calculator.md)
59
+
60
+ ## Classes Documentation
61
+
62
+ * [Utility Classes](/doc/utility-classes.md)
63
+ * [HttpResponse](/doc/http-response.md)
64
+ * [HttpRequest](/doc/http-request.md)
65
+
@@ -0,0 +1,279 @@
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
+ case formatting
17
+ when 'unindexed'
18
+ tuples += array.map { |element| ["#{key}[]", element] }
19
+ when 'indexed'
20
+ tuples += array.map.with_index do |element, index|
21
+ ["#{key}[#{index}]", element]
22
+ end
23
+ when 'plain'
24
+ tuples += array.map { |element| [key, element] }
25
+ else
26
+ raise ArgumentError, 'Invalid format provided.'
27
+ end
28
+ tuples
29
+ end
30
+
31
+ # Replaces template parameters in the given url.
32
+ # @param [String] The query string builder to replace the template
33
+ # parameters.
34
+ # @param [Hash] The parameters to replace in the url.
35
+ def self.append_url_with_template_parameters(query_builder, parameters)
36
+ # perform parameter validation
37
+ unless query_builder.instance_of? String
38
+ raise ArgumentError, 'Given value for parameter \"query_builder\" is
39
+ invalid.'
40
+ end
41
+
42
+ # Return if there are no parameters to replace.
43
+ return query_builder if parameters.nil?
44
+
45
+ parameters.each do |key, val|
46
+ if val.nil?
47
+ replace_value = ''
48
+ elsif val['value'].instance_of? Array
49
+ if val['encode'] == true
50
+ val['value'].map! { |element| CGI.escape(element.to_s) }
51
+ else
52
+ val['value'].map!(&:to_s)
53
+ end
54
+ replace_value = val['value'].join('/')
55
+ else
56
+ replace_value = if val['encode'] == true
57
+ CGI.escape(val['value'].to_s)
58
+ else
59
+ val['value'].to_s
60
+ end
61
+ end
62
+
63
+ # Find the template parameter and replace it with its value.
64
+ query_builder = query_builder.gsub("{#{key}}", replace_value)
65
+ end
66
+ query_builder
67
+ end
68
+
69
+ # Appends the given set of parameters to the given query string.
70
+ # @param [String] The query string builder to add the query parameters to.
71
+ # @param [Hash] The parameters to append.
72
+ def self.append_url_with_query_parameters(query_builder, parameters)
73
+ # Perform parameter validation.
74
+ unless query_builder.instance_of? String
75
+ raise ArgumentError, 'Given value for parameter \"query_builder\"
76
+ is invalid.'
77
+ end
78
+
79
+ # Return if there are no parameters to replace.
80
+ return query_builder if parameters.nil?
81
+
82
+ array_serialization = 'indexed'
83
+
84
+ parameters.each do |key, value|
85
+ seperator = query_builder.include?('?') ? '&' : '?'
86
+ unless value.nil?
87
+ if value.instance_of? Array
88
+ value.compact!
89
+ query_builder += case array_serialization
90
+ when 'csv'
91
+ "#{seperator}#{key}=#{value.map do |element|
92
+ CGI.escape(element.to_s)
93
+ end.join(',')}"
94
+ when 'psv'
95
+ "#{seperator}#{key}=#{value.map do |element|
96
+ CGI.escape(element.to_s)
97
+ end.join('|')}"
98
+ when 'tsv'
99
+ "#{seperator}#{key}=#{value.map do |element|
100
+ CGI.escape(element.to_s)
101
+ end.join("\t")}"
102
+ else
103
+ "#{seperator}#{APIHelper.serialize_array(
104
+ key, value, formatting: array_serialization
105
+ ).map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }
106
+ .join('&')}"
107
+ end
108
+ else
109
+ query_builder += "#{seperator}#{key}=#{CGI.escape(value.to_s)}"
110
+ end
111
+ end
112
+ end
113
+ query_builder
114
+ end
115
+
116
+ # Validates and processes the given Url.
117
+ # @param [String] The given Url to process.
118
+ # @return [String] Pre-processed Url as string.
119
+ def self.clean_url(url)
120
+ # Perform parameter validation.
121
+ raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
122
+
123
+ # Ensure that the urls are absolute.
124
+ matches = url.match(%r{^(https?://[^/]+)})
125
+ raise ArgumentError, 'Invalid Url format.' if matches.nil?
126
+
127
+ # Get the http protocol match.
128
+ protocol = matches[1]
129
+
130
+ # Check if parameters exist.
131
+ index = url.index('?')
132
+
133
+ # Remove redundant forward slashes.
134
+ query = url[protocol.length...(!index.nil? ? index : url.length)]
135
+ query.gsub!(%r{//+}, '/')
136
+
137
+ # Get the parameters.
138
+ parameters = !index.nil? ? url[url.index('?')...url.length] : ''
139
+
140
+ # Return processed url.
141
+ protocol + query + parameters
142
+ end
143
+
144
+ # Parses JSON string.
145
+ # @param [String] A JSON string.
146
+ def self.json_deserialize(json)
147
+ JSON.parse(json)
148
+ rescue StandardError
149
+ raise TypeError, 'Server responded with invalid JSON.'
150
+ end
151
+
152
+ # Removes elements with empty values from a hash.
153
+ # @param [Hash] The hash to clean.
154
+ def self.clean_hash(hash)
155
+ hash.delete_if { |_key, value| value.to_s.strip.empty? }
156
+ end
157
+
158
+ # Form encodes a hash of parameters.
159
+ # @param [Hash] The hash of parameters to encode.
160
+ # @return [Hash] A hash with the same parameters form encoded.
161
+ def self.form_encode_parameters(form_parameters)
162
+ array_serialization = 'indexed'
163
+ encoded = {}
164
+ form_parameters.each do |key, value|
165
+ encoded.merge!(APIHelper.form_encode(value, key, formatting:
166
+ array_serialization))
167
+ end
168
+ encoded
169
+ end
170
+
171
+ def self.custom_merge(a, b)
172
+ x = {}
173
+ a.each do |key, value_a|
174
+ b.each do |k, value_b|
175
+ next unless key == k
176
+
177
+ x[k] = []
178
+ if value_a.instance_of? Array
179
+ value_a.each do |v|
180
+ x[k].push(v)
181
+ end
182
+ else
183
+ x[k].push(value_a)
184
+ end
185
+ if value_b.instance_of? Array
186
+ value_b.each do |v|
187
+ x[k].push(v)
188
+ end
189
+ else
190
+ x[k].push(value_b)
191
+ end
192
+ a.delete(k)
193
+ b.delete(k)
194
+ end
195
+ end
196
+ x.merge!(a)
197
+ x.merge!(b)
198
+ x
199
+ end
200
+
201
+ # Form encodes an object.
202
+ # @param [Dynamic] An object to form encode.
203
+ # @param [String] The name of the object.
204
+ # @return [Hash] A form encoded representation of the object in the form
205
+ # of a hash.
206
+ def self.form_encode(obj, instance_name, formatting: 'indexed')
207
+ retval = {}
208
+
209
+ serializable_types = [String, Numeric, TrueClass,
210
+ FalseClass, Date, DateTime]
211
+
212
+ # If this is a structure, resolve it's field names.
213
+ obj = obj.to_hash if obj.is_a? BaseModel
214
+
215
+ # Create a form encoded hash for this object.
216
+ if obj.nil?
217
+ nil
218
+ elsif obj.instance_of? Array
219
+ if formatting == 'indexed'
220
+ obj.each_with_index do |value, index|
221
+ retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{index}]"))
222
+ end
223
+ elsif serializable_types.map { |x| obj[0].is_a? x }.any?
224
+ obj.each do |value|
225
+ abc = if formatting == 'unindexed'
226
+ APIHelper.form_encode(value, "#{instance_name}[]",
227
+ formatting: formatting)
228
+ else
229
+ APIHelper.form_encode(value, instance_name,
230
+ formatting: formatting)
231
+ end
232
+ retval = APIHelper.custom_merge(retval, abc)
233
+ end
234
+ else
235
+ obj.each_with_index do |value, index|
236
+ retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{index}]",
237
+ formatting: formatting))
238
+ end
239
+ end
240
+ elsif obj.instance_of? Hash
241
+ obj.each do |key, value|
242
+ retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{key}]",
243
+ formatting: formatting))
244
+ end
245
+ elsif obj.instance_of? File
246
+ retval[instance_name] = UploadIO.new(
247
+ obj, 'application/octet-stream', File.basename(obj.path)
248
+ )
249
+ else
250
+ retval[instance_name] = obj
251
+ end
252
+ retval
253
+ end
254
+
255
+ # Retrieves a field from a Hash/Array based on an Array of keys/indexes
256
+ # @param [Hash, Array] The hash to extract data from
257
+ # @param [Array<String, Integer>] The keys/indexes to use
258
+ # @return [Object] The extracted value
259
+ def self.map_response(obj, keys)
260
+ val = obj
261
+ begin
262
+ keys.each do |key|
263
+ val = if val.is_a? Array
264
+ if key.to_i.to_s == key
265
+ val[key.to_i]
266
+ else
267
+ val = nil
268
+ end
269
+ else
270
+ val.fetch(key.to_sym)
271
+ end
272
+ end
273
+ rescue NoMethodError, TypeError, IndexError
274
+ val = nil
275
+ end
276
+ val
277
+ end
278
+ end
279
+ 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(http_client_instance: nil, timeout: 60, max_retries: 0,
18
+ 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(http_client_instance: http_client_instance,
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
@@ -0,0 +1,111 @@
1
+ # apimatic_calculator
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module ApimaticCalculator
7
+ # An enum for SDK environments.
8
+ class Environment
9
+ # PRODUCTION: This environment connect to the LIVE calculator API
10
+ ENVIRONMENT = [
11
+ PRODUCTION = 'production'.freeze
12
+ ].freeze
13
+ end
14
+
15
+ # An enum for API servers.
16
+ class Server
17
+ SERVER = [
18
+ CALCULATOR = 'Calculator'.freeze
19
+ ].freeze
20
+ end
21
+
22
+ # All configuration including auth info and base URI for the API access
23
+ # are configured in this class.
24
+ class Configuration
25
+ # The attribute readers for properties.
26
+ attr_reader :http_client, :http_client_instance, :timeout, :max_retries, :retry_interval,
27
+ :backoff_factor, :retry_statuses, :retry_methods, :environment
28
+
29
+ class << self
30
+ attr_reader :environments
31
+ end
32
+
33
+ def initialize(http_client_instance: nil, timeout: 60, max_retries: 0,
34
+ retry_interval: 1, backoff_factor: 2,
35
+ retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
36
+ retry_methods: %i[get put],
37
+ environment: Environment::PRODUCTION)
38
+ # The Http Client passed from the sdk user for making requests
39
+ @http_client_instance = http_client_instance
40
+
41
+ # The value to use for connection timeout
42
+ @timeout = timeout
43
+
44
+ # The number of times to retry an endpoint call if it fails
45
+ @max_retries = max_retries
46
+
47
+ # Pause in seconds between retries
48
+ @retry_interval = retry_interval
49
+
50
+ # The amount to multiply each successive retry's interval amount
51
+ # by in order to provide backoff
52
+ @backoff_factor = backoff_factor
53
+
54
+ # A list of HTTP statuses to retry
55
+ @retry_statuses = retry_statuses
56
+
57
+ # A list of HTTP methods to retry
58
+ @retry_methods = retry_methods
59
+
60
+ # Current API environment
61
+ @environment = String(environment)
62
+
63
+ # The Http Client to use for making requests.
64
+ @http_client = create_http_client
65
+ end
66
+
67
+ def clone_with(http_client_instance: nil, timeout: nil, max_retries: nil,
68
+ retry_interval: nil, backoff_factor: nil,
69
+ retry_statuses: nil, retry_methods: nil, environment: nil)
70
+ http_client_instance ||= self.http_client_instance
71
+ timeout ||= self.timeout
72
+ max_retries ||= self.max_retries
73
+ retry_interval ||= self.retry_interval
74
+ backoff_factor ||= self.backoff_factor
75
+ retry_statuses ||= self.retry_statuses
76
+ retry_methods ||= self.retry_methods
77
+ environment ||= self.environment
78
+
79
+ Configuration.new(http_client_instance: http_client_instance,
80
+ timeout: timeout, max_retries: max_retries,
81
+ retry_interval: retry_interval,
82
+ backoff_factor: backoff_factor,
83
+ retry_statuses: retry_statuses,
84
+ retry_methods: retry_methods, environment: environment)
85
+ end
86
+
87
+ def create_http_client
88
+ FaradayClient.new(timeout: timeout, max_retries: max_retries,
89
+ retry_interval: retry_interval,
90
+ backoff_factor: backoff_factor,
91
+ retry_statuses: retry_statuses,
92
+ retry_methods: retry_methods,
93
+ http_client_instance: http_client_instance)
94
+ end
95
+
96
+ # All the environments the SDK can run in.
97
+ ENVIRONMENTS = {
98
+ Environment::PRODUCTION => {
99
+ Server::CALCULATOR => 'https://examples.apimatic.io/apps/calculator'
100
+ }
101
+ }.freeze
102
+
103
+ # Generates the appropriate base URI for the environment and the server.
104
+ # @param [Configuration::Server] The server enum for which the base URI is
105
+ # required.
106
+ # @return [String] The base URI.
107
+ def get_base_uri(server = Server::CALCULATOR)
108
+ ENVIRONMENTS[environment][server].clone
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,47 @@
1
+ # apimatic_calculator
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module ApimaticCalculator
7
+ # BaseController.
8
+ class BaseController
9
+ attr_accessor :config, :http_call_back
10
+
11
+ def initialize(config, http_call_back: nil)
12
+ @config = config
13
+ @http_call_back = http_call_back
14
+
15
+ @global_headers = {
16
+ 'user-agent' => 'APIMATIC 3.0'
17
+ }
18
+ end
19
+
20
+ def validate_parameters(args)
21
+ args.each do |_name, value|
22
+ raise ArgumentError, "Required parameter #{_name} cannot be nil." if value.nil?
23
+ end
24
+ end
25
+
26
+ def execute_request(request, binary: false)
27
+ @http_call_back&.on_before_request(request)
28
+
29
+ APIHelper.clean_hash(request.headers)
30
+ request.headers.merge!(@global_headers)
31
+
32
+ response = if binary
33
+ config.http_client.execute_as_binary(request)
34
+ else
35
+ config.http_client.execute_as_string(request)
36
+ end
37
+ @http_call_back&.on_after_response(response)
38
+
39
+ response
40
+ end
41
+
42
+ def validate_response(response)
43
+ raise APIException.new 'HTTP Response Not OK', response unless
44
+ response.status_code.between?(200, 208) # [200,208] = HTTP OK
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,45 @@
1
+ # apimatic_calculator
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module ApimaticCalculator
7
+ # SimpleCalculatorController
8
+ class SimpleCalculatorController < BaseController
9
+ def initialize(config, http_call_back: nil)
10
+ super(config, http_call_back: http_call_back)
11
+ end
12
+
13
+ # Calculates the expression using the specified operation.
14
+ # @param [OperationTypeEnum] operation Required parameter: The operator to
15
+ # apply on the variables
16
+ # @param [Float] x Required parameter: The LHS value
17
+ # @param [Float] y Required parameter: The RHS value
18
+ # @return [Float] response from the API call
19
+ def get_calculate(options = {})
20
+ # Prepare query url.
21
+ _query_builder = config.get_base_uri
22
+ _query_builder << '/{operation}'
23
+ _query_builder = APIHelper.append_url_with_template_parameters(
24
+ _query_builder,
25
+ 'operation' => { 'value' => options['operation'], 'encode' => true }
26
+ )
27
+ _query_builder = APIHelper.append_url_with_query_parameters(
28
+ _query_builder,
29
+ 'x' => options['x'],
30
+ 'y' => options['y']
31
+ )
32
+ _query_url = APIHelper.clean_url _query_builder
33
+
34
+ # Prepare and execute HttpRequest.
35
+ _request = config.http_client.get(
36
+ _query_url
37
+ )
38
+ _response = execute_request(_request)
39
+ validate_response(_response)
40
+
41
+ # Return appropriate response type.
42
+ _response.raw_body.to_f
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,20 @@
1
+ # apimatic_calculator
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module ApimaticCalculator
7
+ # Class for exceptions when there is a network error, status code error, etc.
8
+ class APIException < StandardError
9
+ attr_reader :response, :response_code
10
+
11
+ # The constructor.
12
+ # @param [String] The reason for raising an exception.
13
+ # @param [HttpResponse] The HttpReponse of the API call.
14
+ def initialize(reason, response)
15
+ super(reason)
16
+ @response = response
17
+ @response_code = response.status_code
18
+ end
19
+ end
20
+ end