messagemedia_lookups_sdk 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +201 -0
- data/README.md +42 -0
- data/lib/message_media_lookups.rb +32 -0
- data/lib/message_media_lookups/api_helper.rb +259 -0
- data/lib/message_media_lookups/configuration.rb +35 -0
- data/lib/message_media_lookups/controllers/base_controller.rb +63 -0
- data/lib/message_media_lookups/controllers/lookups_controller.rb +121 -0
- data/lib/message_media_lookups/exceptions/api_exception.rb +16 -0
- data/lib/message_media_lookups/http/auth/basic_auth.rb +21 -0
- data/lib/message_media_lookups/http/auth/hmac_auth.rb +52 -0
- data/lib/message_media_lookups/http/faraday_client.rb +54 -0
- data/lib/message_media_lookups/http/http_call_back.rb +20 -0
- data/lib/message_media_lookups/http/http_client.rb +90 -0
- data/lib/message_media_lookups/http/http_context.rb +16 -0
- data/lib/message_media_lookups/http/http_method_enum.rb +9 -0
- data/lib/message_media_lookups/http/http_request.rb +46 -0
- data/lib/message_media_lookups/http/http_response.rb +19 -0
- data/lib/message_media_lookups/message_media_lookups_client.rb +28 -0
- data/lib/message_media_lookups/models/base_model.rb +32 -0
- data/lib/message_media_lookups/models/lookup_a_phone_number_response.rb +58 -0
- data/test/controllers/controller_test_base.rb +31 -0
- data/test/controllers/test_lookups_controller.rb +58 -0
- data/test/http_response_catcher.rb +12 -0
- data/test/test_helper.rb +90 -0
- metadata +158 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 954fac8750860e9ede7c813fea25cfc3455be9d0
|
4
|
+
data.tar.gz: 1e0e24d4d9bc1eb88bb5e41e3c40480a32f3c71c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8b20a90b64529e58f8165dd73b40ad1a558ef3834e939d76379b6605f8e0f67e9e5477800719d7873589b9da4dde67829a53deaaeca170bc7e5c86a75aea30b3
|
7
|
+
data.tar.gz: 9246ef950421e9356b331a98b347fcd4b67baa469d7cc6720bdae406629682285831b32a899ba399743b60cbf1bbaa18e8e737f0e2678287f0357d6f11906c4c
|
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright 2018 MessageMedia
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# MessageMedia Lookups Ruby SDK
|
2
|
+
[![Build Status](https://travis-ci.org/messagemedia/lookups-ruby-sdk.svg?branch=master)](https://travis-ci.org/messagemedia/lookups-ruby-sdk)
|
3
|
+
[![gem](https://img.shields.io/badge/gem-v1.1.0-red.svg)](https://rubygems.org/gems/messagemedia_lookups)
|
4
|
+
|
5
|
+
The MessageMedia Lookups API provides a number of endpoints for validating the phone numbers you’re sending to by checking their validity, type and carrier records.
|
6
|
+
|
7
|
+
## ⭐️ Installing via RubyGems
|
8
|
+
* Run the following command to install the SDK via RubyGems:
|
9
|
+
```gem install messagemedia_lookups```
|
10
|
+
|
11
|
+
## 🎬 Get Started
|
12
|
+
It's easy to get started. Simply enter the API Key and secret you obtained from the [MessageMedia Developers Portal](https://developers.messagemedia.com) into the code snippet below and a mobile number you wish to send to.
|
13
|
+
|
14
|
+
### 👀 Lookup a number
|
15
|
+
```ruby
|
16
|
+
require 'message_media_lookups'
|
17
|
+
|
18
|
+
# Configuration parameters and credentials
|
19
|
+
auth_user_name = 'YOUR_API_KEY' # The username to use with basic/HMAC authentication
|
20
|
+
auth_password = 'YOUR_SECRET_KEY' # The password to use with basic/HMAC authentication
|
21
|
+
use_hmac = false # Change this to true if you are using HMAC keys
|
22
|
+
|
23
|
+
client = MessageMediaLookups::MessageMediaLookupsClient.new(
|
24
|
+
auth_user_name: auth_user_name,
|
25
|
+
auth_password: auth_password,
|
26
|
+
use_hmac: use_hmac
|
27
|
+
)
|
28
|
+
|
29
|
+
lookups_controller = client.lookups
|
30
|
+
|
31
|
+
phone_number = 'YOUR_MOBILE_NUMBER'
|
32
|
+
options = 'carrier,type'
|
33
|
+
|
34
|
+
result = lookups_controller.get_lookup_a_phone_number(phone_number, options)
|
35
|
+
print result.inspect
|
36
|
+
```
|
37
|
+
|
38
|
+
## 📕 Documentation
|
39
|
+
The Ruby SDK Documentation can be viewed [here](DOCUMENTATION.md)
|
40
|
+
|
41
|
+
## 😕 Need help?
|
42
|
+
Please contact developer support at developers@messagemedia.com or check out the developer portal at [developers.messagemedia.com](https://developers.messagemedia.com/)
|
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
require 'date'
|
3
|
+
require 'json'
|
4
|
+
require 'faraday'
|
5
|
+
require 'certifi'
|
6
|
+
require 'logging'
|
7
|
+
|
8
|
+
require_relative 'message_media_lookups/api_helper.rb'
|
9
|
+
require_relative 'message_media_lookups/message_media_lookups_client.rb'
|
10
|
+
|
11
|
+
# Http
|
12
|
+
require_relative 'message_media_lookups/http/http_call_back.rb'
|
13
|
+
require_relative 'message_media_lookups/http/http_client.rb'
|
14
|
+
require_relative 'message_media_lookups/http/http_method_enum.rb'
|
15
|
+
require_relative 'message_media_lookups/http/http_request.rb'
|
16
|
+
require_relative 'message_media_lookups/http/http_response.rb'
|
17
|
+
require_relative 'message_media_lookups/http/http_context.rb'
|
18
|
+
require_relative 'message_media_lookups/http/faraday_client.rb'
|
19
|
+
require_relative 'message_media_lookups/http/auth/basic_auth.rb'
|
20
|
+
|
21
|
+
# Models
|
22
|
+
require_relative 'message_media_lookups/models/base_model.rb'
|
23
|
+
require_relative 'message_media_lookups/models/lookup_a_phone_number_response.rb'
|
24
|
+
|
25
|
+
# Exceptions
|
26
|
+
require_relative 'message_media_lookups/exceptions/api_exception.rb'
|
27
|
+
|
28
|
+
require_relative 'message_media_lookups/configuration.rb'
|
29
|
+
|
30
|
+
# Controllers
|
31
|
+
require_relative 'message_media_lookups/controllers/base_controller.rb'
|
32
|
+
require_relative 'message_media_lookups/controllers/lookups_controller.rb'
|
@@ -0,0 +1,259 @@
|
|
1
|
+
|
2
|
+
module MessageMediaLookups
|
3
|
+
# API utility class
|
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 do |element, index|
|
16
|
+
["#{key}[#{index}]", element]
|
17
|
+
end
|
18
|
+
elsif formatting == 'plain'
|
19
|
+
tuples += array.map { |element| [key, element] }
|
20
|
+
else
|
21
|
+
raise ArgumentError, 'Invalid format provided.'
|
22
|
+
end
|
23
|
+
tuples
|
24
|
+
end
|
25
|
+
|
26
|
+
# Replaces template parameters in the given url.
|
27
|
+
# @param [String] The query string builder to replace the template
|
28
|
+
# parameters.
|
29
|
+
# @param [Hash] The parameters to replace in the url.
|
30
|
+
def self.append_url_with_template_parameters(query_builder, parameters)
|
31
|
+
# perform parameter validation
|
32
|
+
unless query_builder.instance_of? String
|
33
|
+
raise ArgumentError, 'Given value for parameter \"query_builder\" is
|
34
|
+
invalid.'
|
35
|
+
end
|
36
|
+
|
37
|
+
# Return if there are no parameters to replace.
|
38
|
+
return query_builder if parameters.nil?
|
39
|
+
|
40
|
+
# Iterate and append parameters.
|
41
|
+
parameters.each do |key, value|
|
42
|
+
replace_value = ''
|
43
|
+
|
44
|
+
if value.nil?
|
45
|
+
replace_value = ''
|
46
|
+
elsif value.instance_of? Array
|
47
|
+
value.map! { |element| CGI.escape(element.to_s) }
|
48
|
+
replace_value = value.join('/')
|
49
|
+
else
|
50
|
+
replace_value = CGI.escape(value.to_s)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Find the template parameter and replace it with its value.
|
54
|
+
query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value)
|
55
|
+
end
|
56
|
+
query_builder
|
57
|
+
end
|
58
|
+
|
59
|
+
# Appends the given set of parameters to the given query string.
|
60
|
+
# @param [String] The query string builder to add the query parameters to.
|
61
|
+
# @param [Hash] The parameters to append.
|
62
|
+
# @param [String] The format of array parameter serialization.
|
63
|
+
def self.append_url_with_query_parameters(query_builder, parameters,
|
64
|
+
array_serialization: 'indexed')
|
65
|
+
# Perform parameter validation.
|
66
|
+
unless query_builder.instance_of? String
|
67
|
+
raise ArgumentError, 'Given value for parameter \"query_builder\"
|
68
|
+
is invalid.'
|
69
|
+
end
|
70
|
+
|
71
|
+
# Return if there are no parameters to replace.
|
72
|
+
return query_builder if parameters.nil?
|
73
|
+
|
74
|
+
parameters.each do |key, value|
|
75
|
+
seperator = query_builder.include?('?') ? '&' : '?'
|
76
|
+
unless value.nil?
|
77
|
+
if value.instance_of? Array
|
78
|
+
value.compact!
|
79
|
+
query_builder += if array_serialization == 'csv'
|
80
|
+
"#{seperator}#{key}=#{value.map do |element|
|
81
|
+
CGI.escape(element.to_s)
|
82
|
+
end.join(',')}"
|
83
|
+
elsif array_serialization == 'psv'
|
84
|
+
"#{seperator}#{key}=#{value.map do |element|
|
85
|
+
CGI.escape(element.to_s)
|
86
|
+
end.join('|')}"
|
87
|
+
elsif array_serialization == 'tsv'
|
88
|
+
"#{seperator}#{key}=#{value.map do |element|
|
89
|
+
CGI.escape(element.to_s)
|
90
|
+
end.join("\t")}"
|
91
|
+
else
|
92
|
+
"#{seperator}#{APIHelper.serialize_array(
|
93
|
+
key, value, formatting: array_serialization
|
94
|
+
).map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }
|
95
|
+
.join('&')}"
|
96
|
+
end
|
97
|
+
else
|
98
|
+
query_builder += "#{seperator}#{key}=#{CGI.escape(value.to_s)}"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
query_builder
|
103
|
+
end
|
104
|
+
|
105
|
+
# Validates and processes the given Url.
|
106
|
+
# @param [String] The given Url to process.
|
107
|
+
# @return [String] Pre-processed Url as string.
|
108
|
+
def self.clean_url(url)
|
109
|
+
# Perform parameter validation.
|
110
|
+
raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
|
111
|
+
|
112
|
+
# Ensure that the urls are absolute.
|
113
|
+
matches = url.match(%r{^(https?:\/\/[^\/]+)})
|
114
|
+
raise ArgumentError, 'Invalid Url format.' if matches.nil?
|
115
|
+
|
116
|
+
# Get the http protocol match.
|
117
|
+
protocol = matches[1]
|
118
|
+
|
119
|
+
# Check if parameters exist.
|
120
|
+
index = url.index('?')
|
121
|
+
|
122
|
+
# Remove redundant forward slashes.
|
123
|
+
query = url[protocol.length...(!index.nil? ? index : url.length)]
|
124
|
+
query.gsub!(%r{\/\/+}, '/')
|
125
|
+
|
126
|
+
# Get the parameters.
|
127
|
+
parameters = !index.nil? ? url[url.index('?')...url.length] : ''
|
128
|
+
|
129
|
+
# Return processed url.
|
130
|
+
protocol + query + parameters
|
131
|
+
end
|
132
|
+
|
133
|
+
# Parses JSON string.
|
134
|
+
# @param [String] A JSON string.
|
135
|
+
def self.json_deserialize(json)
|
136
|
+
return JSON.parse(json)
|
137
|
+
rescue StandardError
|
138
|
+
raise TypeError, 'Server responded with invalid JSON.'
|
139
|
+
end
|
140
|
+
|
141
|
+
# Removes elements with empty values from a hash.
|
142
|
+
# @param [Hash] The hash to clean.
|
143
|
+
def self.clean_hash(hash)
|
144
|
+
hash.delete_if { |_key, value| value.to_s.strip.empty? }
|
145
|
+
end
|
146
|
+
|
147
|
+
# Form encodes a hash of parameters.
|
148
|
+
# @param [Hash] The hash of parameters to encode.
|
149
|
+
# @return [Hash] A hash with the same parameters form encoded.
|
150
|
+
def self.form_encode_parameters(form_parameters)
|
151
|
+
encoded = {}
|
152
|
+
form_parameters.each do |key, value|
|
153
|
+
encoded.merge!(APIHelper.form_encode(value, key, formatting:
|
154
|
+
Configuration.array_serialization))
|
155
|
+
end
|
156
|
+
encoded
|
157
|
+
end
|
158
|
+
|
159
|
+
def self.custom_merge(a, b)
|
160
|
+
x = {}
|
161
|
+
a.each do |key, value_a|
|
162
|
+
b.each do |k, value_b|
|
163
|
+
next unless key == k
|
164
|
+
x[k] = []
|
165
|
+
if value_a.instance_of? Array
|
166
|
+
value_a.each do |v|
|
167
|
+
x[k].push(v)
|
168
|
+
end
|
169
|
+
else
|
170
|
+
x[k].push(value_a)
|
171
|
+
end
|
172
|
+
if value_b.instance_of? Array
|
173
|
+
value_b.each do |v|
|
174
|
+
x[k].push(v)
|
175
|
+
end
|
176
|
+
else
|
177
|
+
x[k].push(value_b)
|
178
|
+
end
|
179
|
+
a.delete(k)
|
180
|
+
b.delete(k)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
x.merge!(a)
|
184
|
+
x.merge!(b)
|
185
|
+
x
|
186
|
+
end
|
187
|
+
|
188
|
+
# Form encodes an object.
|
189
|
+
# @param [Dynamic] An object to form encode.
|
190
|
+
# @param [String] The name of the object.
|
191
|
+
# @return [Hash] A form encoded representation of the object in the form
|
192
|
+
# of a hash.
|
193
|
+
def self.form_encode(obj, instance_name, formatting: 'indexed')
|
194
|
+
retval = {}
|
195
|
+
|
196
|
+
serializable_types = [String, Numeric, TrueClass,
|
197
|
+
FalseClass, Date, DateTime]
|
198
|
+
|
199
|
+
# If this is a structure, resolve it's field names.
|
200
|
+
obj = obj.to_hash if obj.is_a? BaseModel
|
201
|
+
|
202
|
+
# Create a form encoded hash for this object.
|
203
|
+
if obj.nil?
|
204
|
+
nil
|
205
|
+
elsif obj.instance_of? Array
|
206
|
+
if formatting == 'indexed'
|
207
|
+
obj.each_with_index do |value, index|
|
208
|
+
retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
|
209
|
+
index.to_s + ']'))
|
210
|
+
end
|
211
|
+
elsif serializable_types.map { |x| obj[0].is_a? x }.any?
|
212
|
+
obj.each do |value|
|
213
|
+
abc = if formatting == 'unindexed'
|
214
|
+
APIHelper.form_encode(value, instance_name + '[]',
|
215
|
+
formatting: formatting)
|
216
|
+
else
|
217
|
+
APIHelper.form_encode(value, instance_name,
|
218
|
+
formatting: formatting)
|
219
|
+
end
|
220
|
+
retval = APIHelper.custom_merge(retval, abc)
|
221
|
+
# print retval
|
222
|
+
end
|
223
|
+
else
|
224
|
+
obj.each_with_index do |value, index|
|
225
|
+
retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
|
226
|
+
index.to_s + ']', formatting: formatting))
|
227
|
+
end
|
228
|
+
end
|
229
|
+
elsif obj.instance_of? Hash
|
230
|
+
obj.each do |key, value|
|
231
|
+
retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
|
232
|
+
key + ']', formatting: formatting))
|
233
|
+
end
|
234
|
+
else
|
235
|
+
retval[instance_name] = obj
|
236
|
+
end
|
237
|
+
retval
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
# Extend types to support to_bool.
|
243
|
+
module ToBoolean
|
244
|
+
def to_bool
|
245
|
+
return true if self == true || to_s.strip =~ /^(true|yes|y|1)$/i
|
246
|
+
false
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
# Extend NilClass type to support to_bool.
|
251
|
+
class NilClass; include ToBoolean; end
|
252
|
+
# Extend TrueClass type to support to_bool.
|
253
|
+
class TrueClass; include ToBoolean; end
|
254
|
+
# Extend FalseClass type to support to_bool.
|
255
|
+
class FalseClass; include ToBoolean; end
|
256
|
+
# Extend Numeric type to support to_bool.
|
257
|
+
class Numeric; include ToBoolean; end
|
258
|
+
# Extend String type to support to_bool.
|
259
|
+
class String; include ToBoolean; end
|