My-first-api-matic 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 +317 -0
- data/lib/youtube.rb +41 -0
- data/lib/youtube/api_helper.rb +289 -0
- data/lib/youtube/client.rb +34 -0
- data/lib/youtube/configuration.rb +110 -0
- data/lib/youtube/controllers/api_controller.rb +59 -0
- data/lib/youtube/controllers/base_controller.rb +55 -0
- data/lib/youtube/exceptions/api_exception.rb +20 -0
- data/lib/youtube/exceptions/problem_details_exception.rb +44 -0
- data/lib/youtube/http/faraday_client.rb +70 -0
- data/lib/youtube/http/http_call_back.rb +24 -0
- data/lib/youtube/http/http_client.rb +104 -0
- data/lib/youtube/http/http_method_enum.rb +13 -0
- data/lib/youtube/http/http_request.rb +50 -0
- data/lib/youtube/http/http_response.rb +29 -0
- data/lib/youtube/models/base_model.rb +36 -0
- data/lib/youtube/models/field_parameter_validator.rb +80 -0
- data/lib/youtube/models/test_enum.rb +20 -0
- data/lib/youtube/models/weather_forcase_json_imported.rb +62 -0
- data/lib/youtube/models/weather_forecast.rb +63 -0
- data/lib/youtube/utilities/file_wrapper.rb +17 -0
- metadata +147 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0b28501632ae5cc4d873ce37d49e309bbfb8be5340862b7be8dacb577918410e
|
4
|
+
data.tar.gz: 71016bf84c15fb98c866a1ea395c83310045e9fddd584436b75bb6f5fb9c9568
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f46eb878e55aef173f2835f90defaee50f8b8fc77b804a1cb53a89dad146e028600b3c3af6c7e65172a79d626a4bba82f3e29ac1d75d5cb9a0fc18e6c5f73a3d
|
7
|
+
data.tar.gz: c688d55de8d81c5e60de67294c0cc470f9bc1cbe61621163c6437faa6c0e9d363a66e333ffc4a9797dd559bd524cebc3702013d6597fca0e4b3a8a955a5bc3c2
|
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,317 @@
|
|
1
|
+
# Getting Started with Youtube
|
2
|
+
|
3
|
+
## Getting Started
|
4
|
+
|
5
|
+
### Install the Package
|
6
|
+
|
7
|
+
Install the gem from the command line:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem install My-first-api-matic -v 1.0
|
11
|
+
```
|
12
|
+
|
13
|
+
Or add the gem to your Gemfile and run `bundle`:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'My-first-api-matic', '1.0'
|
17
|
+
```
|
18
|
+
|
19
|
+
For additional gem details, see the [RubyGems page for the My-first-api-matic gem](https://rubygems.org/gems/My-first-api-matic/versions/1.0).
|
20
|
+
|
21
|
+
### Initialize the API Client
|
22
|
+
|
23
|
+
The following parameters are configurable for the API Client:
|
24
|
+
|
25
|
+
| Parameter | Type | Description |
|
26
|
+
| --- | --- | --- |
|
27
|
+
| `timeout` | `Float` | The value to use for connection timeout. <br> **Default: 60** |
|
28
|
+
| `max_retries` | `Integer` | The number of times to retry an endpoint call if it fails. <br> **Default: 0** |
|
29
|
+
| `retry_interval` | `Float` | Pause in seconds between retries. <br> **Default: 1** |
|
30
|
+
| `backoff_factor` | `Float` | The amount to multiply each successive retry's interval amount by in order to provide backoff. <br> **Default: 2** |
|
31
|
+
| `retry_statuses` | `Array` | A list of HTTP statuses to retry. <br> **Default: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524]** |
|
32
|
+
| `retry_methods` | `Array` | A list of HTTP methods to retry. <br> **Default: %i[get put]** |
|
33
|
+
|
34
|
+
The API client can be initialized as follows:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
client = Youtube::Client.new(
|
38
|
+
)
|
39
|
+
```
|
40
|
+
|
41
|
+
### API Errors
|
42
|
+
|
43
|
+
Here is the list of errors that the API might throw.
|
44
|
+
|
45
|
+
| HTTP Status Code | Error Description | Exception Class |
|
46
|
+
| --- | --- | --- |
|
47
|
+
| 401 | dfgdfgdfgdfgdsfg | [`ProblemDetailsException`](#problem-details) |
|
48
|
+
|
49
|
+
## Client Class Documentation
|
50
|
+
|
51
|
+
### Youtube Client
|
52
|
+
|
53
|
+
The gateway for the SDK. This class acts as a factory for the Controllers and also holds the configuration of the SDK.
|
54
|
+
|
55
|
+
### Controllers
|
56
|
+
|
57
|
+
| Name | Description |
|
58
|
+
| --- | --- |
|
59
|
+
| api | Gets APIController |
|
60
|
+
|
61
|
+
## API Reference
|
62
|
+
|
63
|
+
### List of APIs
|
64
|
+
|
65
|
+
* [API](#api)
|
66
|
+
|
67
|
+
### API
|
68
|
+
|
69
|
+
#### Overview
|
70
|
+
|
71
|
+
##### Get instance
|
72
|
+
|
73
|
+
An instance of the `APIController` class can be accessed from the API Client.
|
74
|
+
|
75
|
+
```
|
76
|
+
client_controller = client.client
|
77
|
+
```
|
78
|
+
|
79
|
+
#### Get Forcast
|
80
|
+
|
81
|
+
get list of 5 random forcasts
|
82
|
+
|
83
|
+
:information_source: **Note** This endpoint does not require authentication.
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
def get_forcast(param: 'String.Empty')
|
87
|
+
```
|
88
|
+
|
89
|
+
##### Parameters
|
90
|
+
|
91
|
+
| Parameter | Type | Tags | Description |
|
92
|
+
| --- | --- | --- | --- |
|
93
|
+
| `param` | `String` | Query, Optional | default string parameter<br>**Default**: `'String.Empty'` |
|
94
|
+
|
95
|
+
##### Server
|
96
|
+
|
97
|
+
`Server::DEFAULT`
|
98
|
+
|
99
|
+
##### Response Type
|
100
|
+
|
101
|
+
`Mixed`
|
102
|
+
|
103
|
+
##### Example Usage
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
param = 'my value for error'
|
107
|
+
result = client_controller.get_forcast(param: param)
|
108
|
+
```
|
109
|
+
|
110
|
+
##### Example Response
|
111
|
+
|
112
|
+
```
|
113
|
+
[
|
114
|
+
{
|
115
|
+
"date": "2021-05-27T10:33:06.7728939+05:00",
|
116
|
+
"temperatureC": -7,
|
117
|
+
"temperatureF": 20,
|
118
|
+
"summary": "Scorching"
|
119
|
+
},
|
120
|
+
{
|
121
|
+
"date": "2021-05-28T10:33:06.7729006+05:00",
|
122
|
+
"temperatureC": 6,
|
123
|
+
"temperatureF": 42,
|
124
|
+
"summary": "Bracing"
|
125
|
+
}
|
126
|
+
]
|
127
|
+
```
|
128
|
+
|
129
|
+
##### Errors
|
130
|
+
|
131
|
+
| HTTP Status Code | Error Description | Exception Class |
|
132
|
+
| --- | --- | --- |
|
133
|
+
| 400 | Test error message | [`ProblemDetailsException`](#problem-details) |
|
134
|
+
| 404 | not found | `APIException` |
|
135
|
+
|
136
|
+
## Model Reference
|
137
|
+
|
138
|
+
### Structures
|
139
|
+
|
140
|
+
* [Weather Forecast](#weather-forecast)
|
141
|
+
* [Weather Forcase Json Imported](#weather-forcase-json-imported)
|
142
|
+
* [Field Parameter Validator](#field-parameter-validator)
|
143
|
+
|
144
|
+
#### Weather Forecast
|
145
|
+
|
146
|
+
##### Class Name
|
147
|
+
|
148
|
+
`WeatherForecast`
|
149
|
+
|
150
|
+
##### Fields
|
151
|
+
|
152
|
+
| Name | Type | Tags | Description |
|
153
|
+
| --- | --- | --- | --- |
|
154
|
+
| `date` | `DateTime` | Required | The time when forecast was taken |
|
155
|
+
| `temperature_c` | `Integer` | Required | the value of temperature in Centigrade |
|
156
|
+
| `temperature_f` | `Integer` | Required, Constant | the value of temperature in Fahrenheit |
|
157
|
+
| `summary` | `String` | Required, Constant | - |
|
158
|
+
|
159
|
+
##### Example (as JSON)
|
160
|
+
|
161
|
+
```json
|
162
|
+
{
|
163
|
+
"date": "2021-05-27T10:33:06.7728939+05:00",
|
164
|
+
"temperatureC": -7,
|
165
|
+
"temperatureF": 20,
|
166
|
+
"summary": "Scorching"
|
167
|
+
}
|
168
|
+
```
|
169
|
+
|
170
|
+
#### Weather Forcase Json Imported
|
171
|
+
|
172
|
+
##### Class Name
|
173
|
+
|
174
|
+
`WeatherForcaseJsonImported`
|
175
|
+
|
176
|
+
##### Fields
|
177
|
+
|
178
|
+
| Name | Type | Tags | Description |
|
179
|
+
| --- | --- | --- | --- |
|
180
|
+
| `date` | `String` | Required | - |
|
181
|
+
| `temperature_c` | `Integer` | Required | - |
|
182
|
+
| `temperature_f` | `Integer` | Required | - |
|
183
|
+
| `summary` | `String` | Required | - |
|
184
|
+
|
185
|
+
##### Example (as JSON)
|
186
|
+
|
187
|
+
```json
|
188
|
+
{
|
189
|
+
"date": "2021-05-27T10:33:06.7728939+05:00",
|
190
|
+
"temperatureC": -7,
|
191
|
+
"temperatureF": 20,
|
192
|
+
"summary": "Scorching"
|
193
|
+
}
|
194
|
+
```
|
195
|
+
|
196
|
+
#### Field Parameter Validator
|
197
|
+
|
198
|
+
##### Class Name
|
199
|
+
|
200
|
+
`FieldParameterValidator`
|
201
|
+
|
202
|
+
##### Fields
|
203
|
+
|
204
|
+
| Name | Type | Tags | Description |
|
205
|
+
| --- | --- | --- | --- |
|
206
|
+
| `precision` | `Float` | Required | **Constraints**: `> 0`, `< 600`, *Multiple Of*: `10`, *Total Digits*: `1000`, *Fraction Digits*: `2` |
|
207
|
+
| `string` | `Array<String>` | Optional, Constant | **Constraints**: *Minimum Items*: `5`, *Maximum Items*: `200`, *Minimum Length*: `3`, *Maximum Length*: `10`, *Pattern*: `/^[A-Z]+$/i` |
|
208
|
+
| `unix_format_date_time` | `DateTime` | Required | - |
|
209
|
+
| `object` | `Object` | Required | - |
|
210
|
+
| `int_64` | `Long` | Required | **Constraints**: `> 23`, `< 23`, *Multiple Of*: `3`, *Total Digits*: `23`, *Fraction Digits*: `23`, *Pattern*: `2` |
|
211
|
+
|
212
|
+
##### Example (as JSON)
|
213
|
+
|
214
|
+
```json
|
215
|
+
{
|
216
|
+
"Precision": 229.08,
|
217
|
+
"UnixFormatDateTime": 1480809600,
|
218
|
+
"Object": {
|
219
|
+
"key1": "val1",
|
220
|
+
"key2": "val2"
|
221
|
+
},
|
222
|
+
"Int64": 10
|
223
|
+
}
|
224
|
+
```
|
225
|
+
|
226
|
+
### Enumerations
|
227
|
+
|
228
|
+
* [Test Enum](#test-enum)
|
229
|
+
|
230
|
+
#### Test Enum
|
231
|
+
|
232
|
+
This is the test description of enum00
|
233
|
+
|
234
|
+
##### Class Name
|
235
|
+
|
236
|
+
`TestEnum`
|
237
|
+
|
238
|
+
##### Fields
|
239
|
+
|
240
|
+
| Name | Description |
|
241
|
+
| --- | --- |
|
242
|
+
| `FIRSTFILED` | Test description of first filed |
|
243
|
+
| `SECONDFIELD` | Test description of Second filed |
|
244
|
+
| `THIRD_VALUE` | |
|
245
|
+
|
246
|
+
### Exceptions
|
247
|
+
|
248
|
+
* [Problem Details](#problem-details)
|
249
|
+
|
250
|
+
#### Problem Details
|
251
|
+
|
252
|
+
##### Class Name
|
253
|
+
|
254
|
+
`ProblemDetailsException`
|
255
|
+
|
256
|
+
##### Fields
|
257
|
+
|
258
|
+
| Name | Type | Tags | Description |
|
259
|
+
| --- | --- | --- | --- |
|
260
|
+
| `type` | `String` | Required, Constant | - |
|
261
|
+
| `title` | `String` | Required, Constant | - |
|
262
|
+
| `status` | `Integer` | Required, Constant | - |
|
263
|
+
| `trace_id` | `String` | Required, Constant | - |
|
264
|
+
|
265
|
+
##### Example (as JSON)
|
266
|
+
|
267
|
+
```json
|
268
|
+
{
|
269
|
+
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
|
270
|
+
"title": "Bad Request",
|
271
|
+
"status": 400,
|
272
|
+
"traceId": "00-3dd0a6221d0ccd48ad5eb89a9cf3df71-91763b2e58fa4742-00"
|
273
|
+
}
|
274
|
+
```
|
275
|
+
|
276
|
+
## Utility Classes Documentation
|
277
|
+
|
278
|
+
### ApiHelper Class
|
279
|
+
|
280
|
+
API utility class.
|
281
|
+
|
282
|
+
### Methods
|
283
|
+
|
284
|
+
| Name | Return Type | Description |
|
285
|
+
| --- | --- | --- |
|
286
|
+
| json_deserialize | Hash | Deserializes a JSON string to a Ruby Hash. |
|
287
|
+
| rfc3339 | DateTime | Safely converts a string into an RFC3339 DateTime object. |
|
288
|
+
|
289
|
+
## Common Code Documentation
|
290
|
+
|
291
|
+
### HttpResponse
|
292
|
+
|
293
|
+
Http response received.
|
294
|
+
|
295
|
+
#### Properties
|
296
|
+
|
297
|
+
| Name | Type | Description |
|
298
|
+
| --- | --- | --- |
|
299
|
+
| status_code | Integer | The status code returned by the server. |
|
300
|
+
| reason_phrase | String | The reason phrase returned by the server. |
|
301
|
+
| headers | Hash | Response headers. |
|
302
|
+
| raw_body | String | Response body. |
|
303
|
+
| request | HttpRequest | The request that resulted in this response. |
|
304
|
+
|
305
|
+
### HttpRequest
|
306
|
+
|
307
|
+
Represents a single Http Request.
|
308
|
+
|
309
|
+
#### Properties
|
310
|
+
|
311
|
+
| Name | Type | Tag | Description |
|
312
|
+
| --- | --- | --- | --- |
|
313
|
+
| http_method | HttpMethodEnum | | The HTTP method of the request. |
|
314
|
+
| query_url | String | | The endpoint URL for the API request. |
|
315
|
+
| headers | Hash | Optional | Request headers. |
|
316
|
+
| parameters | Hash | Optional | Request body. |
|
317
|
+
|
data/lib/youtube.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# youtube
|
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 'youtube/api_helper.rb'
|
13
|
+
require_relative 'youtube/client.rb'
|
14
|
+
|
15
|
+
# Utilities
|
16
|
+
require_relative 'youtube/utilities/file_wrapper.rb'
|
17
|
+
|
18
|
+
# Http
|
19
|
+
require_relative 'youtube/http/http_call_back.rb'
|
20
|
+
require_relative 'youtube/http/http_client.rb'
|
21
|
+
require_relative 'youtube/http/faraday_client.rb'
|
22
|
+
require_relative 'youtube/http/http_method_enum.rb'
|
23
|
+
require_relative 'youtube/http/http_request.rb'
|
24
|
+
require_relative 'youtube/http/http_response.rb'
|
25
|
+
|
26
|
+
# Models
|
27
|
+
require_relative 'youtube/models/base_model.rb'
|
28
|
+
require_relative 'youtube/models/weather_forecast.rb'
|
29
|
+
require_relative 'youtube/models/weather_forcase_json_imported.rb'
|
30
|
+
require_relative 'youtube/models/field_parameter_validator.rb'
|
31
|
+
require_relative 'youtube/models/test_enum.rb'
|
32
|
+
|
33
|
+
# Exceptions
|
34
|
+
require_relative 'youtube/exceptions/api_exception.rb'
|
35
|
+
require_relative 'youtube/exceptions/problem_details_exception.rb'
|
36
|
+
|
37
|
+
require_relative 'youtube/configuration.rb'
|
38
|
+
|
39
|
+
# Controllers
|
40
|
+
require_relative 'youtube/controllers/base_controller.rb'
|
41
|
+
require_relative 'youtube/controllers/api_controller.rb'
|
@@ -0,0 +1,289 @@
|
|
1
|
+
# youtube
|
2
|
+
#
|
3
|
+
# This file was automatically generated by APIMATIC v2.0
|
4
|
+
# ( https://apimatic.io ).
|
5
|
+
|
6
|
+
module Youtube
|
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
|