message_media_conversations 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +201 -0
  3. data/README.md +150 -0
  4. data/lib/message_media_conversations.rb +63 -0
  5. data/lib/message_media_conversations/api_helper.rb +273 -0
  6. data/lib/message_media_conversations/configuration.rb +29 -0
  7. data/lib/message_media_conversations/controllers/app_users_controller.rb +285 -0
  8. data/lib/message_media_conversations/controllers/base_controller.rb +59 -0
  9. data/lib/message_media_conversations/controllers/configuration_controller.rb +90 -0
  10. data/lib/message_media_conversations/controllers/facebook_controller.rb +192 -0
  11. data/lib/message_media_conversations/exceptions/api_exception.rb +18 -0
  12. data/lib/message_media_conversations/http/auth/basic_auth.rb +20 -0
  13. data/lib/message_media_conversations/http/faraday_client.rb +55 -0
  14. data/lib/message_media_conversations/http/http_call_back.rb +22 -0
  15. data/lib/message_media_conversations/http/http_client.rb +102 -0
  16. data/lib/message_media_conversations/http/http_context.rb +18 -0
  17. data/lib/message_media_conversations/http/http_method_enum.rb +11 -0
  18. data/lib/message_media_conversations/http/http_request.rb +48 -0
  19. data/lib/message_media_conversations/http/http_response.rb +21 -0
  20. data/lib/message_media_conversations/message_media_conversations_client.rb +39 -0
  21. data/lib/message_media_conversations/models/app_user_dto.rb +51 -0
  22. data/lib/message_media_conversations/models/app_users_dto.rb +40 -0
  23. data/lib/message_media_conversations/models/base_message_dto.rb +42 -0
  24. data/lib/message_media_conversations/models/base_model.rb +34 -0
  25. data/lib/message_media_conversations/models/configure_account_request.rb +42 -0
  26. data/lib/message_media_conversations/models/configure_account_response.rb +42 -0
  27. data/lib/message_media_conversations/models/facebook_authorisation_response.rb +33 -0
  28. data/lib/message_media_conversations/models/facebook_page_dto.rb +42 -0
  29. data/lib/message_media_conversations/models/facebook_pages_dto.rb +40 -0
  30. data/lib/message_media_conversations/models/get_app_user_by_id_response.rb +51 -0
  31. data/lib/message_media_conversations/models/get_app_user_messages_response.rb +33 -0
  32. data/lib/message_media_conversations/models/get_app_users_response.rb +33 -0
  33. data/lib/message_media_conversations/models/get_facebook_authorisation_url_response.rb +33 -0
  34. data/lib/message_media_conversations/models/get_facebook_pages_response.rb +33 -0
  35. data/lib/message_media_conversations/models/message_dto.rb +60 -0
  36. data/lib/message_media_conversations/models/messages_dto.rb +40 -0
  37. data/lib/message_media_conversations/models/provision_account_request.rb +42 -0
  38. data/lib/message_media_conversations/models/send_message_response.rb +42 -0
  39. metadata +168 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fa59fce70fb37a7613166c8e67ebc15767c006dc86ec4615d8259c6574e70af9
4
+ data.tar.gz: 833cefecb67d254d0b086889c3cc9993a70352f670136d778495371a7e40554d
5
+ SHA512:
6
+ metadata.gz: dc89d319b128dfab7b21021628712476ec6d0f1c7394c78cc04689a914af1ac715ec1cb7560e3caa4d863cd64d8bef0ee48d516cdda4c6af045f451ca4bece66
7
+ data.tar.gz: '0948702457c715effdefb8125c1e4a6ef0f52b79bda1f3321af26e84c52e3163060b3a734c84941810ae887bdc4d0e50a11c2790f9ad124405942e7df2099738'
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.
@@ -0,0 +1,150 @@
1
+ # MessageMedia Conversations Ruby SDK
2
+ [![Gem Version](https://badge.fury.io/rb/messagemedia_conversations_sdk.svg)](https://badge.fury.io/rb/messagemedia_conversations_sdk)
3
+
4
+ ⚠️ **Please note the API documented is in beta and is subject to breaking changes in the short term.** ⚠️
5
+
6
+ The MessageMedia Conversations API allows users to communicate by sending and receiving messages via OTT messaging services. This feature is disabled by default. To enable it, you don't need to make any changes to your application, just an account configuration change by MessageMedia's support team (support@messagemedia.com).
7
+
8
+ ## ⭐️ Installing via RubyGems
9
+ Run the following command to install the SDK via RubyGems:
10
+ * `gem install messagemedia-conversations-sdk`
11
+
12
+ ## 🎬 Get Started
13
+ 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.
14
+
15
+ ### 🚀 Configure an account
16
+ ```ruby
17
+ require 'message_media_conversations'
18
+ require 'pp'
19
+
20
+ # Configuration parameters and credentials
21
+ basic_auth_user_name = 'basic_auth_user_name' # The username to use with basic authentication
22
+ basic_auth_password = 'basic_auth_password' # The password to use with basic authentication
23
+
24
+ client = MessageMediaConversations::MessageMediaConversationsClient.new(
25
+ basic_auth_user_name: basic_auth_user_name,
26
+ basic_auth_password: basic_auth_password
27
+ )
28
+
29
+ configuration_controller = client.configuration
30
+ request = MessageMediaConversations::ConfigureAccountRequest.new("http://accounts-domain.com/callback", "Rainbow Serpent Festival")
31
+
32
+ result = configuration_controller.create_configure_account(request)
33
+ pp result
34
+
35
+ ```
36
+
37
+ ### 🔐 Facebook Authorization
38
+ ```ruby
39
+ require 'message_media_conversations'
40
+ require 'pp'
41
+
42
+ # Configuration parameters and credentials
43
+ basic_auth_user_name = 'basic_auth_user_name' # The username to use with basic authentication
44
+ basic_auth_password = 'basic_auth_password' # The password to use with basic authentication
45
+
46
+ client = MessageMediaConversations::MessageMediaConversationsClient.new(
47
+ basic_auth_user_name: basic_auth_user_name,
48
+ basic_auth_password: basic_auth_password
49
+ )
50
+
51
+ facebook_controller = client.facebook
52
+
53
+ result = facebook_controller.get_facebook_authorisation_url()
54
+ pp result
55
+
56
+ ```
57
+
58
+ ### ⬇️ Get Facebook pages
59
+ ```ruby
60
+ require 'message_media_conversations'
61
+ require 'pp'
62
+
63
+ # Configuration parameters and credentials
64
+ basic_auth_user_name = 'basic_auth_user_name' # The username to use with basic authentication
65
+ basic_auth_password = 'basic_auth_password' # The password to use with basic authentication
66
+
67
+ facebook_controller = client.facebook
68
+
69
+ result = facebook_controller.get_facebook_pages()
70
+ pp result
71
+
72
+ ```
73
+
74
+ ### ♻️ Integrate Facebook page
75
+ You can get the facebookPageId by looking at the response of the Get Facebook pages example.
76
+ ```ruby
77
+ require 'message_media_conversations'
78
+ require 'pp'
79
+
80
+ # Configuration parameters and credentials
81
+ basic_auth_user_name = 'basic_auth_user_name' # The username to use with basic authentication
82
+ basic_auth_password = 'basic_auth_password' # The password to use with basic authentication
83
+
84
+ facebook_controller = client.facebook
85
+
86
+ facebook_page_id = 'facebookPageId'
87
+ result = facebook_controller.create_integrate_facebook_page(facebook_page_id)
88
+ pp result
89
+
90
+ ```
91
+
92
+ ### 👤 Get users
93
+ ```ruby
94
+ require 'message_media_conversations'
95
+ require 'pp'
96
+
97
+ # Configuration parameters and credentials
98
+ basic_auth_user_name = 'basic_auth_user_name' # The username to use with basic authentication
99
+ basic_auth_password = 'basic_auth_password' # The password to use with basic authentication
100
+
101
+ appUsers_controller = client.app_users
102
+ result = appUsers_controller.get_app_users()
103
+ pp result
104
+
105
+ ```
106
+
107
+ ### 💬 Get user messages
108
+ You can get the appUserId from the response of the Get users example.
109
+ ```ruby
110
+ require 'message_media_conversations'
111
+ require 'pp'
112
+
113
+ # Configuration parameters and credentials
114
+ basic_auth_user_name = 'basic_auth_user_name' # The username to use with basic authentication
115
+ basic_auth_password = 'basic_auth_password' # The password to use with basic authentication
116
+
117
+ appUsers_controller = client.app_users
118
+ app_user_id = 'appUserId'
119
+
120
+ result = appUsers_controller.get_app_user_messages(app_user_id)
121
+ pp result
122
+
123
+ ```
124
+
125
+ ### ✉️ Send message to user
126
+ You can get the appUserId from the response of the Get users example.
127
+ ```ruby
128
+ require 'message_media_conversations'
129
+ require 'pp'
130
+
131
+ # Configuration parameters and credentials
132
+ basic_auth_user_name = 'basic_auth_user_name' # The username to use with basic authentication
133
+ basic_auth_password = 'basic_auth_password' # The password to use with basic authentication
134
+
135
+ appUsers_controller = client.app_users
136
+ app_user_id = 'appUserId'
137
+ message = BaseMessageDto.new({"key":"value"})
138
+
139
+ appUsers_controller.create_send_message(app_user_id, message)
140
+
141
+ ```
142
+
143
+ ## 📕 Documentation
144
+ Check out the [full API documentation](DOCUMENTATION.md) for more detailed information.
145
+
146
+ ## 😕 Need help?
147
+ Please contact developer support at developers@messagemedia.com or check out the developer portal at [developers.messagemedia.com](https://developers.messagemedia.com/)
148
+
149
+ ## 📃 License
150
+ Apache License. See the [LICENSE](LICENSE) file.
@@ -0,0 +1,63 @@
1
+ # This file was automatically generated for MessageMedia by APIMATIC v2.0
2
+ # ( https://apimatic.io ).
3
+ require 'date'
4
+ require 'json'
5
+ require 'faraday'
6
+ require 'certifi'
7
+ require 'logging'
8
+
9
+ require_relative 'message_media_conversations/api_helper.rb'
10
+ require_relative 'message_media_conversations/message_media_conversations' \
11
+ '_client.rb'
12
+
13
+ # Http
14
+ require_relative 'message_media_conversations/http/http_call_back.rb'
15
+ require_relative 'message_media_conversations/http/http_client.rb'
16
+ require_relative 'message_media_conversations/http/http_method_enum.rb'
17
+ require_relative 'message_media_conversations/http/http_request.rb'
18
+ require_relative 'message_media_conversations/http/http_response.rb'
19
+ require_relative 'message_media_conversations/http/http_context.rb'
20
+ require_relative 'message_media_conversations/http/faraday_client.rb'
21
+ require_relative 'message_media_conversations/http/auth/basic_auth.rb'
22
+
23
+ # Models
24
+ require_relative 'message_media_conversations/models/base_model.rb'
25
+ require_relative 'message_media_conversations/models/send_message_response.rb'
26
+ require_relative 'message_media_conversations/models/get_app_users_response.rb'
27
+ require_relative 'message_media_conversations/models/get_facebook_pages' \
28
+ '_response.rb'
29
+ require_relative 'message_media_conversations/models/get_facebook' \
30
+ '_authorisation_url_response.rb'
31
+ require_relative 'message_media_conversations/models/configure_account' \
32
+ '_response.rb'
33
+ require_relative 'message_media_conversations/models/configure_account' \
34
+ '_request.rb'
35
+ require_relative 'message_media_conversations/models/provision_account' \
36
+ '_request.rb'
37
+ require_relative 'message_media_conversations/models/messages_dto.rb'
38
+ require_relative 'message_media_conversations/models/facebook_pages_dto.rb'
39
+ require_relative 'message_media_conversations/models/facebook_page_dto.rb'
40
+ require_relative 'message_media_conversations/models/facebook_authorisation' \
41
+ '_response.rb'
42
+ require_relative 'message_media_conversations/models/get_app_user_messages' \
43
+ '_response.rb'
44
+ require_relative 'message_media_conversations/models/base_message_dto.rb'
45
+ require_relative 'message_media_conversations/models/message_dto.rb'
46
+ require_relative 'message_media_conversations/models/app_user_dto.rb'
47
+ require_relative 'message_media_conversations/models/get_app_user_by_id' \
48
+ '_response.rb'
49
+ require_relative 'message_media_conversations/models/app_users_dto.rb'
50
+
51
+ # Exceptions
52
+ require_relative 'message_media_conversations/exceptions/api_exception.rb'
53
+
54
+ require_relative 'message_media_conversations/configuration.rb'
55
+
56
+ # Controllers
57
+ require_relative 'message_media_conversations/controllers/base_controller.rb'
58
+ require_relative 'message_media_conversations/controllers/app_users' \
59
+ '_controller.rb'
60
+ require_relative 'message_media_conversations/controllers/configuration' \
61
+ '_controller.rb'
62
+ require_relative 'message_media_conversations/controllers/facebook' \
63
+ '_controller.rb'
@@ -0,0 +1,273 @@
1
+ # This file was automatically generated for MessageMedia by APIMATIC v2.0
2
+ # ( https://apimatic.io ).
3
+
4
+ module MessageMediaConversations
5
+ # API utility class
6
+ class APIHelper
7
+ # Serializes an array parameter (creates key value pairs).
8
+ # @param [String] The name of the parameter.
9
+ # @param [Array] The value of the parameter.
10
+ # @param [String] The format of the serialization.
11
+ def self.serialize_array(key, array, formatting: 'indexed')
12
+ tuples = []
13
+
14
+ if formatting == 'unindexed'
15
+ tuples += array.map { |element| ["#{key}[]", element] }
16
+ elsif formatting == 'indexed'
17
+ tuples += array.map.with_index do |element, index|
18
+ ["#{key}[#{index}]", element]
19
+ end
20
+ elsif formatting == 'plain'
21
+ tuples += array.map { |element| [key, element] }
22
+ else
23
+ raise ArgumentError, 'Invalid format provided.'
24
+ end
25
+ tuples
26
+ end
27
+
28
+ # Replaces template parameters in the given url.
29
+ # @param [String] The query string builder to replace the template
30
+ # parameters.
31
+ # @param [Hash] The parameters to replace in the url.
32
+ def self.append_url_with_template_parameters(query_builder, parameters)
33
+ # perform parameter validation
34
+ unless query_builder.instance_of? String
35
+ raise ArgumentError, 'Given value for parameter \"query_builder\" is
36
+ invalid.'
37
+ end
38
+
39
+ # Return if there are no parameters to replace.
40
+ return query_builder if parameters.nil?
41
+
42
+ # Iterate and append parameters.
43
+ parameters.each do |key, value|
44
+ replace_value = ''
45
+
46
+ if value.nil?
47
+ replace_value = ''
48
+ elsif value.instance_of? Array
49
+ value.map! { |element| CGI.escape(element.to_s) }
50
+ replace_value = value.join('/')
51
+ else
52
+ replace_value = CGI.escape(value.to_s)
53
+ end
54
+
55
+ # Find the template parameter and replace it with its value.
56
+ query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value)
57
+ end
58
+ query_builder
59
+ end
60
+
61
+ # Appends the given set of parameters to the given query string.
62
+ # @param [String] The query string builder to add the query parameters to.
63
+ # @param [Hash] The parameters to append.
64
+ # @param [String] The format of array parameter serialization.
65
+ def self.append_url_with_query_parameters(query_builder, parameters,
66
+ array_serialization: 'indexed')
67
+ # Perform parameter validation.
68
+ unless query_builder.instance_of? String
69
+ raise ArgumentError, 'Given value for parameter \"query_builder\"
70
+ is invalid.'
71
+ end
72
+
73
+ # Return if there are no parameters to replace.
74
+ return query_builder if parameters.nil?
75
+
76
+ parameters.each do |key, value|
77
+ seperator = query_builder.include?('?') ? '&' : '?'
78
+ unless value.nil?
79
+ if value.instance_of? Array
80
+ value.compact!
81
+ query_builder += if array_serialization == 'csv'
82
+ "#{seperator}#{key}=#{value.map do |element|
83
+ CGI.escape(element.to_s)
84
+ end.join(',')}"
85
+ elsif array_serialization == 'psv'
86
+ "#{seperator}#{key}=#{value.map do |element|
87
+ CGI.escape(element.to_s)
88
+ end.join('|')}"
89
+ elsif array_serialization == 'tsv'
90
+ "#{seperator}#{key}=#{value.map do |element|
91
+ CGI.escape(element.to_s)
92
+ end.join("\t")}"
93
+ else
94
+ "#{seperator}#{APIHelper.serialize_array(
95
+ key, value, formatting: array_serialization
96
+ ).map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }
97
+ .join('&')}"
98
+ end
99
+ else
100
+ query_builder += "#{seperator}#{key}=#{CGI.escape(value.to_s)}"
101
+ end
102
+ end
103
+ end
104
+ query_builder
105
+ end
106
+
107
+ # Validates and processes the given Url.
108
+ # @param [String] The given Url to process.
109
+ # @return [String] Pre-processed Url as string.
110
+ def self.clean_url(url)
111
+ # Perform parameter validation.
112
+ raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
113
+
114
+ # Ensure that the urls are absolute.
115
+ matches = url.match(%r{^(https?:\/\/[^\/]+)})
116
+ raise ArgumentError, 'Invalid Url format.' if matches.nil?
117
+
118
+ # Get the http protocol match.
119
+ protocol = matches[1]
120
+
121
+ # Check if parameters exist.
122
+ index = url.index('?')
123
+
124
+ # Remove redundant forward slashes.
125
+ query = url[protocol.length...(!index.nil? ? index : url.length)]
126
+ query.gsub!(%r{\/\/+}, '/')
127
+
128
+ # Get the parameters.
129
+ parameters = !index.nil? ? url[url.index('?')...url.length] : ''
130
+
131
+ # Return processed url.
132
+ protocol + query + parameters
133
+ end
134
+
135
+ # Parses JSON string.
136
+ # @param [String] A JSON string.
137
+ def self.json_deserialize(json)
138
+ return JSON.parse(json)
139
+ rescue StandardError
140
+ raise TypeError, 'Server responded with invalid JSON.'
141
+ end
142
+
143
+ # Removes elements with empty values from a hash.
144
+ # @param [Hash] The hash to clean.
145
+ def self.clean_hash(hash)
146
+ hash.delete_if { |_key, value| value.to_s.strip.empty? }
147
+ end
148
+
149
+ # Form encodes a hash of parameters.
150
+ # @param [Hash] The hash of parameters to encode.
151
+ # @return [Hash] A hash with the same parameters form encoded.
152
+ def self.form_encode_parameters(form_parameters)
153
+ encoded = {}
154
+ form_parameters.each do |key, value|
155
+ encoded.merge!(APIHelper.form_encode(value, key, formatting:
156
+ Configuration.array_serialization))
157
+ end
158
+ encoded
159
+ end
160
+
161
+ def self.custom_merge(a, b)
162
+ x = {}
163
+ a.each do |key, value_a|
164
+ b.each do |k, value_b|
165
+ next unless key == k
166
+ x[k] = []
167
+ if value_a.instance_of? Array
168
+ value_a.each do |v|
169
+ x[k].push(v)
170
+ end
171
+ else
172
+ x[k].push(value_a)
173
+ end
174
+ if value_b.instance_of? Array
175
+ value_b.each do |v|
176
+ x[k].push(v)
177
+ end
178
+ else
179
+ x[k].push(value_b)
180
+ end
181
+ a.delete(k)
182
+ b.delete(k)
183
+ end
184
+ end
185
+ x.merge!(a)
186
+ x.merge!(b)
187
+ x
188
+ end
189
+
190
+ # Form encodes an object.
191
+ # @param [Dynamic] An object to form encode.
192
+ # @param [String] The name of the object.
193
+ # @return [Hash] A form encoded representation of the object in the form
194
+ # of a hash.
195
+ def self.form_encode(obj, instance_name, formatting: 'indexed')
196
+ retval = {}
197
+
198
+ serializable_types = [String, Numeric, TrueClass,
199
+ FalseClass, Date, DateTime]
200
+
201
+ # If this is a structure, resolve it's field names.
202
+ obj = obj.to_hash if obj.is_a? BaseModel
203
+
204
+ # Create a form encoded hash for this object.
205
+ if obj.nil?
206
+ nil
207
+ elsif obj.instance_of? Array
208
+ if formatting == 'indexed'
209
+ obj.each_with_index do |value, index|
210
+ retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
211
+ index.to_s + ']'))
212
+ end
213
+ elsif serializable_types.map { |x| obj[0].is_a? x }.any?
214
+ obj.each do |value|
215
+ abc = if formatting == 'unindexed'
216
+ APIHelper.form_encode(value, instance_name + '[]',
217
+ formatting: formatting)
218
+ else
219
+ APIHelper.form_encode(value, instance_name,
220
+ formatting: formatting)
221
+ end
222
+ retval = APIHelper.custom_merge(retval, abc)
223
+ # print retval
224
+ end
225
+ else
226
+ obj.each_with_index do |value, index|
227
+ retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
228
+ index.to_s + ']', formatting: formatting))
229
+ end
230
+ end
231
+ elsif obj.instance_of? Hash
232
+ obj.each do |key, value|
233
+ retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
234
+ key + ']', formatting: formatting))
235
+ end
236
+ else
237
+ retval[instance_name] = obj
238
+ end
239
+ retval
240
+ end
241
+
242
+ # Safely converts a string into an rfc3339 DateTime object
243
+ # @param [String] The datetime string
244
+ # @return [DateTime] A DateTime object of rfc3339 format
245
+ def self.rfc3339(date_time)
246
+ # missing timezone information
247
+ if date_time.end_with?('Z') || date_time.index('+')
248
+ DateTime.rfc3339(date_time)
249
+ else
250
+ DateTime.rfc3339(date_time + 'Z')
251
+ end
252
+ end
253
+ end
254
+ end
255
+
256
+ # Extend types to support to_bool.
257
+ module ToBoolean
258
+ def to_bool
259
+ return true if self == true || to_s.strip =~ /^(true|yes|y|1)$/i
260
+ false
261
+ end
262
+ end
263
+
264
+ # Extend NilClass type to support to_bool.
265
+ class NilClass; include ToBoolean; end
266
+ # Extend TrueClass type to support to_bool.
267
+ class TrueClass; include ToBoolean; end
268
+ # Extend FalseClass type to support to_bool.
269
+ class FalseClass; include ToBoolean; end
270
+ # Extend Numeric type to support to_bool.
271
+ class Numeric; include ToBoolean; end
272
+ # Extend String type to support to_bool.
273
+ class String; include ToBoolean; end