sendpost_ruby_sdk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +9 -0
  3. data/README.md +79 -0
  4. data/Rakefile +10 -0
  5. data/config-ruby.json +11 -0
  6. data/docs/Attachment.md +20 -0
  7. data/docs/City.md +26 -0
  8. data/docs/CopyTo.md +22 -0
  9. data/docs/Device.md +18 -0
  10. data/docs/EmailApi.md +145 -0
  11. data/docs/EmailMessage.md +46 -0
  12. data/docs/EmailResponse.md +26 -0
  13. data/docs/EventMetadata.md +30 -0
  14. data/docs/From.md +20 -0
  15. data/docs/Os.md +26 -0
  16. data/docs/QEmailMessage.md +70 -0
  17. data/docs/QEvent.md +42 -0
  18. data/docs/ReplyTo.md +20 -0
  19. data/docs/To.md +26 -0
  20. data/docs/UserAgent.md +24 -0
  21. data/docs/WebhookEvent.md +20 -0
  22. data/generate-libs.bash +1 -0
  23. data/git_push.sh +57 -0
  24. data/lib/sendpost_ruby_sdk/api/email_api.rb +160 -0
  25. data/lib/sendpost_ruby_sdk/api_client.rb +391 -0
  26. data/lib/sendpost_ruby_sdk/api_error.rb +58 -0
  27. data/lib/sendpost_ruby_sdk/configuration.rb +288 -0
  28. data/lib/sendpost_ruby_sdk/models/attachment.rb +228 -0
  29. data/lib/sendpost_ruby_sdk/models/city.rb +255 -0
  30. data/lib/sendpost_ruby_sdk/models/copy_to.rb +237 -0
  31. data/lib/sendpost_ruby_sdk/models/device.rb +219 -0
  32. data/lib/sendpost_ruby_sdk/models/email_message.rb +351 -0
  33. data/lib/sendpost_ruby_sdk/models/email_response.rb +255 -0
  34. data/lib/sendpost_ruby_sdk/models/event_metadata.rb +273 -0
  35. data/lib/sendpost_ruby_sdk/models/from.rb +228 -0
  36. data/lib/sendpost_ruby_sdk/models/os.rb +255 -0
  37. data/lib/sendpost_ruby_sdk/models/q_email_message.rb +461 -0
  38. data/lib/sendpost_ruby_sdk/models/q_event.rb +329 -0
  39. data/lib/sendpost_ruby_sdk/models/reply_to.rb +228 -0
  40. data/lib/sendpost_ruby_sdk/models/to.rb +259 -0
  41. data/lib/sendpost_ruby_sdk/models/user_agent.rb +246 -0
  42. data/lib/sendpost_ruby_sdk/models/webhook_event.rb +228 -0
  43. data/lib/sendpost_ruby_sdk/version.rb +15 -0
  44. data/lib/sendpost_ruby_sdk.rb +55 -0
  45. data/sendpost.yaml +438 -0
  46. data/sendpost_ruby_sdk.gemspec +38 -0
  47. data/spec/api/email_api_spec.rb +59 -0
  48. data/spec/api_client_spec.rb +228 -0
  49. data/spec/configuration_spec.rb +42 -0
  50. data/spec/models/attachment_spec.rb +40 -0
  51. data/spec/models/city_spec.rb +58 -0
  52. data/spec/models/copy_to_spec.rb +46 -0
  53. data/spec/models/device_spec.rb +34 -0
  54. data/spec/models/email_message_spec.rb +118 -0
  55. data/spec/models/email_response_spec.rb +58 -0
  56. data/spec/models/event_metadata_spec.rb +70 -0
  57. data/spec/models/from_spec.rb +40 -0
  58. data/spec/models/os_spec.rb +58 -0
  59. data/spec/models/q_email_message_spec.rb +190 -0
  60. data/spec/models/q_event_spec.rb +106 -0
  61. data/spec/models/reply_to_spec.rb +40 -0
  62. data/spec/models/to_spec.rb +58 -0
  63. data/spec/models/user_agent_spec.rb +52 -0
  64. data/spec/models/webhook_event_spec.rb +40 -0
  65. data/spec/spec_helper.rb +111 -0
  66. metadata +168 -0
data/docs/UserAgent.md ADDED
@@ -0,0 +1,24 @@
1
+ # Sendpost::UserAgent
2
+
3
+ ## Properties
4
+
5
+ | Name | Type | Description | Notes |
6
+ | ---- | ---- | ----------- | ----- |
7
+ | **family** | **String** | | [optional] |
8
+ | **major** | **String** | | [optional] |
9
+ | **minor** | **String** | | [optional] |
10
+ | **patch** | **String** | | [optional] |
11
+
12
+ ## Example
13
+
14
+ ```ruby
15
+ require 'sendpost_ruby_sdk'
16
+
17
+ instance = Sendpost::UserAgent.new(
18
+ family: null,
19
+ major: null,
20
+ minor: null,
21
+ patch: null
22
+ )
23
+ ```
24
+
@@ -0,0 +1,20 @@
1
+ # Sendpost::WebhookEvent
2
+
3
+ ## Properties
4
+
5
+ | Name | Type | Description | Notes |
6
+ | ---- | ---- | ----------- | ----- |
7
+ | **event** | [**QEvent**](QEvent.md) | | [optional] |
8
+ | **email_message** | [**QEmailMessage**](QEmailMessage.md) | | [optional] |
9
+
10
+ ## Example
11
+
12
+ ```ruby
13
+ require 'sendpost_ruby_sdk'
14
+
15
+ instance = Sendpost::WebhookEvent.new(
16
+ event: null,
17
+ email_message: null
18
+ )
19
+ ```
20
+
@@ -0,0 +1 @@
1
+ openapi-generator generate -i sendpost.yaml -g ruby -o ./ -c ./config-ruby.json
data/git_push.sh ADDED
@@ -0,0 +1,57 @@
1
+ #!/bin/sh
2
+ # ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
3
+ #
4
+ # Usage example: /bin/sh ./git_push.sh wing328 openapi-petstore-perl "minor update" "gitlab.com"
5
+
6
+ git_user_id=$1
7
+ git_repo_id=$2
8
+ release_note=$3
9
+ git_host=$4
10
+
11
+ if [ "$git_host" = "" ]; then
12
+ git_host="github.com"
13
+ echo "[INFO] No command line input provided. Set \$git_host to $git_host"
14
+ fi
15
+
16
+ if [ "$git_user_id" = "" ]; then
17
+ git_user_id="GIT_USER_ID"
18
+ echo "[INFO] No command line input provided. Set \$git_user_id to $git_user_id"
19
+ fi
20
+
21
+ if [ "$git_repo_id" = "" ]; then
22
+ git_repo_id="GIT_REPO_ID"
23
+ echo "[INFO] No command line input provided. Set \$git_repo_id to $git_repo_id"
24
+ fi
25
+
26
+ if [ "$release_note" = "" ]; then
27
+ release_note="Minor update"
28
+ echo "[INFO] No command line input provided. Set \$release_note to $release_note"
29
+ fi
30
+
31
+ # Initialize the local directory as a Git repository
32
+ git init
33
+
34
+ # Adds the files in the local repository and stages them for commit.
35
+ git add .
36
+
37
+ # Commits the tracked changes and prepares them to be pushed to a remote repository.
38
+ git commit -m "$release_note"
39
+
40
+ # Sets the new remote
41
+ git_remote=$(git remote)
42
+ if [ "$git_remote" = "" ]; then # git remote not defined
43
+
44
+ if [ "$GIT_TOKEN" = "" ]; then
45
+ echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment."
46
+ git remote add origin https://${git_host}/${git_user_id}/${git_repo_id}.git
47
+ else
48
+ git remote add origin https://${git_user_id}:"${GIT_TOKEN}"@${git_host}/${git_user_id}/${git_repo_id}.git
49
+ fi
50
+
51
+ fi
52
+
53
+ git pull origin master
54
+
55
+ # Pushes (Forces) the changes in the local repository up to the remote repository
56
+ echo "Git pushing to https://${git_host}/${git_user_id}/${git_repo_id}.git"
57
+ git push origin master 2>&1 | grep -v 'To https'
@@ -0,0 +1,160 @@
1
+ =begin
2
+ #SendPost API
3
+
4
+ #Email API and SMTP relay to not just send and measure email sending, but also alert and optimise. We provide you with tools, expertise and support needed to reliably deliver emails to your customers inboxes on time, every time.
5
+
6
+ The version of the OpenAPI document: 1.0.0
7
+ Contact: hello@sendpost.io
8
+ Generated by: https://openapi-generator.tech
9
+ OpenAPI Generator version: 6.6.0
10
+
11
+ =end
12
+
13
+ require 'cgi'
14
+
15
+ module Sendpost
16
+ class EmailApi
17
+ attr_accessor :api_client
18
+
19
+ def initialize(api_client = ApiClient.default)
20
+ @api_client = api_client
21
+ end
22
+ # Send Email To Contacts
23
+ # @param x_sub_account_api_key [String] Sub-Account API Key
24
+ # @param [Hash] opts the optional parameters
25
+ # @option opts [EmailMessage] :email_message Email message
26
+ # @return [Array<EmailResponse>]
27
+ def send_email(x_sub_account_api_key, opts = {})
28
+ data, _status_code, _headers = send_email_with_http_info(x_sub_account_api_key, opts)
29
+ data
30
+ end
31
+
32
+ # Send Email To Contacts
33
+ # @param x_sub_account_api_key [String] Sub-Account API Key
34
+ # @param [Hash] opts the optional parameters
35
+ # @option opts [EmailMessage] :email_message Email message
36
+ # @return [Array<(Array<EmailResponse>, Integer, Hash)>] Array<EmailResponse> data, response status code and response headers
37
+ def send_email_with_http_info(x_sub_account_api_key, opts = {})
38
+ if @api_client.config.debugging
39
+ @api_client.config.logger.debug 'Calling API: EmailApi.send_email ...'
40
+ end
41
+ # verify the required parameter 'x_sub_account_api_key' is set
42
+ if @api_client.config.client_side_validation && x_sub_account_api_key.nil?
43
+ fail ArgumentError, "Missing the required parameter 'x_sub_account_api_key' when calling EmailApi.send_email"
44
+ end
45
+ # resource path
46
+ local_var_path = '/subaccount/email/'
47
+
48
+ # query parameters
49
+ query_params = opts[:query_params] || {}
50
+
51
+ # header parameters
52
+ header_params = opts[:header_params] || {}
53
+ # HTTP header 'Accept' (if needed)
54
+ header_params['Accept'] = @api_client.select_header_accept(['application/json'])
55
+ # HTTP header 'Content-Type'
56
+ content_type = @api_client.select_header_content_type(['application/json'])
57
+ if !content_type.nil?
58
+ header_params['Content-Type'] = content_type
59
+ end
60
+ header_params[:'X-SubAccount-ApiKey'] = x_sub_account_api_key
61
+
62
+ # form parameters
63
+ form_params = opts[:form_params] || {}
64
+
65
+ # http body (model)
66
+ post_body = opts[:debug_body] || @api_client.object_to_http_body(opts[:'email_message'])
67
+
68
+ # return_type
69
+ return_type = opts[:debug_return_type] || 'Array<EmailResponse>'
70
+
71
+ # auth_names
72
+ auth_names = opts[:debug_auth_names] || []
73
+
74
+ new_options = opts.merge(
75
+ :operation => :"EmailApi.send_email",
76
+ :header_params => header_params,
77
+ :query_params => query_params,
78
+ :form_params => form_params,
79
+ :body => post_body,
80
+ :auth_names => auth_names,
81
+ :return_type => return_type
82
+ )
83
+
84
+ data, status_code, headers = @api_client.call_api(:POST, local_var_path, new_options)
85
+ if @api_client.config.debugging
86
+ @api_client.config.logger.debug "API called: EmailApi#send_email\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
87
+ end
88
+ return data, status_code, headers
89
+ end
90
+
91
+ # Send Email To Contacts With Template
92
+ # @param x_sub_account_api_key [String] Sub-Account API Key
93
+ # @param [Hash] opts the optional parameters
94
+ # @option opts [EmailMessage] :email_message Email message
95
+ # @return [Array<EmailResponse>]
96
+ def send_email_with_template(x_sub_account_api_key, opts = {})
97
+ data, _status_code, _headers = send_email_with_template_with_http_info(x_sub_account_api_key, opts)
98
+ data
99
+ end
100
+
101
+ # Send Email To Contacts With Template
102
+ # @param x_sub_account_api_key [String] Sub-Account API Key
103
+ # @param [Hash] opts the optional parameters
104
+ # @option opts [EmailMessage] :email_message Email message
105
+ # @return [Array<(Array<EmailResponse>, Integer, Hash)>] Array<EmailResponse> data, response status code and response headers
106
+ def send_email_with_template_with_http_info(x_sub_account_api_key, opts = {})
107
+ if @api_client.config.debugging
108
+ @api_client.config.logger.debug 'Calling API: EmailApi.send_email_with_template ...'
109
+ end
110
+ # verify the required parameter 'x_sub_account_api_key' is set
111
+ if @api_client.config.client_side_validation && x_sub_account_api_key.nil?
112
+ fail ArgumentError, "Missing the required parameter 'x_sub_account_api_key' when calling EmailApi.send_email_with_template"
113
+ end
114
+ # resource path
115
+ local_var_path = '/subaccount/email/template'
116
+
117
+ # query parameters
118
+ query_params = opts[:query_params] || {}
119
+
120
+ # header parameters
121
+ header_params = opts[:header_params] || {}
122
+ # HTTP header 'Accept' (if needed)
123
+ header_params['Accept'] = @api_client.select_header_accept(['application/json'])
124
+ # HTTP header 'Content-Type'
125
+ content_type = @api_client.select_header_content_type(['application/json'])
126
+ if !content_type.nil?
127
+ header_params['Content-Type'] = content_type
128
+ end
129
+ header_params[:'X-SubAccount-ApiKey'] = x_sub_account_api_key
130
+
131
+ # form parameters
132
+ form_params = opts[:form_params] || {}
133
+
134
+ # http body (model)
135
+ post_body = opts[:debug_body] || @api_client.object_to_http_body(opts[:'email_message'])
136
+
137
+ # return_type
138
+ return_type = opts[:debug_return_type] || 'Array<EmailResponse>'
139
+
140
+ # auth_names
141
+ auth_names = opts[:debug_auth_names] || []
142
+
143
+ new_options = opts.merge(
144
+ :operation => :"EmailApi.send_email_with_template",
145
+ :header_params => header_params,
146
+ :query_params => query_params,
147
+ :form_params => form_params,
148
+ :body => post_body,
149
+ :auth_names => auth_names,
150
+ :return_type => return_type
151
+ )
152
+
153
+ data, status_code, headers = @api_client.call_api(:POST, local_var_path, new_options)
154
+ if @api_client.config.debugging
155
+ @api_client.config.logger.debug "API called: EmailApi#send_email_with_template\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
156
+ end
157
+ return data, status_code, headers
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,391 @@
1
+ =begin
2
+ #SendPost API
3
+
4
+ #Email API and SMTP relay to not just send and measure email sending, but also alert and optimise. We provide you with tools, expertise and support needed to reliably deliver emails to your customers inboxes on time, every time.
5
+
6
+ The version of the OpenAPI document: 1.0.0
7
+ Contact: hello@sendpost.io
8
+ Generated by: https://openapi-generator.tech
9
+ OpenAPI Generator version: 6.6.0
10
+
11
+ =end
12
+
13
+ require 'date'
14
+ require 'json'
15
+ require 'logger'
16
+ require 'tempfile'
17
+ require 'time'
18
+ require 'typhoeus'
19
+
20
+ module Sendpost
21
+ class ApiClient
22
+ # The Configuration object holding settings to be used in the API client.
23
+ attr_accessor :config
24
+
25
+ # Defines the headers to be used in HTTP requests of all API calls by default.
26
+ #
27
+ # @return [Hash]
28
+ attr_accessor :default_headers
29
+
30
+ # Initializes the ApiClient
31
+ # @option config [Configuration] Configuration for initializing the object, default to Configuration.default
32
+ def initialize(config = Configuration.default)
33
+ @config = config
34
+ @user_agent = "OpenAPI-Generator/#{VERSION}/ruby"
35
+ @default_headers = {
36
+ 'Content-Type' => 'application/json',
37
+ 'User-Agent' => @user_agent
38
+ }
39
+ end
40
+
41
+ def self.default
42
+ @@default ||= ApiClient.new
43
+ end
44
+
45
+ # Call an API with given options.
46
+ #
47
+ # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
48
+ # the data deserialized from response body (could be nil), response status code and response headers.
49
+ def call_api(http_method, path, opts = {})
50
+ request = build_request(http_method, path, opts)
51
+ response = request.run
52
+
53
+ if @config.debugging
54
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
55
+ end
56
+
57
+ unless response.success?
58
+ if response.timed_out?
59
+ fail ApiError.new('Connection timed out')
60
+ elsif response.code == 0
61
+ # Errors from libcurl will be made visible here
62
+ fail ApiError.new(:code => 0,
63
+ :message => response.return_message)
64
+ else
65
+ fail ApiError.new(:code => response.code,
66
+ :response_headers => response.headers,
67
+ :response_body => response.body),
68
+ response.status_message
69
+ end
70
+ end
71
+
72
+ if opts[:return_type]
73
+ data = deserialize(response, opts[:return_type])
74
+ else
75
+ data = nil
76
+ end
77
+ return data, response.code, response.headers
78
+ end
79
+
80
+ # Builds the HTTP request
81
+ #
82
+ # @param [String] http_method HTTP method/verb (e.g. POST)
83
+ # @param [String] path URL path (e.g. /account/new)
84
+ # @option opts [Hash] :header_params Header parameters
85
+ # @option opts [Hash] :query_params Query parameters
86
+ # @option opts [Hash] :form_params Query parameters
87
+ # @option opts [Object] :body HTTP body (JSON/XML)
88
+ # @return [Typhoeus::Request] A Typhoeus Request
89
+ def build_request(http_method, path, opts = {})
90
+ url = build_request_url(path, opts)
91
+ http_method = http_method.to_sym.downcase
92
+
93
+ header_params = @default_headers.merge(opts[:header_params] || {})
94
+ query_params = opts[:query_params] || {}
95
+ form_params = opts[:form_params] || {}
96
+ follow_location = opts[:follow_location] || true
97
+
98
+
99
+ # set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
100
+ _verify_ssl_host = @config.verify_ssl_host ? 2 : 0
101
+
102
+ req_opts = {
103
+ :method => http_method,
104
+ :headers => header_params,
105
+ :params => query_params,
106
+ :params_encoding => @config.params_encoding,
107
+ :timeout => @config.timeout,
108
+ :ssl_verifypeer => @config.verify_ssl,
109
+ :ssl_verifyhost => _verify_ssl_host,
110
+ :sslcert => @config.cert_file,
111
+ :sslkey => @config.key_file,
112
+ :verbose => @config.debugging,
113
+ :followlocation => follow_location
114
+ }
115
+
116
+ # set custom cert, if provided
117
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
118
+
119
+ if [:post, :patch, :put, :delete].include?(http_method)
120
+ req_body = build_request_body(header_params, form_params, opts[:body])
121
+ req_opts.update :body => req_body
122
+ if @config.debugging
123
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
124
+ end
125
+ end
126
+
127
+ request = Typhoeus::Request.new(url, req_opts)
128
+ download_file(request) if opts[:return_type] == 'File'
129
+ request
130
+ end
131
+
132
+ # Builds the HTTP request body
133
+ #
134
+ # @param [Hash] header_params Header parameters
135
+ # @param [Hash] form_params Query parameters
136
+ # @param [Object] body HTTP body (JSON/XML)
137
+ # @return [String] HTTP body data in the form of string
138
+ def build_request_body(header_params, form_params, body)
139
+ # http form
140
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
141
+ header_params['Content-Type'] == 'multipart/form-data'
142
+ data = {}
143
+ form_params.each do |key, value|
144
+ case value
145
+ when ::File, ::Array, nil
146
+ # let typhoeus handle File, Array and nil parameters
147
+ data[key] = value
148
+ else
149
+ data[key] = value.to_s
150
+ end
151
+ end
152
+ elsif body
153
+ data = body.is_a?(String) ? body : body.to_json
154
+ else
155
+ data = nil
156
+ end
157
+ data
158
+ end
159
+
160
+ # Save response body into a file in (the defined) temporary folder, using the filename
161
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
162
+ # The response body is written to the file in chunks in order to handle files which
163
+ # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
164
+ # process can use.
165
+ #
166
+ # @see Configuration#temp_folder_path
167
+ def download_file(request)
168
+ tempfile = nil
169
+ encoding = nil
170
+ request.on_headers do |response|
171
+ content_disposition = response.headers['Content-Disposition']
172
+ if content_disposition && content_disposition =~ /filename=/i
173
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
174
+ prefix = sanitize_filename(filename)
175
+ else
176
+ prefix = 'download-'
177
+ end
178
+ prefix = prefix + '-' unless prefix.end_with?('-')
179
+ encoding = response.body.encoding
180
+ tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
181
+ @tempfile = tempfile
182
+ end
183
+ request.on_body do |chunk|
184
+ chunk.force_encoding(encoding)
185
+ tempfile.write(chunk)
186
+ end
187
+ request.on_complete do |response|
188
+ if tempfile
189
+ tempfile.close
190
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
191
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
192
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
193
+ "explicitly with `tempfile.delete`"
194
+ end
195
+ end
196
+ end
197
+
198
+ # Check if the given MIME is a JSON MIME.
199
+ # JSON MIME examples:
200
+ # application/json
201
+ # application/json; charset=UTF8
202
+ # APPLICATION/JSON
203
+ # */*
204
+ # @param [String] mime MIME
205
+ # @return [Boolean] True if the MIME is application/json
206
+ def json_mime?(mime)
207
+ (mime == '*/*') || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
208
+ end
209
+
210
+ # Deserialize the response to the given return type.
211
+ #
212
+ # @param [Response] response HTTP response
213
+ # @param [String] return_type some examples: "User", "Array<User>", "Hash<String, Integer>"
214
+ def deserialize(response, return_type)
215
+ body = response.body
216
+
217
+ # handle file downloading - return the File instance processed in request callbacks
218
+ # note that response body is empty when the file is written in chunks in request on_body callback
219
+ return @tempfile if return_type == 'File'
220
+
221
+ return nil if body.nil? || body.empty?
222
+
223
+ # return response body directly for String return type
224
+ return body if return_type == 'String'
225
+
226
+ # ensuring a default content type
227
+ content_type = response.headers['Content-Type'] || 'application/json'
228
+
229
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
230
+
231
+ begin
232
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
233
+ rescue JSON::ParserError => e
234
+ if %w(String Date Time).include?(return_type)
235
+ data = body
236
+ else
237
+ raise e
238
+ end
239
+ end
240
+
241
+ convert_to_type data, return_type
242
+ end
243
+
244
+ # Convert data to the given return type.
245
+ # @param [Object] data Data to be converted
246
+ # @param [String] return_type Return type
247
+ # @return [Mixed] Data in a particular type
248
+ def convert_to_type(data, return_type)
249
+ return nil if data.nil?
250
+ case return_type
251
+ when 'String'
252
+ data.to_s
253
+ when 'Integer'
254
+ data.to_i
255
+ when 'Float'
256
+ data.to_f
257
+ when 'Boolean'
258
+ data == true
259
+ when 'Time'
260
+ # parse date time (expecting ISO 8601 format)
261
+ Time.parse data
262
+ when 'Date'
263
+ # parse date time (expecting ISO 8601 format)
264
+ Date.parse data
265
+ when 'Object'
266
+ # generic object (usually a Hash), return directly
267
+ data
268
+ when /\AArray<(.+)>\z/
269
+ # e.g. Array<Pet>
270
+ sub_type = $1
271
+ data.map { |item| convert_to_type(item, sub_type) }
272
+ when /\AHash\<String, (.+)\>\z/
273
+ # e.g. Hash<String, Integer>
274
+ sub_type = $1
275
+ {}.tap do |hash|
276
+ data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
277
+ end
278
+ else
279
+ # models (e.g. Pet) or oneOf
280
+ klass = Sendpost.const_get(return_type)
281
+ klass.respond_to?(:openapi_one_of) ? klass.build(data) : klass.build_from_hash(data)
282
+ end
283
+ end
284
+
285
+ # Sanitize filename by removing path.
286
+ # e.g. ../../sun.gif becomes sun.gif
287
+ #
288
+ # @param [String] filename the filename to be sanitized
289
+ # @return [String] the sanitized filename
290
+ def sanitize_filename(filename)
291
+ filename.gsub(/.*[\/\\]/, '')
292
+ end
293
+
294
+ def build_request_url(path, opts = {})
295
+ # Add leading and trailing slashes to path
296
+ path = "/#{path}".gsub(/\/+/, '/')
297
+ @config.base_url(opts[:operation]) + path
298
+ end
299
+
300
+ # Update header and query params based on authentication settings.
301
+ #
302
+ # @param [Hash] header_params Header parameters
303
+ # @param [Hash] query_params Query parameters
304
+ # @param [String] auth_names Authentication scheme name
305
+ def update_params_for_auth!(header_params, query_params, auth_names)
306
+ Array(auth_names).each do |auth_name|
307
+ auth_setting = @config.auth_settings[auth_name]
308
+ next unless auth_setting
309
+ case auth_setting[:in]
310
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
311
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
312
+ else fail ArgumentError, 'Authentication token must be in `query` or `header`'
313
+ end
314
+ end
315
+ end
316
+
317
+ # Sets user agent in HTTP header
318
+ #
319
+ # @param [String] user_agent User agent (e.g. openapi-generator/ruby/1.0.0)
320
+ def user_agent=(user_agent)
321
+ @user_agent = user_agent
322
+ @default_headers['User-Agent'] = @user_agent
323
+ end
324
+
325
+ # Return Accept header based on an array of accepts provided.
326
+ # @param [Array] accepts array for Accept
327
+ # @return [String] the Accept header (e.g. application/json)
328
+ def select_header_accept(accepts)
329
+ return nil if accepts.nil? || accepts.empty?
330
+ # use JSON when present, otherwise use all of the provided
331
+ json_accept = accepts.find { |s| json_mime?(s) }
332
+ json_accept || accepts.join(',')
333
+ end
334
+
335
+ # Return Content-Type header based on an array of content types provided.
336
+ # @param [Array] content_types array for Content-Type
337
+ # @return [String] the Content-Type header (e.g. application/json)
338
+ def select_header_content_type(content_types)
339
+ # return nil by default
340
+ return if content_types.nil? || content_types.empty?
341
+ # use JSON when present, otherwise use the first one
342
+ json_content_type = content_types.find { |s| json_mime?(s) }
343
+ json_content_type || content_types.first
344
+ end
345
+
346
+ # Convert object (array, hash, object, etc) to JSON string.
347
+ # @param [Object] model object to be converted into JSON string
348
+ # @return [String] JSON string representation of the object
349
+ def object_to_http_body(model)
350
+ return model if model.nil? || model.is_a?(String)
351
+ local_body = nil
352
+ if model.is_a?(Array)
353
+ local_body = model.map { |m| object_to_hash(m) }
354
+ else
355
+ local_body = object_to_hash(model)
356
+ end
357
+ local_body.to_json
358
+ end
359
+
360
+ # Convert object(non-array) to hash.
361
+ # @param [Object] obj object to be converted into JSON string
362
+ # @return [String] JSON string representation of the object
363
+ def object_to_hash(obj)
364
+ if obj.respond_to?(:to_hash)
365
+ obj.to_hash
366
+ else
367
+ obj
368
+ end
369
+ end
370
+
371
+ # Build parameter value according to the given collection format.
372
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
373
+ def build_collection_param(param, collection_format)
374
+ case collection_format
375
+ when :csv
376
+ param.join(',')
377
+ when :ssv
378
+ param.join(' ')
379
+ when :tsv
380
+ param.join("\t")
381
+ when :pipes
382
+ param.join('|')
383
+ when :multi
384
+ # return the array directly as typhoeus will handle it as expected
385
+ param
386
+ else
387
+ fail "unknown collection format: #{collection_format.inspect}"
388
+ end
389
+ end
390
+ end
391
+ end