BATester 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +28 -0
- data/README.md +182 -0
- data/lib/ba_tester_with_custom_parameters.rb +40 -0
- data/lib/ba_tester_with_custom_parameters/api_helper.rb +289 -0
- data/lib/ba_tester_with_custom_parameters/client.rb +33 -0
- data/lib/ba_tester_with_custom_parameters/configuration.rb +130 -0
- data/lib/ba_tester_with_custom_parameters/controllers/api_controller.rb +33 -0
- data/lib/ba_tester_with_custom_parameters/controllers/base_controller.rb +49 -0
- data/lib/ba_tester_with_custom_parameters/exceptions/api_exception.rb +20 -0
- data/lib/ba_tester_with_custom_parameters/http/auth/basic_auth.rb +22 -0
- data/lib/ba_tester_with_custom_parameters/http/faraday_client.rb +67 -0
- data/lib/ba_tester_with_custom_parameters/http/http_call_back.rb +24 -0
- data/lib/ba_tester_with_custom_parameters/http/http_client.rb +104 -0
- data/lib/ba_tester_with_custom_parameters/http/http_method_enum.rb +13 -0
- data/lib/ba_tester_with_custom_parameters/http/http_request.rb +50 -0
- data/lib/ba_tester_with_custom_parameters/http/http_response.rb +29 -0
- data/lib/ba_tester_with_custom_parameters/models/base_model.rb +36 -0
- data/lib/ba_tester_with_custom_parameters/models/suite_code_enum.rb +23 -0
- data/lib/ba_tester_with_custom_parameters/utilities/file_wrapper.rb +17 -0
- data/test/controllers/controller_test_base.rb +21 -0
- data/test/controllers/test_api_controller.rb +29 -0
- data/test/http_response_catcher.rb +19 -0
- data/test/test_helper.rb +94 -0
- metadata +182 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: dc60ef680c3edf8a885861f678b83655a9df04e43142a1b3b8c0b5eaf3f1959a
|
4
|
+
data.tar.gz: a2eb5db5dc48637b5199e0d9e9e3713e79916d3ba3a3426a73d264ad2d0eaf9c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 49b2f03dc3a6f68663c92c9445a1f612e90e7d204963c7eb55121c3ab5108b53845bbba274641051a0b03942b9122c1f5c8cd503d2a319c5063c4ff6010dbdb3
|
7
|
+
data.tar.gz: 51bb72a762f0a2c12542627eb6c67189df681992a7ba5c5b557e68b33778f2a7f5c1481e5379a6e85c9827e9e993313faf5235f95fc4976cb6fc5a9bc4727703
|
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,182 @@
|
|
1
|
+
# Getting Started with BATester with custom parameters
|
2
|
+
|
3
|
+
## Getting Started
|
4
|
+
|
5
|
+
### Install the Package
|
6
|
+
|
7
|
+
Install the gem from the command line:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem install BATester -v 1.1.0
|
11
|
+
```
|
12
|
+
|
13
|
+
Or add the gem to your Gemfile and run `bundle`:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'BATester', '1.1.0'
|
17
|
+
```
|
18
|
+
|
19
|
+
For additional gem details, see the [RubyGems page for the BATester gem](https://rubygems.org/gems/BATester/versions/1.1.0).
|
20
|
+
|
21
|
+
### Environments
|
22
|
+
|
23
|
+
The SDK can be configured to use a different environment for making API calls. Available environments are:
|
24
|
+
|
25
|
+
#### Fields
|
26
|
+
|
27
|
+
| Name | Description |
|
28
|
+
| --- | --- |
|
29
|
+
| production | - |
|
30
|
+
| testing | **Default** |
|
31
|
+
|
32
|
+
### Initialize the API Client
|
33
|
+
|
34
|
+
The following parameters are configurable for the API Client:
|
35
|
+
|
36
|
+
| Parameter | Type | Description |
|
37
|
+
| --- | --- | --- |
|
38
|
+
| `username` | `String` | *Default*: `'farhan'` |
|
39
|
+
| `password` | `String` | *Default*: `'apimatic'` |
|
40
|
+
| `port` | `String` | *Default*: `'80'` |
|
41
|
+
| `suites` | `SuiteCodeEnum` | *Default*: `SuiteCodeEnum::HEARTS` |
|
42
|
+
| `environment` | Environment | The API environment. <br> **Default: `Environment.TESTING`** |
|
43
|
+
| `timeout` | `Float` | The value to use for connection timeout. <br> **Default: 60** |
|
44
|
+
| `max_retries` | `Integer` | The number of times to retry an endpoint call if it fails. <br> **Default: 0** |
|
45
|
+
| `retry_interval` | `Float` | Pause in seconds between retries. <br> **Default: 1** |
|
46
|
+
| `backoff_factor` | `Float` | The amount to multiply each successive retry's interval amount by in order to provide backoff. <br> **Default: 1** |
|
47
|
+
|
48
|
+
The API client can be initialized as follows:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
client = BaTesterWithCustomParameters::Client.new(
|
52
|
+
username: 'farhan',
|
53
|
+
password: 'apimatic',
|
54
|
+
environment: Environment::TESTING,
|
55
|
+
port: '80',
|
56
|
+
suites: SuiteCodeEnum::HEARTS,
|
57
|
+
)
|
58
|
+
```
|
59
|
+
|
60
|
+
### Authorization
|
61
|
+
|
62
|
+
This API uses `Basic Authentication`.
|
63
|
+
|
64
|
+
### Test the SDK
|
65
|
+
|
66
|
+
To run the tests, navigate to the root directory of the SDK in your terminal and execute the following command:
|
67
|
+
|
68
|
+
```
|
69
|
+
rake
|
70
|
+
```
|
71
|
+
|
72
|
+
## Client Class Documentation
|
73
|
+
|
74
|
+
### BATester with custom parameters Client
|
75
|
+
|
76
|
+
The gateway for the SDK. This class acts as a factory for the Controllers and also holds the configuration of the SDK.
|
77
|
+
|
78
|
+
### Controllers
|
79
|
+
|
80
|
+
| Name | Description |
|
81
|
+
| --- | --- |
|
82
|
+
| api | Gets APIController |
|
83
|
+
|
84
|
+
## API Reference
|
85
|
+
|
86
|
+
### List of APIs
|
87
|
+
|
88
|
+
* [API](#api)
|
89
|
+
|
90
|
+
### API
|
91
|
+
|
92
|
+
#### Overview
|
93
|
+
|
94
|
+
##### Get instance
|
95
|
+
|
96
|
+
An instance of the `APIController` class can be accessed from the API Client.
|
97
|
+
|
98
|
+
```
|
99
|
+
client_controller = client.client
|
100
|
+
```
|
101
|
+
|
102
|
+
#### Get Basic Auth Test
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
def get_basic_auth_test
|
106
|
+
```
|
107
|
+
|
108
|
+
##### Response Type
|
109
|
+
|
110
|
+
`String`
|
111
|
+
|
112
|
+
##### Example Usage
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
result = client_controller.get_basic_auth_test()
|
116
|
+
```
|
117
|
+
|
118
|
+
## Model Reference
|
119
|
+
|
120
|
+
### Enumerations
|
121
|
+
|
122
|
+
* [Suite Code](#suite-code)
|
123
|
+
|
124
|
+
#### Suite Code
|
125
|
+
|
126
|
+
A integer based enum representing a Suite in a game of cards
|
127
|
+
|
128
|
+
##### Class Name
|
129
|
+
|
130
|
+
`SuiteCodeEnum`
|
131
|
+
|
132
|
+
##### Fields
|
133
|
+
|
134
|
+
| Name |
|
135
|
+
| --- |
|
136
|
+
| `HEARTS` |
|
137
|
+
| `SPADES` |
|
138
|
+
| `CLUBS` |
|
139
|
+
| `DIAMONDS` |
|
140
|
+
|
141
|
+
## Utility Classes Documentation
|
142
|
+
|
143
|
+
### ApiHelper Class
|
144
|
+
|
145
|
+
API utility class.
|
146
|
+
|
147
|
+
### Methods
|
148
|
+
|
149
|
+
| Name | Return Type | Description |
|
150
|
+
| --- | --- | --- |
|
151
|
+
| json_deserialize | Hash | Deserializes a JSON string to a Ruby Hash. |
|
152
|
+
| rfc3339 | DateTime | Safely converts a string into an RFC3339 DateTime object. |
|
153
|
+
|
154
|
+
## Common Code Documentation
|
155
|
+
|
156
|
+
### HttpResponse
|
157
|
+
|
158
|
+
Http response received.
|
159
|
+
|
160
|
+
#### Properties
|
161
|
+
|
162
|
+
| Name | Type | Description |
|
163
|
+
| --- | --- | --- |
|
164
|
+
| status_code | Integer | The status code returned by the server. |
|
165
|
+
| reason_phrase | String | The reason phrase returned by the server. |
|
166
|
+
| headers | Hash | Response headers. |
|
167
|
+
| raw_body | String | Response body. |
|
168
|
+
| request | HttpRequest | The request that resulted in this response. |
|
169
|
+
|
170
|
+
### HttpRequest
|
171
|
+
|
172
|
+
Represents a single Http Request.
|
173
|
+
|
174
|
+
#### Properties
|
175
|
+
|
176
|
+
| Name | Type | Tag | Description |
|
177
|
+
| --- | --- | --- | --- |
|
178
|
+
| http_method | HttpMethodEnum | | The HTTP method of the request. |
|
179
|
+
| query_url | String | | The endpoint URL for the API request. |
|
180
|
+
| headers | Hash | Optional | Request headers. |
|
181
|
+
| parameters | Hash | Optional | Request body. |
|
182
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# ba_tester_with_custom_parameters
|
2
|
+
#
|
3
|
+
# This file was automatically generated by APIMATIC v2.0
|
4
|
+
# ( https://apimatic.io ).
|
5
|
+
|
6
|
+
require 'date'
|
7
|
+
require 'json'
|
8
|
+
require 'faraday'
|
9
|
+
require 'certifi'
|
10
|
+
require 'logging'
|
11
|
+
|
12
|
+
require_relative 'ba_tester_with_custom_parameters/api_helper.rb'
|
13
|
+
require_relative 'ba_tester_with_custom_parameters/client.rb'
|
14
|
+
|
15
|
+
# Utilities
|
16
|
+
require_relative 'ba_tester_with_custom_parameters/utilities/file_wrapper.rb'
|
17
|
+
|
18
|
+
# Http
|
19
|
+
require_relative 'ba_tester_with_custom_parameters/http/http_call_back.rb'
|
20
|
+
require_relative 'ba_tester_with_custom_parameters/http/http_client.rb'
|
21
|
+
require_relative 'ba_tester_with_custom_parameters/http/faraday_client.rb'
|
22
|
+
require_relative 'ba_tester_with_custom_parameters/http/http_method_enum.rb'
|
23
|
+
require_relative 'ba_tester_with_custom_parameters/http/http_request.rb'
|
24
|
+
require_relative 'ba_tester_with_custom_parameters/http/http_response.rb'
|
25
|
+
require_relative 'ba_tester_with_custom_parameters/http/auth/basic_auth.rb'
|
26
|
+
|
27
|
+
# Models
|
28
|
+
require_relative 'ba_tester_with_custom_parameters/models/base_model.rb'
|
29
|
+
require_relative 'ba_tester_with_custom_parameters/models/suite_code_enum.rb'
|
30
|
+
|
31
|
+
# Exceptions
|
32
|
+
require_relative 'ba_tester_with_custom_parameters/exceptions/api_exception.rb'
|
33
|
+
|
34
|
+
require_relative 'ba_tester_with_custom_parameters/configuration.rb'
|
35
|
+
|
36
|
+
# Controllers
|
37
|
+
require_relative 'ba_tester_with_custom_parameters/controllers/base' \
|
38
|
+
'_controller.rb'
|
39
|
+
require_relative 'ba_tester_with_custom_parameters/controllers/api' \
|
40
|
+
'_controller.rb'
|
@@ -0,0 +1,289 @@
|
|
1
|
+
# ba_tester_with_custom_parameters
|
2
|
+
#
|
3
|
+
# This file was automatically generated by APIMATIC v2.0
|
4
|
+
# ( https://apimatic.io ).
|
5
|
+
|
6
|
+
module BaTesterWithCustomParameters
|
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
|
+
if formatting == 'unindexed'
|
17
|
+
tuples += array.map { |element| ["#{key}[]", element] }
|
18
|
+
elsif formatting == 'indexed'
|
19
|
+
tuples += array.map.with_index do |element, index|
|
20
|
+
["#{key}[#{index}]", element]
|
21
|
+
end
|
22
|
+
elsif formatting == 'plain'
|
23
|
+
tuples += array.map { |element| [key, element] }
|
24
|
+
else
|
25
|
+
raise ArgumentError, 'Invalid format provided.'
|
26
|
+
end
|
27
|
+
tuples
|
28
|
+
end
|
29
|
+
|
30
|
+
# Replaces template parameters in the given url.
|
31
|
+
# @param [String] The query string builder to replace the template
|
32
|
+
# parameters.
|
33
|
+
# @param [Hash] The parameters to replace in the url.
|
34
|
+
def self.append_url_with_template_parameters(query_builder, parameters)
|
35
|
+
# perform parameter validation
|
36
|
+
unless query_builder.instance_of? String
|
37
|
+
raise ArgumentError, 'Given value for parameter \"query_builder\" is
|
38
|
+
invalid.'
|
39
|
+
end
|
40
|
+
|
41
|
+
# Return if there are no parameters to replace.
|
42
|
+
return query_builder if parameters.nil?
|
43
|
+
|
44
|
+
parameters.each do |key, val|
|
45
|
+
if val.nil?
|
46
|
+
replace_value = ''
|
47
|
+
elsif val['value'].instance_of? Array
|
48
|
+
if val['encode'] == true
|
49
|
+
val['value'].map! { |element| CGI.escape(element.to_s) }
|
50
|
+
else
|
51
|
+
val['value'].map!(&:to_s)
|
52
|
+
end
|
53
|
+
replace_value = val['value'].join('/')
|
54
|
+
else
|
55
|
+
replace_value = if val['encode'] == true
|
56
|
+
CGI.escape(val['value'].to_s)
|
57
|
+
else
|
58
|
+
val['value'].to_s
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Find the template parameter and replace it with its value.
|
63
|
+
query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value)
|
64
|
+
end
|
65
|
+
query_builder
|
66
|
+
end
|
67
|
+
|
68
|
+
# Appends the given set of parameters to the given query string.
|
69
|
+
# @param [String] The query string builder to add the query parameters to.
|
70
|
+
# @param [Hash] The parameters to append.
|
71
|
+
def self.append_url_with_query_parameters(query_builder, parameters)
|
72
|
+
# Perform parameter validation.
|
73
|
+
unless query_builder.instance_of? String
|
74
|
+
raise ArgumentError, 'Given value for parameter \"query_builder\"
|
75
|
+
is invalid.'
|
76
|
+
end
|
77
|
+
|
78
|
+
# Return if there are no parameters to replace.
|
79
|
+
return query_builder if parameters.nil?
|
80
|
+
|
81
|
+
array_serialization = 'indexed'
|
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
|
+
query_builder += if array_serialization == 'csv'
|
89
|
+
"#{seperator}#{key}=#{value.map do |element|
|
90
|
+
CGI.escape(element.to_s)
|
91
|
+
end.join(',')}"
|
92
|
+
elsif array_serialization == 'psv'
|
93
|
+
"#{seperator}#{key}=#{value.map do |element|
|
94
|
+
CGI.escape(element.to_s)
|
95
|
+
end.join('|')}"
|
96
|
+
elsif array_serialization == 'tsv'
|
97
|
+
"#{seperator}#{key}=#{value.map do |element|
|
98
|
+
CGI.escape(element.to_s)
|
99
|
+
end.join("\t")}"
|
100
|
+
else
|
101
|
+
"#{seperator}#{APIHelper.serialize_array(
|
102
|
+
key, value, formatting: array_serialization
|
103
|
+
).map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }
|
104
|
+
.join('&')}"
|
105
|
+
end
|
106
|
+
else
|
107
|
+
query_builder += "#{seperator}#{key}=#{CGI.escape(value.to_s)}"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
query_builder
|
112
|
+
end
|
113
|
+
|
114
|
+
# Validates and processes the given Url.
|
115
|
+
# @param [String] The given Url to process.
|
116
|
+
# @return [String] Pre-processed Url as string.
|
117
|
+
def self.clean_url(url)
|
118
|
+
# Perform parameter validation.
|
119
|
+
raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
|
120
|
+
|
121
|
+
# Ensure that the urls are absolute.
|
122
|
+
matches = url.match(%r{^(https?:\/\/[^\/]+)})
|
123
|
+
raise ArgumentError, 'Invalid Url format.' if matches.nil?
|
124
|
+
|
125
|
+
# Get the http protocol match.
|
126
|
+
protocol = matches[1]
|
127
|
+
|
128
|
+
# Check if parameters exist.
|
129
|
+
index = url.index('?')
|
130
|
+
|
131
|
+
# Remove redundant forward slashes.
|
132
|
+
query = url[protocol.length...(!index.nil? ? index : url.length)]
|
133
|
+
query.gsub!(%r{\/\/+}, '/')
|
134
|
+
|
135
|
+
# Get the parameters.
|
136
|
+
parameters = !index.nil? ? url[url.index('?')...url.length] : ''
|
137
|
+
|
138
|
+
# Return processed url.
|
139
|
+
protocol + query + parameters
|
140
|
+
end
|
141
|
+
|
142
|
+
# Parses JSON string.
|
143
|
+
# @param [String] A JSON string.
|
144
|
+
def self.json_deserialize(json)
|
145
|
+
return JSON.parse(json)
|
146
|
+
rescue StandardError
|
147
|
+
raise TypeError, 'Server responded with invalid JSON.'
|
148
|
+
end
|
149
|
+
|
150
|
+
# Removes elements with empty values from a hash.
|
151
|
+
# @param [Hash] The hash to clean.
|
152
|
+
def self.clean_hash(hash)
|
153
|
+
hash.delete_if { |_key, value| value.to_s.strip.empty? }
|
154
|
+
end
|
155
|
+
|
156
|
+
# Form encodes a hash of parameters.
|
157
|
+
# @param [Hash] The hash of parameters to encode.
|
158
|
+
# @return [Hash] A hash with the same parameters form encoded.
|
159
|
+
def self.form_encode_parameters(form_parameters)
|
160
|
+
array_serialization = 'indexed'
|
161
|
+
encoded = {}
|
162
|
+
form_parameters.each do |key, value|
|
163
|
+
encoded.merge!(APIHelper.form_encode(value, key, formatting:
|
164
|
+
array_serialization))
|
165
|
+
end
|
166
|
+
encoded
|
167
|
+
end
|
168
|
+
|
169
|
+
def self.custom_merge(a, b)
|
170
|
+
x = {}
|
171
|
+
a.each do |key, value_a|
|
172
|
+
b.each do |k, value_b|
|
173
|
+
next unless key == k
|
174
|
+
x[k] = []
|
175
|
+
if value_a.instance_of? Array
|
176
|
+
value_a.each do |v|
|
177
|
+
x[k].push(v)
|
178
|
+
end
|
179
|
+
else
|
180
|
+
x[k].push(value_a)
|
181
|
+
end
|
182
|
+
if value_b.instance_of? Array
|
183
|
+
value_b.each do |v|
|
184
|
+
x[k].push(v)
|
185
|
+
end
|
186
|
+
else
|
187
|
+
x[k].push(value_b)
|
188
|
+
end
|
189
|
+
a.delete(k)
|
190
|
+
b.delete(k)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
x.merge!(a)
|
194
|
+
x.merge!(b)
|
195
|
+
x
|
196
|
+
end
|
197
|
+
|
198
|
+
# Form encodes an object.
|
199
|
+
# @param [Dynamic] An object to form encode.
|
200
|
+
# @param [String] The name of the object.
|
201
|
+
# @return [Hash] A form encoded representation of the object in the form
|
202
|
+
# of a hash.
|
203
|
+
def self.form_encode(obj, instance_name, formatting: 'indexed')
|
204
|
+
retval = {}
|
205
|
+
|
206
|
+
serializable_types = [String, Numeric, TrueClass,
|
207
|
+
FalseClass, Date, DateTime]
|
208
|
+
|
209
|
+
# If this is a structure, resolve it's field names.
|
210
|
+
obj = obj.to_hash if obj.is_a? BaseModel
|
211
|
+
|
212
|
+
# Create a form encoded hash for this object.
|
213
|
+
if obj.nil?
|
214
|
+
nil
|
215
|
+
elsif obj.instance_of? Array
|
216
|
+
if formatting == 'indexed'
|
217
|
+
obj.each_with_index do |value, index|
|
218
|
+
retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
|
219
|
+
index.to_s + ']'))
|
220
|
+
end
|
221
|
+
elsif serializable_types.map { |x| obj[0].is_a? x }.any?
|
222
|
+
obj.each do |value|
|
223
|
+
abc = if formatting == 'unindexed'
|
224
|
+
APIHelper.form_encode(value, instance_name + '[]',
|
225
|
+
formatting: formatting)
|
226
|
+
else
|
227
|
+
APIHelper.form_encode(value, instance_name,
|
228
|
+
formatting: formatting)
|
229
|
+
end
|
230
|
+
retval = APIHelper.custom_merge(retval, abc)
|
231
|
+
end
|
232
|
+
else
|
233
|
+
obj.each_with_index do |value, index|
|
234
|
+
retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
|
235
|
+
index.to_s + ']', formatting: formatting))
|
236
|
+
end
|
237
|
+
end
|
238
|
+
elsif obj.instance_of? Hash
|
239
|
+
obj.each do |key, value|
|
240
|
+
retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
|
241
|
+
key.to_s + ']', formatting: formatting))
|
242
|
+
end
|
243
|
+
elsif obj.instance_of? File
|
244
|
+
retval[instance_name] = UploadIO.new(
|
245
|
+
obj, 'application/octet-stream', File.basename(obj.path)
|
246
|
+
)
|
247
|
+
else
|
248
|
+
retval[instance_name] = obj
|
249
|
+
end
|
250
|
+
retval
|
251
|
+
end
|
252
|
+
|
253
|
+
# Retrieves a field from a Hash/Array based on an Array of keys/indexes
|
254
|
+
# @param [Hash, Array] The hash to extract data from
|
255
|
+
# @param [Array<String, Integer>] The keys/indexes to use
|
256
|
+
# @return [Object] The extracted value
|
257
|
+
def self.map_response(obj, keys)
|
258
|
+
val = obj
|
259
|
+
begin
|
260
|
+
keys.each do |key|
|
261
|
+
val = if val.is_a? Array
|
262
|
+
if key.to_i.to_s == key
|
263
|
+
val[key.to_i]
|
264
|
+
else
|
265
|
+
val = nil
|
266
|
+
end
|
267
|
+
else
|
268
|
+
val.fetch(key.to_sym)
|
269
|
+
end
|
270
|
+
end
|
271
|
+
rescue NoMethodError, TypeError, IndexError
|
272
|
+
val = nil
|
273
|
+
end
|
274
|
+
val
|
275
|
+
end
|
276
|
+
|
277
|
+
# Safely converts a string into an rfc3339 DateTime object
|
278
|
+
# @param [String] The datetime string
|
279
|
+
# @return [DateTime] A DateTime object of rfc3339 format
|
280
|
+
def self.rfc3339(date_time)
|
281
|
+
# missing timezone information
|
282
|
+
if date_time.end_with?('Z') || date_time.index('+')
|
283
|
+
DateTime.rfc3339(date_time)
|
284
|
+
else
|
285
|
+
DateTime.rfc3339(date_time + 'Z')
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|