test_kaferi_cloud 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/D://tmp//2018-02027//Ruby//.gitignore +42 -0
  3. data/D://tmp//2018-02027//Ruby//.idea//.rakeTasks +7 -0
  4. data/D://tmp//2018-02027//Ruby//.idea//inspectionProfiles//Project_Default.xml +6 -0
  5. data/D://tmp//2018-02027//Ruby//.idea//misc.xml +4 -0
  6. data/D://tmp//2018-02027//Ruby//.idea//modules.xml +8 -0
  7. data/D://tmp//2018-02027//Ruby//.idea//ruby.iml +15 -0
  8. data/D://tmp//2018-02027//Ruby//.idea//vcs.xml +6 -0
  9. data/D://tmp//2018-02027//Ruby//.idea//workspace.xml +776 -0
  10. data/D://tmp//2018-02027//Ruby//.swagger-codegen-ignore +58 -0
  11. data/D://tmp//2018-02027//Ruby//.swagger-codegen//VERSION +1 -0
  12. data/D://tmp//2018-02027//Ruby//LICENSE +21 -0
  13. data/D://tmp//2018-02027//Ruby//README.md +37 -0
  14. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud.rb +126 -0
  15. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//api//pdf_api.rb +4026 -0
  16. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//api_client.rb +523 -0
  17. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//api_error.rb +47 -0
  18. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//configuration.rb +189 -0
  19. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//annotation.rb +260 -0
  20. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//annotation_response.rb +222 -0
  21. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//annotations.rb +211 -0
  22. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//annotations_response.rb +222 -0
  23. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//append_document.rb +228 -0
  24. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//attachment.rb +270 -0
  25. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//attachment_response.rb +222 -0
  26. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//attachments.rb +211 -0
  27. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//attachments_response.rb +222 -0
  28. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//bookmarks.rb +211 -0
  29. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//bookmarks_response.rb +222 -0
  30. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//color.rb +244 -0
  31. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//document.rb +220 -0
  32. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//document_pages_response.rb +222 -0
  33. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//document_properties.rb +211 -0
  34. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//document_properties_response.rb +222 -0
  35. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//document_property.rb +227 -0
  36. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//document_property_response.rb +222 -0
  37. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//document_response.rb +233 -0
  38. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//document_text_replace_response.rb +231 -0
  39. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//field.rb +254 -0
  40. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//field_response.rb +222 -0
  41. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//field_type.rb +44 -0
  42. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//fields.rb +211 -0
  43. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//fields_response.rb +222 -0
  44. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//font_styles.rb +43 -0
  45. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//horizontal_alignment.rb +44 -0
  46. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//http_status_code.rb +87 -0
  47. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//image.rb +220 -0
  48. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//image_response.rb +222 -0
  49. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//images.rb +211 -0
  50. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//images_list_request.rb +205 -0
  51. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//images_response.rb +222 -0
  52. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//line_spacing.rb +43 -0
  53. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//link.rb +228 -0
  54. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//link_action_type.rb +46 -0
  55. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//link_annotation.rb +236 -0
  56. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//link_annotation_response.rb +222 -0
  57. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//link_annotations.rb +211 -0
  58. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//link_annotations_response.rb +222 -0
  59. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//link_element.rb +200 -0
  60. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//link_highlighting_mode.rb +45 -0
  61. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//merge_documents.rb +200 -0
  62. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//optimize_options.rb +293 -0
  63. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//page.rb +228 -0
  64. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//page_text_replace_response.rb +231 -0
  65. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//page_word_count.rb +218 -0
  66. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//pages.rb +211 -0
  67. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//paragraph.rb +303 -0
  68. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//rectangle.rb +244 -0
  69. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//rotation.rb +44 -0
  70. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//saa_spose_response.rb +213 -0
  71. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//segment.rb +206 -0
  72. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//signature.rb +333 -0
  73. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//signature_type.rb +42 -0
  74. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//split_result.rb +200 -0
  75. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//split_result_document.rb +238 -0
  76. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//split_result_response.rb +223 -0
  77. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//stamp.rb +413 -0
  78. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//stamp_type.rb +44 -0
  79. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//text_format.rb +227 -0
  80. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//text_format_response.rb +222 -0
  81. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//text_horizontal_alignment.rb +47 -0
  82. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//text_item.rb +218 -0
  83. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//text_item_response.rb +222 -0
  84. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//text_items.rb +211 -0
  85. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//text_items_response.rb +222 -0
  86. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//text_line.rb +210 -0
  87. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//text_rect.rb +220 -0
  88. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//text_rects.rb +199 -0
  89. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//text_rects_response.rb +222 -0
  90. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//text_replace.rb +253 -0
  91. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//text_replace_list_request.rb +232 -0
  92. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//text_replace_request.rb +262 -0
  93. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//text_replace_response.rb +222 -0
  94. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//text_state.rb +248 -0
  95. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//tiff_export_options.rb +348 -0
  96. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//vertical_alignment.rb +45 -0
  97. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//word_count.rb +199 -0
  98. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//word_count_response.rb +223 -0
  99. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//models//wrap_mode.rb +44 -0
  100. data/D://tmp//2018-02027//Ruby//lib//test_kaferi_cloud//version.rb +24 -0
  101. data/D://tmp//2018-02027//Ruby//test_kaferi_cloud.gemspec +49 -0
  102. metadata +102 -2
@@ -0,0 +1,523 @@
1
+ =begin
2
+ --------------------------------------------------------------------------------------------------------------------
3
+ Copyright (c) 2018 Aspose.Pdf for Cloud
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18
+ SOFTWARE.
19
+ --------------------------------------------------------------------------------------------------------------------
20
+ =end
21
+
22
+ require 'date'
23
+ require 'json'
24
+ require 'logger'
25
+ require 'tempfile'
26
+ require 'typhoeus'
27
+ require 'uri'
28
+ require 'openssl'
29
+ require 'base64'
30
+ require 'rexml/document'
31
+
32
+ module TestKaferiCloud
33
+ class ApiClient
34
+
35
+ include AsposeStorageCloud
36
+
37
+ # The Configuration object holding settings to be used in the API client.
38
+ attr_accessor :config
39
+
40
+ # Defines the headers to be used in HTTP requests of all API calls by default.
41
+ #
42
+ # @return [Hash]
43
+ attr_accessor :default_headers
44
+
45
+ # Initializes the ApiClient
46
+ # @option config [Configuration] Configuration for initializing the object, default to Configuration.default
47
+ def initialize(config = Configuration.default)
48
+ @config = config
49
+ @user_agent = "Swagger-Codegen/#{VERSION}/ruby"
50
+ @default_headers = {
51
+ 'Content-Type' => "application/json",
52
+ 'User-Agent' => @user_agent
53
+ }
54
+ end
55
+
56
+ def self.default
57
+ @@default ||= ApiClient.new
58
+ end
59
+
60
+ # Call an API with given options.
61
+ #
62
+ # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
63
+ # the data deserialized from response body (could be nil), response status code and response headers.
64
+ def call_api(http_method, path, opts = {})
65
+ request = build_request(http_method, path, opts)
66
+ response = request.run
67
+
68
+ if @config.debugging
69
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
70
+ end
71
+
72
+ unless response.success?
73
+ if response.timed_out?
74
+ fail ApiError.new('Connection timed out')
75
+ elsif response.code == 0
76
+ # Errors from libcurl will be made visible here
77
+ fail ApiError.new(:code => 0,
78
+ :message => response.return_message)
79
+ else
80
+ fail ApiError.new(:code => response.code,
81
+ :response_headers => response.headers,
82
+ :response_body => response.body),
83
+ response.status_message
84
+ end
85
+ end
86
+
87
+ if opts[:return_type]
88
+ data = deserialize(response, opts[:return_type])
89
+ else
90
+ data = nil
91
+ end
92
+ return data, response.code, response.headers
93
+ end
94
+
95
+ # Builds the HTTP request
96
+ #
97
+ # @param [String] http_method HTTP method/verb (e.g. POST)
98
+ # @param [String] path URL path (e.g. /account/new)
99
+ # @option opts [Hash] :header_params Header parameters
100
+ # @option opts [Hash] :query_params Query parameters
101
+ # @option opts [Hash] :form_params Query parameters
102
+ # @option opts [Object] :body HTTP body (JSON/XML)
103
+ # @return [Typhoeus::Request] A Typhoeus Request
104
+ def build_request(http_method, path, opts = {})
105
+ url = build_request_url(path)
106
+ http_method = http_method.to_sym.downcase
107
+
108
+ header_params = @default_headers.merge(opts[:header_params] || {})
109
+ query_params = {}
110
+ form_params = opts[:form_params] || {}
111
+
112
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
113
+
114
+ # set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
115
+ _verify_ssl_host = @config.verify_ssl_host ? 2 : 0
116
+
117
+ req_opts = {
118
+ :method => http_method,
119
+ :headers => header_params,
120
+ :params => query_params,
121
+ :params_encoding => @config.params_encoding,
122
+ :timeout => @config.timeout,
123
+ :ssl_verifypeer => @config.verify_ssl,
124
+ :ssl_verifyhost => _verify_ssl_host,
125
+ :sslcert => @config.cert_file,
126
+ :sslkey => @config.key_file,
127
+ :verbose => @config.debugging
128
+ }
129
+
130
+ # set custom cert, if provided
131
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
132
+
133
+ if [:post, :patch, :put, :delete].include?(http_method)
134
+ req_body = build_request_body(header_params, form_params, opts[:body])
135
+ req_opts.update :body => req_body
136
+ if @config.debugging
137
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
138
+ end
139
+ end
140
+
141
+ if @config.auth_type == Configuration::AUTH_TYPE_REQUEST_SIGNATURE
142
+ url = sign(url, opts[:query_params])
143
+ elsif @config.auth_type == Configuration::AUTH_TYPE_O_AUTH_2
144
+ # OAuth 2.0
145
+ req_opts[:params] = opts[:query_params]
146
+ if @config.access_token.nil?
147
+ request_token
148
+ end
149
+ add_o_auth_token(req_opts)
150
+ end
151
+
152
+ request = Typhoeus::Request.new(url, req_opts)
153
+ download_file(request) if opts[:return_type] == 'File'
154
+ request
155
+ end
156
+
157
+ # Signs a URI with your appSID and Key.
158
+ # * :url describes the URL to sign
159
+
160
+ def sign(url, query_params)
161
+
162
+ fail "Please set Aspose App key and SID first. You can get App key and App SID from https://cloud.aspose.com" if AsposeApp.app_key.nil? || AsposeApp.app_sid.nil?
163
+
164
+ url = url[0..-2] if url[-1].eql? '/'
165
+
166
+ unless query_params.empty?
167
+ url = "#{url}?"
168
+ query_params.each { |key, value|
169
+ url = "#{url}#{key}=#{value}&"
170
+ }
171
+ url = url[0..-2]
172
+ end
173
+
174
+ parsed_url = URI.parse(url)
175
+
176
+ url_to_sign = "#{parsed_url.scheme}://#{parsed_url.host}#{parsed_url.path}"
177
+ url_to_sign += "?#{parsed_url.query}" if parsed_url.query
178
+ if parsed_url.query
179
+ url_to_sign += "&appSID=#{AsposeApp.app_sid}"
180
+ else
181
+ url_to_sign += "?appSID=#{AsposeApp.app_sid}"
182
+ end
183
+
184
+ # create a signature using the private key and the URL
185
+ raw_signature = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), AsposeApp.app_key, url_to_sign)
186
+
187
+ #Convert raw to encoded string
188
+ signature = Base64.strict_encode64(raw_signature).tr('+/', '-_')
189
+
190
+ #remove invalid character
191
+ signature = signature.gsub(/[=_-]/, '=' => '', '_' => '%2f', '-' => '%2b')
192
+
193
+ #Define expression
194
+ pat = Regexp.new('%[0-9a-f]{2}')
195
+
196
+ #Replace the portion matched to the above pattern to upper case
197
+ 6.times do
198
+ signature = signature.sub(pat, pat.match(signature).to_s.upcase)
199
+ end
200
+
201
+ # prepend the server and append the signature.
202
+ url_to_sign + "&signature=#{signature}"
203
+
204
+ end
205
+
206
+ # Check if the given MIME is a JSON MIME.
207
+ # JSON MIME examples:
208
+ # application/json
209
+ # application/json; charset=UTF8
210
+ # APPLICATION/JSON
211
+ # */*
212
+ # @param [String] mime MIME
213
+ # @return [Boolean] True if the MIME is application/json
214
+ def json_mime?(mime)
215
+ (mime == "*/*") || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
216
+ end
217
+
218
+ # Deserialize the response to the given return type.
219
+ #
220
+ # @param [Response] response HTTP response
221
+ # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
222
+ def deserialize(response, return_type)
223
+ body = response.body
224
+
225
+ # handle file downloading - return the File instance processed in request callbacks
226
+ # note that response body is empty when the file is written in chunks in request on_body callback
227
+ return @tempfile if return_type == 'File'
228
+
229
+ return nil if body.nil? || body.empty?
230
+
231
+ # return response body directly for String return type
232
+ return body if return_type == 'String'
233
+
234
+ # ensuring a default content type
235
+ content_type = response.headers['Content-Type'] || 'application/json'
236
+
237
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
238
+
239
+ begin
240
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
241
+ rescue JSON::ParserError => e
242
+ if %w(String Date DateTime).include?(return_type)
243
+ data = body
244
+ else
245
+ raise e
246
+ end
247
+ end
248
+
249
+ convert_to_type data, return_type
250
+ end
251
+
252
+ # Convert data to the given return type.
253
+ # @param [Object] data Data to be converted
254
+ # @param [String] return_type Return type
255
+ # @return [Mixed] Data in a particular type
256
+ def convert_to_type(data, return_type)
257
+ return nil if data.nil?
258
+ case return_type
259
+ when 'String'
260
+ data.to_s
261
+ when 'Integer'
262
+ data.to_i
263
+ when 'Float'
264
+ data.to_f
265
+ when 'BOOLEAN'
266
+ data == true
267
+ when 'DateTime'
268
+ # parse date time (expecting ISO 8601 format)
269
+ DateTime.parse data
270
+ when 'Date'
271
+ # parse date time (expecting ISO 8601 format)
272
+ Date.parse data
273
+ when 'Object'
274
+ # generic object (usually a Hash), return directly
275
+ data
276
+ when /\AArray<(.+)>\z/
277
+ # e.g. Array<Pet>
278
+ sub_type = $1
279
+ data.map {|item| convert_to_type(item, sub_type) }
280
+ when /\AHash\<String, (.+)\>\z/
281
+ # e.g. Hash<String, Integer>
282
+ sub_type = $1
283
+ {}.tap do |hash|
284
+ data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
285
+ end
286
+ else
287
+ # models, e.g. Pet
288
+ TestKaferiCloud.const_get(return_type).new.tap do |model|
289
+ model.build_from_hash data
290
+ end
291
+ end
292
+ end
293
+
294
+ # Save response body into a file in (the defined) temporary folder, using the filename
295
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
296
+ # The response body is written to the file in chunks in order to handle files which
297
+ # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
298
+ # process can use.
299
+ #
300
+ # @see Configuration#temp_folder_path
301
+ def download_file(request)
302
+ tempfile = nil
303
+ encoding = nil
304
+ request.on_headers do |response|
305
+ content_disposition = response.headers['Content-Disposition']
306
+ if content_disposition and content_disposition =~ /filename=/i
307
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
308
+ prefix = sanitize_filename(filename)
309
+ else
310
+ prefix = 'download-'
311
+ end
312
+ prefix = prefix + '-' unless prefix.end_with?('-')
313
+ encoding = response.body.encoding
314
+ tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
315
+ @tempfile = tempfile
316
+ end
317
+ request.on_body do |chunk|
318
+ chunk.force_encoding(encoding)
319
+ tempfile.write(chunk)
320
+ end
321
+ request.on_complete do |response|
322
+ tempfile.close
323
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
324
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
325
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
326
+ "explicitly with `tempfile.delete`"
327
+ end
328
+ end
329
+
330
+ # Sanitize filename by removing path.
331
+ # e.g. ../../sun.gif becomes sun.gif
332
+ #
333
+ # @param [String] filename the filename to be sanitized
334
+ # @return [String] the sanitized filename
335
+ def sanitize_filename(filename)
336
+ filename.gsub(/.*[\/\\]/, '')
337
+ end
338
+
339
+ def build_request_url(path)
340
+ # Add leading and trailing slashes to path
341
+ path = "/#{path}".gsub(/\/+/, '/')
342
+ URI.encode(@config.base_url + path)
343
+ end
344
+
345
+ # Builds the HTTP request body
346
+ #
347
+ # @param [Hash] header_params Header parameters
348
+ # @param [Hash] form_params Query parameters
349
+ # @param [Object] body HTTP body (JSON/XML)
350
+ # @return [String] HTTP body data in the form of string
351
+ def build_request_body(header_params, form_params, body)
352
+ # http form
353
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
354
+ header_params['Content-Type'] == 'multipart/form-data'
355
+ data = {}
356
+ form_params.each do |key, value|
357
+ case value
358
+ when File, Array, nil
359
+ # let typhoeus handle File, Array and nil parameters
360
+ data[key] = value
361
+ else
362
+ data[key] = value.to_s
363
+ end
364
+ end
365
+ elsif body
366
+ data = body.is_a?(String) ? body : body.to_json
367
+ else
368
+ data = nil
369
+ end
370
+ data
371
+ end
372
+
373
+ # Update hearder and query params based on authentication settings.
374
+ #
375
+ # @param [Hash] header_params Header parameters
376
+ # @param [Hash] query_params Query parameters
377
+ # @param [String] auth_names Authentication scheme name
378
+ def update_params_for_auth!(header_params, query_params, auth_names)
379
+ Array(auth_names).each do |auth_name|
380
+ auth_setting = @config.auth_settings[auth_name]
381
+ next unless auth_setting
382
+ case auth_setting[:in]
383
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
384
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
385
+ else fail ArgumentError, 'Authentication token must be in `query` of `header`'
386
+ end
387
+ end
388
+ end
389
+
390
+ # Sets user agent in HTTP header
391
+ #
392
+ # @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
393
+ def user_agent=(user_agent)
394
+ @user_agent = user_agent
395
+ @default_headers['User-Agent'] = @user_agent
396
+ end
397
+
398
+ # Return Accept header based on an array of accepts provided.
399
+ # @param [Array] accepts array for Accept
400
+ # @return [String] the Accept header (e.g. application/json)
401
+ def select_header_accept(accepts)
402
+ return nil if accepts.nil? || accepts.empty?
403
+ # use JSON when present, otherwise use all of the provided
404
+ json_accept = accepts.find { |s| json_mime?(s) }
405
+ return json_accept || accepts.join(',')
406
+ end
407
+
408
+ # Return Content-Type header based on an array of content types provided.
409
+ # @param [Array] content_types array for Content-Type
410
+ # @return [String] the Content-Type header (e.g. application/json)
411
+ def select_header_content_type(content_types)
412
+ # use application/json by default
413
+ return 'application/json' if content_types.nil? || content_types.empty?
414
+ # use JSON when present, otherwise use the first one
415
+ json_content_type = content_types.find { |s| json_mime?(s) }
416
+ return json_content_type || content_types.first
417
+ end
418
+
419
+ # Convert object (array, hash, object, etc) to JSON string.
420
+ # @param [Object] model object to be converted into JSON string
421
+ # @return [String] JSON string representation of the object
422
+ def object_to_http_body(model)
423
+ return model if model.nil? || model.is_a?(String)
424
+ local_body = nil
425
+ if model.is_a?(Array)
426
+ local_body = model.map{|m| object_to_hash(m) }
427
+ else
428
+ local_body = object_to_hash(model)
429
+ end
430
+ local_body.to_json
431
+ end
432
+
433
+ # Convert object(non-array) to hash.
434
+ # @param [Object] obj object to be converted into JSON string
435
+ # @return [String] JSON string representation of the object
436
+ def object_to_hash(obj)
437
+ if obj.respond_to?(:to_hash)
438
+ obj.to_hash
439
+ else
440
+ obj
441
+ end
442
+ end
443
+
444
+ # Build parameter value according to the given collection format.
445
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
446
+ def build_collection_param(param, collection_format)
447
+ case collection_format
448
+ when :csv
449
+ param.join(',')
450
+ when :ssv
451
+ param.join(' ')
452
+ when :tsv
453
+ param.join("\t")
454
+ when :pipes
455
+ param.join('|')
456
+ when :multi
457
+ # return the array directly as typhoeus will handle it as expected
458
+ param
459
+ else
460
+ fail "unknown collection format: #{collection_format.inspect}"
461
+ end
462
+ end
463
+
464
+ # Request access and refresh tokens
465
+ def request_token
466
+ # resource path
467
+ local_var_path = "/oauth2/token"
468
+
469
+ # query parameters
470
+ query_params = {}
471
+
472
+ # header parameters
473
+ header_params = {}
474
+ # HTTP header 'Content-Type'
475
+ header_params['Content-Type'] = select_header_content_type(['application/x-www-form-urlencoded'])
476
+
477
+ # form parameters
478
+ form_params = {}
479
+ form_params["grant_type"] = 'client_credentials'
480
+ form_params["client_id"] = AsposeApp.app_sid
481
+ form_params["client_secret"] = AsposeApp.app_key
482
+
483
+
484
+ url = build_request_url(local_var_path).gsub(@config.base_path, '')
485
+ http_method = :POST.to_sym.downcase
486
+
487
+
488
+ # set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
489
+ _verify_ssl_host = @config.verify_ssl_host ? 2 : 0
490
+
491
+ req_opts = {
492
+ :method => http_method,
493
+ :headers => header_params,
494
+ :params => query_params,
495
+ :params_encoding => @config.params_encoding,
496
+ :timeout => @config.timeout,
497
+ :ssl_verifypeer => @config.verify_ssl,
498
+ :ssl_verifyhost => _verify_ssl_host,
499
+ :sslcert => @config.cert_file,
500
+ :sslkey => @config.key_file,
501
+ :verbose => @config.debugging,
502
+ :body => form_params
503
+ }
504
+
505
+ # set custom cert, if provided
506
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
507
+
508
+ request = Typhoeus::Request.new(url, req_opts)
509
+ response = request.run
510
+
511
+ data = JSON.parse("[#{response.body}]", :symbolize_names => true)[0]
512
+
513
+ @config.access_token = data[:access_token]
514
+ @config.refresh_token = data[:refresh_token]
515
+ end
516
+
517
+ # Adds OAuth2.0 token
518
+ def add_o_auth_token(req_opts)
519
+ req_opts[:headers][:Authorization] = "Bearer " + @config.access_token
520
+ end
521
+
522
+ end
523
+ end