gonebusy-ruby-client 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -68
- data/lib/gonebusy/api_helper.rb +181 -149
- data/lib/gonebusy/configuration.rb +51 -16
- data/lib/gonebusy/controllers/base_controller.rb +46 -45
- data/lib/gonebusy/controllers/bookings_controller.rb +257 -293
- data/lib/gonebusy/controllers/categories_controller.rb +151 -180
- data/lib/gonebusy/controllers/pricing_models_controller.rb +202 -241
- data/lib/gonebusy/controllers/resources_controller.rb +295 -355
- data/lib/gonebusy/controllers/schedules_controller.rb +361 -428
- data/lib/gonebusy/controllers/search_controller.rb +52 -63
- data/lib/gonebusy/controllers/services_controller.rb +310 -367
- data/lib/gonebusy/controllers/users_controller.rb +199 -239
- data/lib/gonebusy/exceptions/api_exception.rb +16 -16
- data/lib/gonebusy/exceptions/entities_error_error_exception.rb +27 -0
- data/lib/gonebusy/exceptions/entities_error_exception.rb +26 -26
- data/lib/gonebusy/gonebusy_client.rb +58 -58
- data/lib/gonebusy/http/auth/custom_auth.rb +15 -15
- data/lib/gonebusy/http/faraday_client.rb +43 -41
- data/lib/gonebusy/http/http_call_back.rb +17 -17
- data/lib/gonebusy/http/http_client.rb +84 -82
- data/lib/gonebusy/http/http_context.rb +15 -15
- data/lib/gonebusy/http/http_method_enum.rb +7 -7
- data/lib/gonebusy/http/http_request.rb +44 -44
- data/lib/gonebusy/http/http_response.rb +19 -19
- data/lib/gonebusy/models/base_model.rb +31 -31
- data/lib/gonebusy/models/cancel_booking_by_id_response.rb +35 -35
- data/lib/gonebusy/models/create_booking_body.rb +134 -80
- data/lib/gonebusy/models/create_booking_response.rb +35 -35
- data/lib/gonebusy/models/create_category_body.rb +71 -71
- data/lib/gonebusy/models/create_category_response.rb +35 -35
- data/lib/gonebusy/models/create_pricing_model_body.rb +80 -80
- data/lib/gonebusy/models/create_pricing_model_response.rb +35 -35
- data/lib/gonebusy/models/create_resource_body.rb +89 -89
- data/lib/gonebusy/models/create_resource_response.rb +35 -35
- data/lib/gonebusy/models/create_schedule_body.rb +143 -144
- data/lib/gonebusy/models/create_schedule_response.rb +35 -35
- data/lib/gonebusy/models/create_schedule_time_window_body.rb +116 -117
- data/lib/gonebusy/models/create_schedule_time_window_response.rb +35 -35
- data/lib/gonebusy/models/create_service_body.rb +107 -107
- data/lib/gonebusy/models/create_service_response.rb +35 -35
- data/lib/gonebusy/models/create_user_body.rb +89 -89
- data/lib/gonebusy/models/create_user_response.rb +35 -35
- data/lib/gonebusy/models/date_recurs_by_enum.rb +12 -12
- data/lib/gonebusy/models/delete_resource_by_id_response.rb +35 -35
- data/lib/gonebusy/models/delete_schedule_by_id_response.rb +35 -35
- data/lib/gonebusy/models/delete_schedule_time_window_by_id_response.rb +35 -35
- data/lib/gonebusy/models/delete_service_by_id_response.rb +35 -35
- data/lib/gonebusy/models/entities_address_entity.rb +116 -116
- data/lib/gonebusy/models/entities_availability_response.rb +58 -58
- data/lib/gonebusy/models/entities_booking_response.rb +71 -71
- data/lib/gonebusy/models/entities_category_response.rb +98 -98
- data/lib/gonebusy/models/entities_pricing_model_response.rb +89 -89
- data/lib/gonebusy/models/entities_resource_availabilities.rb +49 -49
- data/lib/gonebusy/models/entities_resource_response.rb +107 -107
- data/lib/gonebusy/models/entities_schedule_response.rb +76 -76
- data/lib/gonebusy/models/entities_search_response.rb +54 -54
- data/lib/gonebusy/models/entities_service_response.rb +134 -130
- data/lib/gonebusy/models/entities_slots.rb +44 -45
- data/lib/gonebusy/models/entities_thing_type_response.rb +44 -44
- data/lib/gonebusy/models/entities_time_window_response.rb +134 -135
- data/lib/gonebusy/models/entities_user_response.rb +152 -152
- data/lib/gonebusy/models/frequency_enum.rb +17 -17
- data/lib/gonebusy/models/gender_enum.rb +9 -9
- data/lib/gonebusy/models/get_booking_by_id_response.rb +35 -35
- data/lib/gonebusy/models/get_bookings_response.rb +40 -40
- data/lib/gonebusy/models/get_categories_response.rb +40 -40
- data/lib/gonebusy/models/get_category_by_id_response.rb +35 -35
- data/lib/gonebusy/models/get_pricing_model_by_id_response.rb +35 -35
- data/lib/gonebusy/models/get_pricing_models_response.rb +40 -40
- data/lib/gonebusy/models/get_resource_by_id_response.rb +35 -35
- data/lib/gonebusy/models/get_resource_things_response.rb +40 -40
- data/lib/gonebusy/models/get_resources_response.rb +40 -40
- data/lib/gonebusy/models/get_schedule_by_id_response.rb +35 -35
- data/lib/gonebusy/models/get_schedules_response.rb +40 -40
- data/lib/gonebusy/models/get_service_available_slots_by_id_response.rb +35 -35
- data/lib/gonebusy/models/get_service_by_id_response.rb +35 -35
- data/lib/gonebusy/models/get_services_response.rb +40 -40
- data/lib/gonebusy/models/get_user_by_id_response.rb +35 -35
- data/lib/gonebusy/models/get_users_response.rb +40 -40
- data/lib/gonebusy/models/occurrence_enum.rb +21 -21
- data/lib/gonebusy/models/recurs_by_enum.rb +15 -15
- data/lib/gonebusy/models/search_query_response.rb +35 -35
- data/lib/gonebusy/models/update_booking_by_id_body.rb +116 -0
- data/lib/gonebusy/models/update_booking_by_id_response.rb +35 -35
- data/lib/gonebusy/models/update_pricing_model_by_id_body.rb +62 -62
- data/lib/gonebusy/models/update_pricing_model_by_id_response.rb +35 -35
- data/lib/gonebusy/models/update_resource_by_id_body.rb +71 -71
- data/lib/gonebusy/models/update_resource_by_id_response.rb +35 -35
- data/lib/gonebusy/models/update_schedule_time_window_by_id_body.rb +116 -117
- data/lib/gonebusy/models/update_schedule_time_window_by_id_response.rb +35 -35
- data/lib/gonebusy/models/update_service_by_id_body.rb +98 -98
- data/lib/gonebusy/models/update_service_by_id_response.rb +35 -35
- data/lib/gonebusy/models/update_user_by_id_body.rb +89 -89
- data/lib/gonebusy/models/update_user_by_id_response.rb +35 -35
- data/lib/gonebusy.rb +108 -104
- metadata +53 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78b8512a26ebf173b7d5dff019ada61c11f3e537
|
4
|
+
data.tar.gz: ae3379eb6e60d839b67cf3c9e7e84a2797f9b086
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b95d3c6c28de64dd0ebe9b514abf93a5f205ab93421b99c507ec5bd2b8e03b57064a4891085f1d6b3cf5a88ed79f7af0792055b21ff33fc573eb9a5fbf532dcf
|
7
|
+
data.tar.gz: 2d18a850c595ac5593a207ffc100d7bc23beb2f39ac39678a654ddbf833b12e2bb39d842ca36f5a087fefd194376156d56d1abcaeb7a26960ce8060733602669
|
data/README.md
CHANGED
@@ -18,63 +18,18 @@ This client library is a Ruby gem which can be compiled and used in your Ruby an
|
|
18
18
|
2. Run ``` gem build gonebusy.gemspec ``` to build the gem.
|
19
19
|
3. Once built, the gem can be installed on the current work environment using ``` gem install gonebusy-0.0.9.gem ```
|
20
20
|
|
21
|
-
![Building Gem](http://apidocs.io/illustration/ruby?step=buildSDK&workspaceFolder=Gonebusy-Ruby&workspaceName=Gonebusy-Ruby&projectName=gonebusy&gemName=gonebusy&gemVer=0.0.9)
|
22
|
-
|
23
21
|
## How to Use
|
24
22
|
|
25
|
-
|
26
|
-
|
27
|
-
### 1. Starting a new project
|
28
|
-
|
29
|
-
Close any existing projects in RubyMine™ by selecting ``` File -> Close Project ```. Next, click on ``` Create New Project ``` to create a new project from scratch.
|
30
|
-
|
31
|
-
![Create a new project in RubyMine](http://apidocs.io/illustration/ruby?step=createNewProject0&workspaceFolder=Gonebusy-Ruby&workspaceName=Gonebusy&projectName=gonebusy&gemName=gonebusy&gemVer=0.0.9)
|
32
|
-
|
33
|
-
Next, provide ``` TestApp ``` as the project name, choose ``` Rails Application ``` as the project type, and click ``` OK ```.
|
34
|
-
|
35
|
-
![Create a new Rails Application in RubyMine - step 1](http://apidocs.io/illustration/ruby?step=createNewProject1&workspaceFolder=Gonebusy-Ruby&workspaceName=Gonebusy&projectName=gonebusy&gemName=gonebusy&gemVer=0.0.9)
|
36
|
-
|
37
|
-
In the next dialog make sure that correct *Ruby SDK* is being used (minimum 2.0.0) and click ``` OK ```.
|
38
|
-
|
39
|
-
![Create a new Rails Application in RubyMine - step 2](http://apidocs.io/illustration/ruby?step=createNewProject2&workspaceFolder=Gonebusy-Ruby&workspaceName=Gonebusy&projectName=gonebusy&gemName=gonebusy&gemVer=0.0.9)
|
40
|
-
|
41
|
-
This will create a new Rails Application project with an existing set of files and folder.
|
42
|
-
|
43
|
-
### 2. Add reference of the gem
|
44
|
-
|
45
|
-
In order to use the Gonebusy 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 'gonebusy', '~> 0.0.9' ```
|
46
|
-
|
47
|
-
![Add references of the Gemfile](http://apidocs.io/illustration/ruby?step=addReference&workspaceFolder=Gonebusy-Ruby&workspaceName=Gonebusy&projectName=gonebusy&gemName=gonebusy&gemVer=0.0.9)
|
23
|
+
### API Key
|
48
24
|
|
49
|
-
|
25
|
+
If testing with Sandbox, [Signup/Logon](https://sandbox.gonebusy.com/login) at https://sandbox.gonebusy.com/login.
|
50
26
|
|
51
|
-
|
52
|
-
|
53
|
-
![Run Rails Generator on Controllers Folder](http://apidocs.io/illustration/ruby?step=addCode0&workspaceFolder=Gonebusy-Ruby&workspaceName=Gonebusy&projectName=gonebusy&gemName=gonebusy&gemVer=0.0.9)
|
54
|
-
|
55
|
-
Selecting the said option will popup a small window where the generator names are displayed. Here, select the ``` controller ``` template.
|
56
|
-
|
57
|
-
![Create a new Controller](http://apidocs.io/illustration/ruby?step=addCode1&workspaceFolder=Gonebusy-Ruby&workspaceName=Gonebusy&projectName=gonebusy&gemName=gonebusy&gemVer=0.0.9)
|
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
|
-
![Add a new Controller](http://apidocs.io/illustration/ruby?step=addCode2&workspaceFolder=Gonebusy-Ruby&workspaceName=Gonebusy&projectName=gonebusy&gemName=gonebusy&gemVer=0.0.9)
|
62
|
-
|
63
|
-
A new controller class anmed ``` 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
|
-
![Initialize the library](http://apidocs.io/illustration/ruby?step=addCode3&workspaceFolder=Gonebusy-Ruby&workspaceName=Gonebusy&projectName=gonebusy&gemName=gonebusy&gemVer=0.0.9)
|
66
|
-
|
67
|
-
## How to Test
|
68
|
-
|
69
|
-
You can test the generated SDK and the server with automatically generated test
|
70
|
-
cases as follows:
|
27
|
+
If using Production site, [Signup/Logon](https://beta.gonebusy.com/login) at https://beta.gonebusy.com/login.
|
71
28
|
|
72
|
-
|
73
|
-
2. Invoke: `bundle exec rake`
|
29
|
+
Once logged in, navigate to the API Key page and request an API key.
|
74
30
|
|
75
|
-
|
31
|
+
### Initialization/Authentication
|
76
32
|
|
77
|
-
### Authentication
|
78
33
|
In order to setup authentication and initialization of the API client, you need the following information.
|
79
34
|
|
80
35
|
| Parameter | Description |
|
@@ -91,9 +46,13 @@ authorization = "Token <your API key>"; # Set Authorization to "Token <your API
|
|
91
46
|
client = Gonebusy::GonebusyClient.new(authorization)
|
92
47
|
```
|
93
48
|
|
94
|
-
|
49
|
+
## How to Test
|
50
|
+
|
51
|
+
You can test the generated SDK and the server with automatically generated test
|
52
|
+
cases as follows:
|
95
53
|
|
96
|
-
|
54
|
+
1. From terminal/cmd navigate to the root directory of the SDK.
|
55
|
+
2. Invoke: `bundle exec rake`
|
97
56
|
|
98
57
|
## Class Reference
|
99
58
|
|
@@ -225,7 +184,11 @@ result = bookings.get_bookings(collect)
|
|
225
184
|
|
226
185
|
|
227
186
|
```ruby
|
228
|
-
def cancel_booking_by_id(
|
187
|
+
def cancel_booking_by_id(authorization,
|
188
|
+
id,
|
189
|
+
cancel_recurring = nil,
|
190
|
+
date = nil,
|
191
|
+
end_date = nil); end
|
229
192
|
```
|
230
193
|
|
231
194
|
#### Parameters
|
@@ -234,21 +197,21 @@ def cancel_booking_by_id(options = Hash.new); end
|
|
234
197
|
|-----------|------|-------------|
|
235
198
|
| authorization | ``` Required ``` | A valid API key, in the format 'Token API_KEY' |
|
236
199
|
| id | ``` Required ``` | TODO: Add a parameter description |
|
200
|
+
| cancel_recurring | ``` Optional ``` | When a recurring booking, one of: ['instance', 'all', 'infinite'] |
|
201
|
+
| date | ``` Optional ``` | If a recurring booking, the date of an instance to cancel. Several formats are supported: '2014-10-31', 'October 31, 2014' |
|
202
|
+
| end_date | ``` Optional ``` | If recurring, cancel up to :end_date or leave blank for infinite booking. Several formats are supported: '2014-10-31', 'October 31, 2014'. |
|
237
203
|
|
238
204
|
|
239
205
|
#### Example Usage
|
240
206
|
|
241
207
|
```ruby
|
242
|
-
collect = Hash.new
|
243
|
-
|
244
208
|
authorization = 'Authorization'
|
245
|
-
collect['authorization'] = authorization
|
246
|
-
|
247
209
|
id = 'id'
|
248
|
-
|
249
|
-
|
210
|
+
cancel_recurring = 'cancel_recurring'
|
211
|
+
date = DateTime.now
|
212
|
+
end_date = DateTime.now
|
250
213
|
|
251
|
-
result = bookings.cancel_booking_by_id(
|
214
|
+
result = bookings.cancel_booking_by_id(authorization, id, cancel_recurring, date, end_date)
|
252
215
|
|
253
216
|
```
|
254
217
|
|
@@ -270,7 +233,9 @@ result = bookings.cancel_booking_by_id(collect)
|
|
270
233
|
|
271
234
|
|
272
235
|
```ruby
|
273
|
-
def update_booking_by_id(
|
236
|
+
def update_booking_by_id(authorization,
|
237
|
+
id,
|
238
|
+
update_booking_by_id_body = nil); end
|
274
239
|
```
|
275
240
|
|
276
241
|
#### Parameters
|
@@ -279,21 +244,17 @@ def update_booking_by_id(options = Hash.new); end
|
|
279
244
|
|-----------|------|-------------|
|
280
245
|
| authorization | ``` Required ``` | A valid API key, in the format 'Token API_KEY' |
|
281
246
|
| id | ``` Required ``` | TODO: Add a parameter description |
|
247
|
+
| update_booking_by_id_body | ``` Optional ``` | the content of the request |
|
282
248
|
|
283
249
|
|
284
250
|
#### Example Usage
|
285
251
|
|
286
252
|
```ruby
|
287
|
-
collect = Hash.new
|
288
|
-
|
289
253
|
authorization = 'Authorization'
|
290
|
-
collect['authorization'] = authorization
|
291
|
-
|
292
254
|
id = 'id'
|
293
|
-
|
294
|
-
|
255
|
+
update_booking_by_id_body = UpdateBookingByIdBody.new
|
295
256
|
|
296
|
-
result = bookings.update_booking_by_id(
|
257
|
+
result = bookings.update_booking_by_id(authorization, id, update_booking_by_id_body)
|
297
258
|
|
298
259
|
```
|
299
260
|
|
data/lib/gonebusy/api_helper.rb
CHANGED
@@ -1,149 +1,181 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
#
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
return
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
#
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
1
|
+
# This file was automatically generated by APIMATIC v2.0 ( https://apimatic.io ).
|
2
|
+
|
3
|
+
module Gonebusy
|
4
|
+
class APIHelper
|
5
|
+
# Serializes an array parameter (creates key value pairs)
|
6
|
+
# @param [String] The name of the parameter
|
7
|
+
# @param [Array] The value of the parameter
|
8
|
+
# @param [String] The format of the serialization
|
9
|
+
def self.serialize_array(key, array, formatting: 'indexed')
|
10
|
+
tuples = []
|
11
|
+
|
12
|
+
if formatting == 'unindexed'
|
13
|
+
tuples += array.map { |element| ["#{key}[]", element] }
|
14
|
+
elsif formatting == 'indexed'
|
15
|
+
tuples += array.map.with_index { |element, index| ["#{key}[#{index}]", element] }
|
16
|
+
elsif formatting == 'plain'
|
17
|
+
tuples += array.map { |element| [key, element] }
|
18
|
+
else
|
19
|
+
raise ArgumentError, 'Invalid format provided.'
|
20
|
+
end
|
21
|
+
tuples
|
22
|
+
end
|
23
|
+
|
24
|
+
# Replaces template parameters in the given url
|
25
|
+
# @param [String] The query string builder to replace the template parameters
|
26
|
+
# @param [Hash] The parameters to replace in the url
|
27
|
+
def self.append_url_with_template_parameters(query_builder, parameters)
|
28
|
+
# perform parameter validation
|
29
|
+
raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.instance_of? String
|
30
|
+
|
31
|
+
# return if there are no parameters to replace
|
32
|
+
return query_builder if parameters.nil?
|
33
|
+
|
34
|
+
# iterate and append parameters
|
35
|
+
parameters.each do |key, value|
|
36
|
+
replace_value = ''
|
37
|
+
|
38
|
+
if value.nil?
|
39
|
+
replace_value = ''
|
40
|
+
elsif value.instance_of? Array
|
41
|
+
value.map! { |element| CGI.escape(element.to_s) }
|
42
|
+
replace_value = value.join('/')
|
43
|
+
else
|
44
|
+
replace_value = CGI.escape(value.to_s)
|
45
|
+
end
|
46
|
+
|
47
|
+
# find the template parameter and replace it with its value
|
48
|
+
query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value)
|
49
|
+
end
|
50
|
+
query_builder
|
51
|
+
end
|
52
|
+
|
53
|
+
# Appends the given set of parameters to the given query string
|
54
|
+
# @param [String] The query string builder to add the query parameters to
|
55
|
+
# @param [Hash] The parameters to append
|
56
|
+
# @param [String] The format of array parameter serialization
|
57
|
+
def self.append_url_with_query_parameters(query_builder, parameters, array_serialization: 'indexed')
|
58
|
+
# perform parameter validation
|
59
|
+
raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.instance_of? String
|
60
|
+
|
61
|
+
# return if there are no parameters to replace
|
62
|
+
return query_builder if parameters.nil?
|
63
|
+
|
64
|
+
parameters.each do |key, value|
|
65
|
+
seperator = (query_builder.include? '?') ? '&' : '?'
|
66
|
+
if not value.nil?
|
67
|
+
if value.instance_of? Array
|
68
|
+
value.compact!
|
69
|
+
if array_serialization == 'csv'
|
70
|
+
query_builder += "#{seperator}#{key}=#{value.map { |element| CGI.escape(element.to_s) }.join(',')}"
|
71
|
+
elsif array_serialization == 'psv'
|
72
|
+
query_builder += "#{seperator}#{key}=#{value.map { |element| CGI.escape(element.to_s) }.join('|')}"
|
73
|
+
elsif array_serialization == 'tsv'
|
74
|
+
query_builder += "#{seperator}#{key}=#{value.map { |element| CGI.escape(element.to_s) }.join('\t')}"
|
75
|
+
else
|
76
|
+
query_builder += "#{seperator}#{APIHelper.serialize_array(key, value, formatting: array_serialization).
|
77
|
+
map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')}"
|
78
|
+
end
|
79
|
+
else
|
80
|
+
query_builder += "#{seperator}#{key}=#{CGI.escape(value.to_s)}"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
query_builder
|
85
|
+
end
|
86
|
+
|
87
|
+
# Validates and processes the given Url
|
88
|
+
# @param [String] The given Url to process
|
89
|
+
# @return [String] Pre-processed Url as string
|
90
|
+
def self.clean_url(url)
|
91
|
+
# perform parameter validation
|
92
|
+
raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
|
93
|
+
|
94
|
+
# ensure that the urls are absolute
|
95
|
+
matches = url.match(%r{^(https?:\/\/[^\/]+)})
|
96
|
+
raise ArgumentError, 'Invalid Url format.' if matches.nil?
|
97
|
+
|
98
|
+
# get the http protocol match
|
99
|
+
protocol = matches[1]
|
100
|
+
|
101
|
+
# check if parameters exist
|
102
|
+
index = url.index('?')
|
103
|
+
|
104
|
+
# remove redundant forward slashes
|
105
|
+
query = url[protocol.length...(!index.nil? ? index : url.length)]
|
106
|
+
query.gsub!(%r{\/\/+}, '/')
|
107
|
+
|
108
|
+
# get the parameters
|
109
|
+
parameters = !index.nil? ? url[url.index('?')...url.length] : ''
|
110
|
+
|
111
|
+
# return processed url
|
112
|
+
protocol + query + parameters
|
113
|
+
end
|
114
|
+
|
115
|
+
# Parses JSON string.
|
116
|
+
# @param [String] A JSON string.
|
117
|
+
def self.json_deserialize(json)
|
118
|
+
return JSON.parse(json)
|
119
|
+
rescue
|
120
|
+
raise TypeError, 'Server responded with invalid JSON.'
|
121
|
+
end
|
122
|
+
|
123
|
+
# Removes elements with empty values from a hash.
|
124
|
+
# @param [Hash] The hash to clean.
|
125
|
+
def self.clean_hash(hash)
|
126
|
+
hash.delete_if { |_key, value| value.to_s.strip.empty? }
|
127
|
+
end
|
128
|
+
|
129
|
+
# Form encodes a hash of parameters.
|
130
|
+
# @param [Hash] The hash of parameters to encode.
|
131
|
+
# @return [Hash] A hash with the same parameters form encoded.
|
132
|
+
def self.form_encode_parameters(form_parameters)
|
133
|
+
encoded = {}
|
134
|
+
form_parameters.each do |key, value|
|
135
|
+
encoded.merge!(APIHelper.form_encode(value, key))
|
136
|
+
end
|
137
|
+
encoded
|
138
|
+
end
|
139
|
+
|
140
|
+
# Form encodes an object.
|
141
|
+
# @param [Dynamic] An object to form encode.
|
142
|
+
# @param [String] The name of the object.
|
143
|
+
# @return [Hash] A form encoded representation of the object in the form of a hash.
|
144
|
+
def self.form_encode(obj, instance_name)
|
145
|
+
retval = {}
|
146
|
+
|
147
|
+
# If this is a structure, resolve it's field names.
|
148
|
+
obj = obj.to_hash if obj.is_a? BaseModel
|
149
|
+
|
150
|
+
# Create a form encoded hash for this object.
|
151
|
+
if obj.nil?
|
152
|
+
nil
|
153
|
+
elsif obj.instance_of? Array
|
154
|
+
obj.each_with_index do |value, index|
|
155
|
+
retval.merge!(APIHelper.form_encode(value, instance_name + '[' + index.to_s + ']'))
|
156
|
+
end
|
157
|
+
elsif obj.instance_of? Hash
|
158
|
+
obj.each do |key, value|
|
159
|
+
retval.merge!(APIHelper.form_encode(value, instance_name + '[' + key + ']'))
|
160
|
+
end
|
161
|
+
else
|
162
|
+
retval[instance_name] = obj
|
163
|
+
end
|
164
|
+
retval
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
# extend types to support to_bool
|
170
|
+
module ToBoolean
|
171
|
+
def to_bool
|
172
|
+
return true if self == true || self.to_s.strip =~ /^(true|yes|y|1)$/i
|
173
|
+
return false
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
class NilClass; include ToBoolean; end
|
178
|
+
class TrueClass; include ToBoolean; end
|
179
|
+
class FalseClass; include ToBoolean; end
|
180
|
+
class Numeric; include ToBoolean; end
|
181
|
+
class String; include ToBoolean; end
|