bandwidth-sdk 0.1.0 → 1.0.0.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE +27 -27
  3. data/README.md +115 -1
  4. data/lib/bandwidth.rb +39 -37
  5. data/lib/bandwidth/api_helper.rb +280 -280
  6. data/lib/bandwidth/client.rb +41 -32
  7. data/lib/bandwidth/configuration.rb +130 -111
  8. data/lib/bandwidth/exceptions/api_exception.rb +20 -20
  9. data/lib/bandwidth/http/api_response.rb +36 -36
  10. data/lib/bandwidth/http/auth/messaging_basic_auth.rb +22 -0
  11. data/lib/bandwidth/http/auth/voice_basic_auth.rb +22 -22
  12. data/lib/bandwidth/http/faraday_client.rb +64 -64
  13. data/lib/bandwidth/http/http_call_back.rb +24 -24
  14. data/lib/bandwidth/http/http_client.rb +104 -104
  15. data/lib/bandwidth/http/http_method_enum.rb +13 -13
  16. data/lib/bandwidth/http/http_request.rb +50 -50
  17. data/lib/bandwidth/http/http_response.rb +29 -29
  18. data/lib/bandwidth/messaging_lib/messaging.rb +23 -0
  19. data/lib/bandwidth/messaging_lib/messaging/client.rb +40 -0
  20. data/lib/bandwidth/messaging_lib/messaging/controllers/api_controller.rb +406 -0
  21. data/lib/bandwidth/messaging_lib/messaging/controllers/base_controller.rb +49 -0
  22. data/lib/bandwidth/messaging_lib/messaging/exceptions/generic_client_exception.rb +46 -0
  23. data/lib/bandwidth/messaging_lib/messaging/exceptions/path_client_exception.rb +49 -0
  24. data/lib/bandwidth/messaging_lib/messaging/models/bandwidth_callback_message.rb +80 -0
  25. data/lib/bandwidth/messaging_lib/messaging/models/bandwidth_message.rb +125 -0
  26. data/lib/bandwidth/messaging_lib/messaging/models/deferred_result.rb +44 -0
  27. data/lib/bandwidth/messaging_lib/messaging/models/field_error.rb +44 -0
  28. data/lib/bandwidth/messaging_lib/messaging/models/media.rb +123 -0
  29. data/lib/bandwidth/messaging_lib/messaging/models/message_request.rb +80 -0
  30. data/lib/bandwidth/messaging_lib/messaging/models/tag.rb +44 -0
  31. data/lib/bandwidth/models/base_model.rb +36 -36
  32. data/lib/bandwidth/voice_lib/bxml/bxml.rb +40 -0
  33. data/lib/bandwidth/voice_lib/bxml/verbs/forward.rb +19 -0
  34. data/lib/bandwidth/voice_lib/bxml/verbs/gather.rb +38 -0
  35. data/lib/bandwidth/voice_lib/bxml/verbs/hangup.rb +15 -0
  36. data/lib/bandwidth/voice_lib/bxml/verbs/pause.rb +16 -0
  37. data/lib/bandwidth/voice_lib/bxml/verbs/phone_number.rb +19 -0
  38. data/lib/bandwidth/voice_lib/bxml/verbs/play_audio.rb +18 -0
  39. data/lib/bandwidth/voice_lib/bxml/verbs/redirect.rb +21 -0
  40. data/lib/bandwidth/voice_lib/bxml/verbs/send_dtmf.rb +14 -0
  41. data/lib/bandwidth/voice_lib/bxml/verbs/speak_sentence.rb +19 -0
  42. data/lib/bandwidth/voice_lib/bxml/verbs/transfer.rb +38 -0
  43. data/lib/bandwidth/voice_lib/bxml/verbs/xml_verb.rb +27 -0
  44. data/lib/bandwidth/voice_lib/voice.rb +27 -23
  45. data/lib/bandwidth/voice_lib/voice/client.rb +40 -36
  46. data/lib/bandwidth/voice_lib/voice/controllers/api_controller.rb +184 -0
  47. data/lib/bandwidth/voice_lib/voice/controllers/base_controller.rb +49 -49
  48. data/lib/bandwidth/voice_lib/voice/exceptions/bandwidth_exception.rb +51 -0
  49. data/lib/bandwidth/voice_lib/voice/exceptions/bandwidth_not_found_exception.rb +49 -0
  50. data/lib/bandwidth/voice_lib/voice/exceptions/bandwidth_rate_limit_error_exception.rb +56 -0
  51. data/lib/bandwidth/voice_lib/voice/models/answer_method_enum.rb +35 -35
  52. data/lib/bandwidth/voice_lib/voice/models/api_call_response.rb +116 -116
  53. data/lib/bandwidth/voice_lib/voice/models/api_create_call_request.rb +125 -125
  54. data/lib/bandwidth/voice_lib/voice/models/api_modify_call_request.rb +80 -80
  55. data/lib/bandwidth/voice_lib/voice/models/bandwidth_callback_message_voice.rb +98 -98
  56. data/lib/bandwidth/voice_lib/voice/models/disconnect_method_enum.rb +35 -35
  57. data/lib/bandwidth/voice_lib/voice/models/field_error.rb +44 -0
  58. data/lib/bandwidth/voice_lib/voice/models/redirect_method_enum.rb +35 -35
  59. data/lib/bandwidth/voice_lib/voice/models/state_enum.rb +17 -17
  60. metadata +36 -47
  61. data/lib/bandwidth/voice_lib/voice/bxml/bxml.rb +0 -36
  62. data/lib/bandwidth/voice_lib/voice/bxml/verbs/forward.rb +0 -19
  63. data/lib/bandwidth/voice_lib/voice/bxml/verbs/gather.rb +0 -37
  64. data/lib/bandwidth/voice_lib/voice/bxml/verbs/hangup.rb +0 -14
  65. data/lib/bandwidth/voice_lib/voice/bxml/verbs/pause.rb +0 -15
  66. data/lib/bandwidth/voice_lib/voice/bxml/verbs/phone_number.rb +0 -18
  67. data/lib/bandwidth/voice_lib/voice/bxml/verbs/play_audio.rb +0 -17
  68. data/lib/bandwidth/voice_lib/voice/bxml/verbs/redirect.rb +0 -20
  69. data/lib/bandwidth/voice_lib/voice/bxml/verbs/send_dtmf.rb +0 -12
  70. data/lib/bandwidth/voice_lib/voice/bxml/verbs/speak_sentence.rb +0 -18
  71. data/lib/bandwidth/voice_lib/voice/bxml/verbs/transfer.rb +0 -37
  72. data/lib/bandwidth/voice_lib/voice/bxml/verbs/xml_verb.rb +0 -26
  73. data/lib/bandwidth/voice_lib/voice/controllers/calls_controller.rb +0 -88
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: dda3a64e6345a2b4b57ff3e2ef4925b1aa325dd6
4
- data.tar.gz: 579af4d2e8973ebf0577bcb86be0a1cea0186dcc
2
+ SHA256:
3
+ metadata.gz: 063ad65c1d2bbb442368e32d3e3319dc96328830fdf11af877781e00cbd6b142
4
+ data.tar.gz: 7e70e9ae684568b796cadfc7b14a9fc401a267e2115f0d4b2897b14e6a6d4919
5
5
  SHA512:
6
- metadata.gz: ec54ff2a4c0da9ca4e0da7125ef1ea4eb5ba480eccbb2bdfb53ae5336a18ddb177d1a8e4416f2f4db01fafc257fa9b2205dd797f2c6f8e77148b021416b2d906
7
- data.tar.gz: da2b2fed5ae62674e1b6cfdd47df80157f599d61c7abfb139e07b1bce1f38944c15b5d296e42aa390113461fff47d3f91e2de2e663807eea6afcfa051ad27102
6
+ metadata.gz: b1748d7bf50f4f165cb3ac2fac2e8048443041788e4eae189a563adfccfc92fb065b9acf1b47f8797206e8e8ff60d0a457c703a31cd8be17432845a42fb3c846
7
+ data.tar.gz: 52264df03e925edcc36d94d252c48d8f402b4455ddb4b89e1e954c6e70be3388b9ef28f5dee6dcab898802296d9908c981909ace7dfecbee63ae4ada67f8bbea
data/LICENSE CHANGED
@@ -1,28 +1,28 @@
1
- License:
2
- ========
3
- The MIT License (MIT)
4
- http://opensource.org/licenses/MIT
5
-
6
- Copyright (c) 2014 - 2016 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
- ==========
1
+ License:
2
+ ========
3
+ The MIT License (MIT)
4
+ http://opensource.org/licenses/MIT
5
+
6
+ Copyright (c) 2014 - 2016 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
28
  APIMATIC is a trade mark for APIMATIC Limited
data/README.md CHANGED
@@ -1 +1,115 @@
1
- # Bandwidth's Ruby SDK
1
+ # Getting Started with bandwidth
2
+
3
+ Bandwidth's set of APIs
4
+
5
+
6
+ ## Building
7
+
8
+ The generated code depends on a few Ruby gems. The references to these gems are added in the gemspec file. The easiest way to resolve the dependencies, build the gem and install it is through Rake:
9
+
10
+ 1. Install Rake if not already installed: `gem install rake`
11
+ 2. Install Bundler if not already installed: `gem install bundler`
12
+ 3. From terminal/cmd navigate to the root directory of the SDK.
13
+ 4. Invoke: `rake install`
14
+
15
+ Alternatively, you can build and install the gem manually:
16
+
17
+ 1. From terminal/cmd navigate to the root directory of the SDK.
18
+ 2. Run the build command: `gem build bandwidth.gemspec`
19
+ 3. Run the install command: `gem install bandwidth-1.0.0.pre.gem`
20
+
21
+ ![Installing Gem](https://apidocs.io/illustration/ruby?workspaceFolder=Bandwidth&gemVer=1.0.0.pre&gemName=bandwidth&step=buildSDK)
22
+
23
+ ## Installation
24
+
25
+ The following section explains how to use the bandwidth 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 - Step 1](https://apidocs.io/illustration/ruby?workspaceFolder=Bandwidth&step=createNewProject0)
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 2](https://apidocs.io/illustration/ruby?workspaceFolder=Bandwidth&step=createNewProject1)
36
+
37
+ In the next dialog make sure that the correct Ruby SDK is being used (minimum 2.0.0) and click `OK`.
38
+
39
+ ![Create a new Rails Application in RubyMine - Step 3](https://apidocs.io/illustration/ruby?workspaceFolder=Bandwidth&step=createNewProject2)
40
+
41
+ ### 2. Add reference of the gem
42
+
43
+ In order to use the Tester 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 'bandwidth', '~> 1.0.0.pre'`
44
+
45
+ ![Add new reference to the Gemfile](https://apidocs.io/illustration/ruby?workspaceFolder=Bandwidth&gemVer=1.0.0.pre&gemName=bandwidth&step=addReference)
46
+
47
+ ### 3. Adding a new Rails Controller
48
+
49
+ 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...`.
50
+
51
+ ![Run Rails Generator on Controllers Folder](https://apidocs.io/illustration/ruby?workspaceFolder=Bandwidth&gemVer=1.0.0.pre&gemName=bandwidth&step=addCode0)
52
+
53
+ Selecting the said option will popup a small window where the generator names are displayed. Here, select the `controller` template.
54
+
55
+ ![Create a new Controller](https://apidocs.io/illustration/ruby?workspaceFolder=Bandwidth&step=addCode1)
56
+
57
+ 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`.
58
+
59
+ ![Add a new Controller](https://apidocs.io/illustration/ruby?workspaceFolder=Bandwidth&gemVer=1.0.0.pre&gemName=bandwidth&step=addCode2)
60
+
61
+ A new controller class named `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.
62
+
63
+ ![Initialize the library](https://apidocs.io/illustration/ruby?workspaceFolder=Bandwidth&gemName=bandwidth&step=addCode3)
64
+
65
+ ## Usage
66
+
67
+ To initialize the API client, the following parameters need to be passed.
68
+
69
+ | Parameter | Type | Description |
70
+ | --- | --- | --- |
71
+ | `messaging_basic_auth_user_name` | `String` | The username to use with basic authentication |
72
+ | `messaging_basic_auth_password` | `String` | The password to use with basic authentication |
73
+ | `voice_basic_auth_user_name` | `String` | The username to use with basic authentication |
74
+ | `voice_basic_auth_password` | `String` | The password to use with basic authentication |
75
+ | `timeout` | `Float` | The value to use for connection timeout |
76
+ | `max_retries` | `Integer` | The number of times to retry an endpoint call if it fails |
77
+ | `retry_interval` | `Float` | Pause in seconds between retries |
78
+ | `backoff_factor` | `Float` | The amount to multiply each successive retry's interval amount by in order to provide backoff |
79
+
80
+ The API client can be initialized using a Configuration object as following.
81
+
82
+ ```ruby
83
+ client = Bandwidth::Client.new(
84
+ messaging_basic_auth_user_name: 'MessagingBasicAuthUserName',
85
+ messaging_basic_auth_password: 'MessagingBasicAuthPassword',
86
+ voice_basic_auth_user_name: 'VoiceBasicAuthUserName',
87
+ voice_basic_auth_password: 'VoiceBasicAuthPassword',
88
+ environment: Environment::PRODUCTION,
89
+ )
90
+ ```
91
+
92
+ API calls return an `ApiResponse` object that includes the following fields:
93
+
94
+ | Field | Description |
95
+ | --- | --- |
96
+ | `status_code` | Status code of the HTTP response |
97
+ | `reason_phrase` | Reason phrase of the HTTP response |
98
+ | `headers` | Headers of the HTTP response as a Hash |
99
+ | `raw_body` | The body of the HTTP response as a String |
100
+ | `request` | HTTP request info |
101
+ | `errors` | Errors, if they exist |
102
+ | `data` | The deserialized body of the HTTP response |
103
+
104
+
105
+ ## Authorization
106
+
107
+ This API does not require authentication.
108
+
109
+
110
+
111
+ ## API Reference
112
+
113
+ ### List of APIs
114
+
115
+ *
data/lib/bandwidth.rb CHANGED
@@ -1,37 +1,39 @@
1
- # bandwidth
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 'bandwidth/api_helper.rb'
13
- require_relative 'bandwidth/client.rb'
14
-
15
- # Http
16
- require_relative 'bandwidth/http/api_response.rb'
17
- require_relative 'bandwidth/http/http_call_back.rb'
18
- require_relative 'bandwidth/http/http_client.rb'
19
- require_relative 'bandwidth/http/faraday_client.rb'
20
- require_relative 'bandwidth/http/http_method_enum.rb'
21
- require_relative 'bandwidth/http/http_request.rb'
22
- require_relative 'bandwidth/http/http_response.rb'
23
-
24
- # Models
25
- require_relative 'bandwidth/models/base_model.rb'
26
-
27
- # Exceptions
28
- require_relative 'bandwidth/exceptions/api_exception.rb'
29
-
30
- require_relative 'bandwidth/configuration.rb'
31
-
32
- # Namespaces
33
- require_relative 'bandwidth/voice_lib/voice'
34
- require_relative 'bandwidth/http/auth/voice_basic_auth.rb'
35
-
36
-
37
- # Controllers
1
+ # bandwidth
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 'bandwidth/api_helper.rb'
13
+ require_relative 'bandwidth/client.rb'
14
+
15
+ # Http
16
+ require_relative 'bandwidth/http/api_response.rb'
17
+ require_relative 'bandwidth/http/http_call_back.rb'
18
+ require_relative 'bandwidth/http/http_client.rb'
19
+ require_relative 'bandwidth/http/faraday_client.rb'
20
+ require_relative 'bandwidth/http/http_method_enum.rb'
21
+ require_relative 'bandwidth/http/http_request.rb'
22
+ require_relative 'bandwidth/http/http_response.rb'
23
+
24
+ # Models
25
+ require_relative 'bandwidth/models/base_model.rb'
26
+
27
+ # Exceptions
28
+ require_relative 'bandwidth/exceptions/api_exception.rb'
29
+
30
+ require_relative 'bandwidth/configuration.rb'
31
+
32
+ # Namespaces
33
+ require_relative 'bandwidth/messaging_lib/messaging'
34
+ require_relative 'bandwidth/http/auth/messaging_basic_auth.rb'
35
+ require_relative 'bandwidth/voice_lib/voice'
36
+ require_relative 'bandwidth/http/auth/voice_basic_auth.rb'
37
+
38
+
39
+ # Controllers
@@ -1,280 +1,280 @@
1
- # bandwidth
2
- #
3
- # This file was automatically generated by APIMATIC v2.0
4
- # ( https://apimatic.io ).
5
-
6
- module Bandwidth
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
- # Iterate and append parameters.
45
- parameters.each do |key, value|
46
- replace_value = ''
47
-
48
- if value.nil?
49
- replace_value = ''
50
- elsif value.instance_of? Array
51
- value.map! { |element| CGI.escape(element.to_s) }
52
- replace_value = value.join('/')
53
- else
54
- replace_value = CGI.escape(value.to_s)
55
- end
56
-
57
- # Find the template parameter and replace it with its value.
58
- query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value)
59
- end
60
- query_builder
61
- end
62
-
63
- # Appends the given set of parameters to the given query string.
64
- # @param [String] The query string builder to add the query parameters to.
65
- # @param [Hash] The parameters to append.
66
- # @param [String] The format of array parameter serialization.
67
- def self.append_url_with_query_parameters(query_builder, parameters,
68
- array_serialization: 'indexed')
69
- # Perform parameter validation.
70
- unless query_builder.instance_of? String
71
- raise ArgumentError, 'Given value for parameter \"query_builder\"
72
- is invalid.'
73
- end
74
-
75
- # Return if there are no parameters to replace.
76
- return query_builder if parameters.nil?
77
-
78
- parameters.each do |key, value|
79
- seperator = query_builder.include?('?') ? '&' : '?'
80
- unless value.nil?
81
- if value.instance_of? Array
82
- value.compact!
83
- query_builder += if array_serialization == 'csv'
84
- "#{seperator}#{key}=#{value.map do |element|
85
- CGI.escape(element.to_s)
86
- end.join(',')}"
87
- elsif array_serialization == 'psv'
88
- "#{seperator}#{key}=#{value.map do |element|
89
- CGI.escape(element.to_s)
90
- end.join('|')}"
91
- elsif array_serialization == 'tsv'
92
- "#{seperator}#{key}=#{value.map do |element|
93
- CGI.escape(element.to_s)
94
- end.join("\t")}"
95
- else
96
- "#{seperator}#{APIHelper.serialize_array(
97
- key, value, formatting: array_serialization
98
- ).map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }
99
- .join('&')}"
100
- end
101
- else
102
- query_builder += "#{seperator}#{key}=#{CGI.escape(value.to_s)}"
103
- end
104
- end
105
- end
106
- query_builder
107
- end
108
-
109
- # Validates and processes the given Url.
110
- # @param [String] The given Url to process.
111
- # @return [String] Pre-processed Url as string.
112
- def self.clean_url(url)
113
- # Perform parameter validation.
114
- raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
115
-
116
- # Ensure that the urls are absolute.
117
- matches = url.match(%r{^(https?:\/\/[^\/]+)})
118
- raise ArgumentError, 'Invalid Url format.' if matches.nil?
119
-
120
- # Get the http protocol match.
121
- protocol = matches[1]
122
-
123
- # Check if parameters exist.
124
- index = url.index('?')
125
-
126
- # Remove redundant forward slashes.
127
- query = url[protocol.length...(!index.nil? ? index : url.length)]
128
- query.gsub!(%r{\/\/+}, '/')
129
-
130
- # Get the parameters.
131
- parameters = !index.nil? ? url[url.index('?')...url.length] : ''
132
-
133
- # Return processed url.
134
- protocol + query + parameters
135
- end
136
-
137
- # Parses JSON string.
138
- # @param [String] A JSON string.
139
- def self.json_deserialize(json)
140
- return JSON.parse(json)
141
- rescue StandardError
142
- raise TypeError, 'Server responded with invalid JSON.'
143
- end
144
-
145
- # Removes elements with empty values from a hash.
146
- # @param [Hash] The hash to clean.
147
- def self.clean_hash(hash)
148
- hash.delete_if { |_key, value| value.to_s.strip.empty? }
149
- end
150
-
151
- # Form encodes a hash of parameters.
152
- # @param [Hash] The hash of parameters to encode.
153
- # @return [Hash] A hash with the same parameters form encoded.
154
- def self.form_encode_parameters(form_parameters,
155
- array_serialization: 'indexed')
156
- encoded = {}
157
- form_parameters.each do |key, value|
158
- encoded.merge!(APIHelper.form_encode(value, key, formatting:
159
- array_serialization))
160
- end
161
- encoded
162
- end
163
-
164
- def self.custom_merge(a, b)
165
- x = {}
166
- a.each do |key, value_a|
167
- b.each do |k, value_b|
168
- next unless key == k
169
- x[k] = []
170
- if value_a.instance_of? Array
171
- value_a.each do |v|
172
- x[k].push(v)
173
- end
174
- else
175
- x[k].push(value_a)
176
- end
177
- if value_b.instance_of? Array
178
- value_b.each do |v|
179
- x[k].push(v)
180
- end
181
- else
182
- x[k].push(value_b)
183
- end
184
- a.delete(k)
185
- b.delete(k)
186
- end
187
- end
188
- x.merge!(a)
189
- x.merge!(b)
190
- x
191
- end
192
-
193
- # Form encodes an object.
194
- # @param [Dynamic] An object to form encode.
195
- # @param [String] The name of the object.
196
- # @return [Hash] A form encoded representation of the object in the form
197
- # of a hash.
198
- def self.form_encode(obj, instance_name, formatting: 'indexed')
199
- retval = {}
200
-
201
- serializable_types = [String, Numeric, TrueClass,
202
- FalseClass, Date, DateTime]
203
-
204
- # If this is a structure, resolve it's field names.
205
- obj = obj.to_hash if obj.is_a? BaseModel
206
-
207
- # Create a form encoded hash for this object.
208
- if obj.nil?
209
- nil
210
- elsif obj.instance_of? Array
211
- if formatting == 'indexed'
212
- obj.each_with_index do |value, index|
213
- retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
214
- index.to_s + ']'))
215
- end
216
- elsif serializable_types.map { |x| obj[0].is_a? x }.any?
217
- obj.each do |value|
218
- abc = if formatting == 'unindexed'
219
- APIHelper.form_encode(value, instance_name + '[]',
220
- formatting: formatting)
221
- else
222
- APIHelper.form_encode(value, instance_name,
223
- formatting: formatting)
224
- end
225
- retval = APIHelper.custom_merge(retval, abc)
226
- end
227
- else
228
- obj.each_with_index do |value, index|
229
- retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
230
- index.to_s + ']', formatting: formatting))
231
- end
232
- end
233
- elsif obj.instance_of? Hash
234
- obj.each do |key, value|
235
- retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
236
- key.to_s + ']', formatting: formatting))
237
- end
238
- else
239
- retval[instance_name] = obj
240
- end
241
- retval
242
- end
243
-
244
- # Retrieves a field from a Hash/Array based on an Array of keys/indexes
245
- # @param [Hash, Array] The hash to extract data from
246
- # @param [Array<String, Integer>] The keys/indexes to use
247
- # @return [Object] The extracted value
248
- def self.map_response(obj, keys)
249
- val = obj
250
- begin
251
- keys.each do |key|
252
- val = if val.is_a? Array
253
- if key.to_i.to_s == key
254
- val[key.to_i]
255
- else
256
- val = nil
257
- end
258
- else
259
- val.fetch(key.to_sym)
260
- end
261
- end
262
- rescue NoMethodError, TypeError, IndexError
263
- val = nil
264
- end
265
- val
266
- end
267
-
268
- # Safely converts a string into an rfc3339 DateTime object
269
- # @param [String] The datetime string
270
- # @return [DateTime] A DateTime object of rfc3339 format
271
- def self.rfc3339(date_time)
272
- # missing timezone information
273
- if date_time.end_with?('Z') || date_time.index('+')
274
- DateTime.rfc3339(date_time)
275
- else
276
- DateTime.rfc3339(date_time + 'Z')
277
- end
278
- end
279
- end
280
- end
1
+ # bandwidth
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module Bandwidth
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
+ # Iterate and append parameters.
45
+ parameters.each do |key, value|
46
+ replace_value = ''
47
+
48
+ if value.nil?
49
+ replace_value = ''
50
+ elsif value.instance_of? Array
51
+ value.map! { |element| CGI.escape(element.to_s) }
52
+ replace_value = value.join('/')
53
+ else
54
+ replace_value = CGI.escape(value.to_s)
55
+ end
56
+
57
+ # Find the template parameter and replace it with its value.
58
+ query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value)
59
+ end
60
+ query_builder
61
+ end
62
+
63
+ # Appends the given set of parameters to the given query string.
64
+ # @param [String] The query string builder to add the query parameters to.
65
+ # @param [Hash] The parameters to append.
66
+ # @param [String] The format of array parameter serialization.
67
+ def self.append_url_with_query_parameters(query_builder, parameters,
68
+ array_serialization: 'indexed')
69
+ # Perform parameter validation.
70
+ unless query_builder.instance_of? String
71
+ raise ArgumentError, 'Given value for parameter \"query_builder\"
72
+ is invalid.'
73
+ end
74
+
75
+ # Return if there are no parameters to replace.
76
+ return query_builder if parameters.nil?
77
+
78
+ parameters.each do |key, value|
79
+ seperator = query_builder.include?('?') ? '&' : '?'
80
+ unless value.nil?
81
+ if value.instance_of? Array
82
+ value.compact!
83
+ query_builder += if array_serialization == 'csv'
84
+ "#{seperator}#{key}=#{value.map do |element|
85
+ CGI.escape(element.to_s)
86
+ end.join(',')}"
87
+ elsif array_serialization == 'psv'
88
+ "#{seperator}#{key}=#{value.map do |element|
89
+ CGI.escape(element.to_s)
90
+ end.join('|')}"
91
+ elsif array_serialization == 'tsv'
92
+ "#{seperator}#{key}=#{value.map do |element|
93
+ CGI.escape(element.to_s)
94
+ end.join("\t")}"
95
+ else
96
+ "#{seperator}#{APIHelper.serialize_array(
97
+ key, value, formatting: array_serialization
98
+ ).map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }
99
+ .join('&')}"
100
+ end
101
+ else
102
+ query_builder += "#{seperator}#{key}=#{CGI.escape(value.to_s)}"
103
+ end
104
+ end
105
+ end
106
+ query_builder
107
+ end
108
+
109
+ # Validates and processes the given Url.
110
+ # @param [String] The given Url to process.
111
+ # @return [String] Pre-processed Url as string.
112
+ def self.clean_url(url)
113
+ # Perform parameter validation.
114
+ raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
115
+
116
+ # Ensure that the urls are absolute.
117
+ matches = url.match(%r{^(https?:\/\/[^\/]+)})
118
+ raise ArgumentError, 'Invalid Url format.' if matches.nil?
119
+
120
+ # Get the http protocol match.
121
+ protocol = matches[1]
122
+
123
+ # Check if parameters exist.
124
+ index = url.index('?')
125
+
126
+ # Remove redundant forward slashes.
127
+ query = url[protocol.length...(!index.nil? ? index : url.length)]
128
+ query.gsub!(%r{\/\/+}, '/')
129
+
130
+ # Get the parameters.
131
+ parameters = !index.nil? ? url[url.index('?')...url.length] : ''
132
+
133
+ # Return processed url.
134
+ protocol + query + parameters
135
+ end
136
+
137
+ # Parses JSON string.
138
+ # @param [String] A JSON string.
139
+ def self.json_deserialize(json)
140
+ return JSON.parse(json)
141
+ rescue StandardError
142
+ raise TypeError, 'Server responded with invalid JSON.'
143
+ end
144
+
145
+ # Removes elements with empty values from a hash.
146
+ # @param [Hash] The hash to clean.
147
+ def self.clean_hash(hash)
148
+ hash.delete_if { |_key, value| value.to_s.strip.empty? }
149
+ end
150
+
151
+ # Form encodes a hash of parameters.
152
+ # @param [Hash] The hash of parameters to encode.
153
+ # @return [Hash] A hash with the same parameters form encoded.
154
+ def self.form_encode_parameters(form_parameters,
155
+ array_serialization: 'indexed')
156
+ encoded = {}
157
+ form_parameters.each do |key, value|
158
+ encoded.merge!(APIHelper.form_encode(value, key, formatting:
159
+ array_serialization))
160
+ end
161
+ encoded
162
+ end
163
+
164
+ def self.custom_merge(a, b)
165
+ x = {}
166
+ a.each do |key, value_a|
167
+ b.each do |k, value_b|
168
+ next unless key == k
169
+ x[k] = []
170
+ if value_a.instance_of? Array
171
+ value_a.each do |v|
172
+ x[k].push(v)
173
+ end
174
+ else
175
+ x[k].push(value_a)
176
+ end
177
+ if value_b.instance_of? Array
178
+ value_b.each do |v|
179
+ x[k].push(v)
180
+ end
181
+ else
182
+ x[k].push(value_b)
183
+ end
184
+ a.delete(k)
185
+ b.delete(k)
186
+ end
187
+ end
188
+ x.merge!(a)
189
+ x.merge!(b)
190
+ x
191
+ end
192
+
193
+ # Form encodes an object.
194
+ # @param [Dynamic] An object to form encode.
195
+ # @param [String] The name of the object.
196
+ # @return [Hash] A form encoded representation of the object in the form
197
+ # of a hash.
198
+ def self.form_encode(obj, instance_name, formatting: 'indexed')
199
+ retval = {}
200
+
201
+ serializable_types = [String, Numeric, TrueClass,
202
+ FalseClass, Date, DateTime]
203
+
204
+ # If this is a structure, resolve it's field names.
205
+ obj = obj.to_hash if obj.is_a? BaseModel
206
+
207
+ # Create a form encoded hash for this object.
208
+ if obj.nil?
209
+ nil
210
+ elsif obj.instance_of? Array
211
+ if formatting == 'indexed'
212
+ obj.each_with_index do |value, index|
213
+ retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
214
+ index.to_s + ']'))
215
+ end
216
+ elsif serializable_types.map { |x| obj[0].is_a? x }.any?
217
+ obj.each do |value|
218
+ abc = if formatting == 'unindexed'
219
+ APIHelper.form_encode(value, instance_name + '[]',
220
+ formatting: formatting)
221
+ else
222
+ APIHelper.form_encode(value, instance_name,
223
+ formatting: formatting)
224
+ end
225
+ retval = APIHelper.custom_merge(retval, abc)
226
+ end
227
+ else
228
+ obj.each_with_index do |value, index|
229
+ retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
230
+ index.to_s + ']', formatting: formatting))
231
+ end
232
+ end
233
+ elsif obj.instance_of? Hash
234
+ obj.each do |key, value|
235
+ retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
236
+ key.to_s + ']', formatting: formatting))
237
+ end
238
+ else
239
+ retval[instance_name] = obj
240
+ end
241
+ retval
242
+ end
243
+
244
+ # Retrieves a field from a Hash/Array based on an Array of keys/indexes
245
+ # @param [Hash, Array] The hash to extract data from
246
+ # @param [Array<String, Integer>] The keys/indexes to use
247
+ # @return [Object] The extracted value
248
+ def self.map_response(obj, keys)
249
+ val = obj
250
+ begin
251
+ keys.each do |key|
252
+ val = if val.is_a? Array
253
+ if key.to_i.to_s == key
254
+ val[key.to_i]
255
+ else
256
+ val = nil
257
+ end
258
+ else
259
+ val.fetch(key.to_sym)
260
+ end
261
+ end
262
+ rescue NoMethodError, TypeError, IndexError
263
+ val = nil
264
+ end
265
+ val
266
+ end
267
+
268
+ # Safely converts a string into an rfc3339 DateTime object
269
+ # @param [String] The datetime string
270
+ # @return [DateTime] A DateTime object of rfc3339 format
271
+ def self.rfc3339(date_time)
272
+ # missing timezone information
273
+ if date_time.end_with?('Z') || date_time.index('+')
274
+ DateTime.rfc3339(date_time)
275
+ else
276
+ DateTime.rfc3339(date_time + 'Z')
277
+ end
278
+ end
279
+ end
280
+ end