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.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +29 -68
  3. data/lib/gonebusy/api_helper.rb +181 -149
  4. data/lib/gonebusy/configuration.rb +51 -16
  5. data/lib/gonebusy/controllers/base_controller.rb +46 -45
  6. data/lib/gonebusy/controllers/bookings_controller.rb +257 -293
  7. data/lib/gonebusy/controllers/categories_controller.rb +151 -180
  8. data/lib/gonebusy/controllers/pricing_models_controller.rb +202 -241
  9. data/lib/gonebusy/controllers/resources_controller.rb +295 -355
  10. data/lib/gonebusy/controllers/schedules_controller.rb +361 -428
  11. data/lib/gonebusy/controllers/search_controller.rb +52 -63
  12. data/lib/gonebusy/controllers/services_controller.rb +310 -367
  13. data/lib/gonebusy/controllers/users_controller.rb +199 -239
  14. data/lib/gonebusy/exceptions/api_exception.rb +16 -16
  15. data/lib/gonebusy/exceptions/entities_error_error_exception.rb +27 -0
  16. data/lib/gonebusy/exceptions/entities_error_exception.rb +26 -26
  17. data/lib/gonebusy/gonebusy_client.rb +58 -58
  18. data/lib/gonebusy/http/auth/custom_auth.rb +15 -15
  19. data/lib/gonebusy/http/faraday_client.rb +43 -41
  20. data/lib/gonebusy/http/http_call_back.rb +17 -17
  21. data/lib/gonebusy/http/http_client.rb +84 -82
  22. data/lib/gonebusy/http/http_context.rb +15 -15
  23. data/lib/gonebusy/http/http_method_enum.rb +7 -7
  24. data/lib/gonebusy/http/http_request.rb +44 -44
  25. data/lib/gonebusy/http/http_response.rb +19 -19
  26. data/lib/gonebusy/models/base_model.rb +31 -31
  27. data/lib/gonebusy/models/cancel_booking_by_id_response.rb +35 -35
  28. data/lib/gonebusy/models/create_booking_body.rb +134 -80
  29. data/lib/gonebusy/models/create_booking_response.rb +35 -35
  30. data/lib/gonebusy/models/create_category_body.rb +71 -71
  31. data/lib/gonebusy/models/create_category_response.rb +35 -35
  32. data/lib/gonebusy/models/create_pricing_model_body.rb +80 -80
  33. data/lib/gonebusy/models/create_pricing_model_response.rb +35 -35
  34. data/lib/gonebusy/models/create_resource_body.rb +89 -89
  35. data/lib/gonebusy/models/create_resource_response.rb +35 -35
  36. data/lib/gonebusy/models/create_schedule_body.rb +143 -144
  37. data/lib/gonebusy/models/create_schedule_response.rb +35 -35
  38. data/lib/gonebusy/models/create_schedule_time_window_body.rb +116 -117
  39. data/lib/gonebusy/models/create_schedule_time_window_response.rb +35 -35
  40. data/lib/gonebusy/models/create_service_body.rb +107 -107
  41. data/lib/gonebusy/models/create_service_response.rb +35 -35
  42. data/lib/gonebusy/models/create_user_body.rb +89 -89
  43. data/lib/gonebusy/models/create_user_response.rb +35 -35
  44. data/lib/gonebusy/models/date_recurs_by_enum.rb +12 -12
  45. data/lib/gonebusy/models/delete_resource_by_id_response.rb +35 -35
  46. data/lib/gonebusy/models/delete_schedule_by_id_response.rb +35 -35
  47. data/lib/gonebusy/models/delete_schedule_time_window_by_id_response.rb +35 -35
  48. data/lib/gonebusy/models/delete_service_by_id_response.rb +35 -35
  49. data/lib/gonebusy/models/entities_address_entity.rb +116 -116
  50. data/lib/gonebusy/models/entities_availability_response.rb +58 -58
  51. data/lib/gonebusy/models/entities_booking_response.rb +71 -71
  52. data/lib/gonebusy/models/entities_category_response.rb +98 -98
  53. data/lib/gonebusy/models/entities_pricing_model_response.rb +89 -89
  54. data/lib/gonebusy/models/entities_resource_availabilities.rb +49 -49
  55. data/lib/gonebusy/models/entities_resource_response.rb +107 -107
  56. data/lib/gonebusy/models/entities_schedule_response.rb +76 -76
  57. data/lib/gonebusy/models/entities_search_response.rb +54 -54
  58. data/lib/gonebusy/models/entities_service_response.rb +134 -130
  59. data/lib/gonebusy/models/entities_slots.rb +44 -45
  60. data/lib/gonebusy/models/entities_thing_type_response.rb +44 -44
  61. data/lib/gonebusy/models/entities_time_window_response.rb +134 -135
  62. data/lib/gonebusy/models/entities_user_response.rb +152 -152
  63. data/lib/gonebusy/models/frequency_enum.rb +17 -17
  64. data/lib/gonebusy/models/gender_enum.rb +9 -9
  65. data/lib/gonebusy/models/get_booking_by_id_response.rb +35 -35
  66. data/lib/gonebusy/models/get_bookings_response.rb +40 -40
  67. data/lib/gonebusy/models/get_categories_response.rb +40 -40
  68. data/lib/gonebusy/models/get_category_by_id_response.rb +35 -35
  69. data/lib/gonebusy/models/get_pricing_model_by_id_response.rb +35 -35
  70. data/lib/gonebusy/models/get_pricing_models_response.rb +40 -40
  71. data/lib/gonebusy/models/get_resource_by_id_response.rb +35 -35
  72. data/lib/gonebusy/models/get_resource_things_response.rb +40 -40
  73. data/lib/gonebusy/models/get_resources_response.rb +40 -40
  74. data/lib/gonebusy/models/get_schedule_by_id_response.rb +35 -35
  75. data/lib/gonebusy/models/get_schedules_response.rb +40 -40
  76. data/lib/gonebusy/models/get_service_available_slots_by_id_response.rb +35 -35
  77. data/lib/gonebusy/models/get_service_by_id_response.rb +35 -35
  78. data/lib/gonebusy/models/get_services_response.rb +40 -40
  79. data/lib/gonebusy/models/get_user_by_id_response.rb +35 -35
  80. data/lib/gonebusy/models/get_users_response.rb +40 -40
  81. data/lib/gonebusy/models/occurrence_enum.rb +21 -21
  82. data/lib/gonebusy/models/recurs_by_enum.rb +15 -15
  83. data/lib/gonebusy/models/search_query_response.rb +35 -35
  84. data/lib/gonebusy/models/update_booking_by_id_body.rb +116 -0
  85. data/lib/gonebusy/models/update_booking_by_id_response.rb +35 -35
  86. data/lib/gonebusy/models/update_pricing_model_by_id_body.rb +62 -62
  87. data/lib/gonebusy/models/update_pricing_model_by_id_response.rb +35 -35
  88. data/lib/gonebusy/models/update_resource_by_id_body.rb +71 -71
  89. data/lib/gonebusy/models/update_resource_by_id_response.rb +35 -35
  90. data/lib/gonebusy/models/update_schedule_time_window_by_id_body.rb +116 -117
  91. data/lib/gonebusy/models/update_schedule_time_window_by_id_response.rb +35 -35
  92. data/lib/gonebusy/models/update_service_by_id_body.rb +98 -98
  93. data/lib/gonebusy/models/update_service_by_id_response.rb +35 -35
  94. data/lib/gonebusy/models/update_user_by_id_body.rb +89 -89
  95. data/lib/gonebusy/models/update_user_by_id_response.rb +35 -35
  96. data/lib/gonebusy.rb +108 -104
  97. metadata +53 -17
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bb2f3bc9e6c616526afbc1c62a58aaf6316e59fd
4
- data.tar.gz: 47bf5dcb379793dc565542c87d476f23618b36e1
3
+ metadata.gz: 78b8512a26ebf173b7d5dff019ada61c11f3e537
4
+ data.tar.gz: ae3379eb6e60d839b67cf3c9e7e84a2797f9b086
5
5
  SHA512:
6
- metadata.gz: cf29cda26c000d730e1a36b061db07e26738b289d9fdf67699cd468a8b7de8174ecc72186c3578198e50b9ca473faa06ef22c8661fe129ad837ea70d92207b8c
7
- data.tar.gz: f3b441c1df95d8779078ee0f3306a3e38a0a4f15ff3f8164538f37be9603b4356a012996f7cd64e56d22b86cb228e2a366bb432a561afe46103be14caebab599
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
- The following section explains how to use the Gonebusy 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.
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
- ### 3. Adding a new Rails Controller
25
+ If testing with Sandbox, [Signup/Logon](https://sandbox.gonebusy.com/login) at https://sandbox.gonebusy.com/login.
50
26
 
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
- ![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
- 1. From terminal/cmd navigate to the root directory of the SDK.
73
- 2. Invoke: `bundle exec rake`
29
+ Once logged in, navigate to the API Key page and request an API key.
74
30
 
75
- ## Initialization
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
- The added initlization code can be debugged by putting a breakpoint in the ``` Index ``` method and running the project in debug mode by selecting ``` Run -> Debug 'Development: TestApp' ```.
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
- ![Debug the TestApp](http://apidocs.io/illustration/ruby?step=addCode4&workspaceFolder=GoneBusy%2011-17-16-Ruby&workspaceName=Gonebusy&projectName=gonebusy&gemName=gonebusy&gemVer=0.0.9&initLine=client%2520%253D%2520GonebusyClient.new%2528%2527authorization%2527%2529)
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(options = Hash.new); end
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
- collect['id'] = id
249
-
210
+ cancel_recurring = 'cancel_recurring'
211
+ date = DateTime.now
212
+ end_date = DateTime.now
250
213
 
251
- result = bookings.cancel_booking_by_id(collect)
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(options = Hash.new); end
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
- collect['id'] = id
294
-
255
+ update_booking_by_id_body = UpdateBookingByIdBody.new
295
256
 
296
- result = bookings.update_booking_by_id(collect)
257
+ result = bookings.update_booking_by_id(authorization, id, update_booking_by_id_body)
297
258
 
298
259
  ```
299
260
 
@@ -1,149 +1,181 @@
1
- module Gonebusy
2
- class APIHelper
3
- # Replaces template parameters in the given url
4
- # @param [String] The query string builder to replace the template parameters
5
- # @param [Hash] The parameters to replace in the url
6
- def self.append_url_with_template_parameters(query_builder, parameters)
7
- # perform parameter validation
8
- raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.instance_of? String
9
-
10
- # return if there are no parameters to replace
11
- if parameters.nil?
12
- query_builder
13
- else
14
- # iterate and append parameters
15
- parameters.each do |key, value|
16
- replace_value = ''
17
-
18
- if value.nil?
19
- replace_value = ''
20
- elsif value.instance_of? Array
21
- value.map!{|element| CGI.escape(element.to_s)}
22
- replace_value = value.join('/')
23
- else
24
- replace_value = CGI.escape(value.to_s)
25
- end
26
-
27
- # find the template parameter and replace it with its value
28
- query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value)
29
- end
30
- end
31
- return query_builder
32
- end
33
-
34
- # Appends the given set of parameters to the given query string
35
- # @param [String] The query string builder to replace the template parameters
36
- # @param [Hash] The parameters to append
37
- def self.append_url_with_query_parameters(query_builder, parameters)
38
- # perform parameter validation
39
- raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.instance_of? String
40
-
41
- # return if there are no parameters to replace
42
- if parameters.nil?
43
- return query_builder
44
- else
45
- # remove any nil values
46
- parameters = parameters.reject { |_key, value| value.nil? }
47
-
48
- # does the query string already has parameters
49
- has_params = query_builder.include? '?'
50
- separator = has_params ? '&' : '?'
51
-
52
- # append query with separator and parameters and return
53
- return query_builder << separator << URI.encode_www_form(parameters)
54
- end
55
- end
56
-
57
- # Validates and processes the given Url
58
- # @param [String] The given Url to process
59
- # @return [String] Pre-processed Url as string
60
- def self.clean_url(url)
61
- # perform parameter validation
62
- raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
63
-
64
- # ensure that the urls are absolute
65
- matches = url.match(%r{^(https?:\/\/[^\/]+)})
66
- raise ArgumentError, 'Invalid Url format.' if matches.nil?
67
-
68
- # get the http protocol match
69
- protocol = matches[1]
70
-
71
- # check if parameters exist
72
- index = url.index('?')
73
-
74
- # remove redundant forward slashes
75
- query = url[protocol.length...(index != nil ? index : url.length)]
76
- query.gsub!(%r{\/\/+}, '/')
77
-
78
- # get the parameters
79
- parameters = index != nil ? url[url.index('?')...url.length] : ""
80
-
81
- # return processed url
82
- return protocol + query + parameters
83
- end
84
-
85
- # Parses JSON string.
86
- # @param [String] A JSON string.
87
- def self.json_deserialize(json)
88
- begin
89
- return JSON.parse(json)
90
- rescue
91
- raise TypeError, "Server responded with invalid JSON."
92
- end
93
- end
94
-
95
- # Form encodes a hash of parameters.
96
- # @param [Hash] The hash of parameters to encode.
97
- # @return [Hash] A hash with the same parameters form encoded.
98
- def self.form_encode_parameters(form_parameters)
99
- encoded = Hash.new
100
- form_parameters.each do |key, value|
101
- encoded.merge!(APIHelper.form_encode value, key)
102
- end
103
- return encoded
104
- end
105
-
106
- # Form encodes an object.
107
- # @param [Dynamic] An object to form encode.
108
- # @param [String] The name of the object.
109
- # @return [Hash] A form encoded representation of the object in the form of a hash.
110
- def self.form_encode(obj, instance_name)
111
- retval = Hash.new
112
-
113
- # If this is a structure, resolve it's field names.
114
- if obj.kind_of? BaseModel
115
- obj = obj.to_hash
116
- end
117
-
118
- # Create a form encoded hash for this object.
119
- if obj == nil
120
- nil
121
- elsif obj.instance_of? Array
122
- obj.each_with_index do |value, index|
123
- retval.merge!(APIHelper.form_encode(value, instance_name + "[" + index.to_s + "]"))
124
- end
125
- elsif obj.instance_of? Hash
126
- obj.each do |key, value|
127
- retval.merge!(APIHelper.form_encode(value, instance_name + "[" + key + "]"))
128
- end
129
- else
130
- retval[instance_name] = obj
131
- end
132
- return retval
133
- end
134
- end
135
- end
136
-
137
- # extend types to support to_bool
138
- module ToBoolean
139
- def to_bool
140
- return true if self == true || self.to_s.strip =~ /^(true|yes|y|1)$/i
141
- return false
142
- end
143
- end
144
-
145
- class NilClass; include ToBoolean; end
146
- class TrueClass; include ToBoolean; end
147
- class FalseClass; include ToBoolean; end
148
- class Numeric; include ToBoolean; end
149
- class String; include ToBoolean; end
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