aspose_pdf_cloud 1.0.2 → 18.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +42 -0
  3. data/.swagger-codegen-ignore +58 -0
  4. data/.swagger-codegen/VERSION +1 -0
  5. data/LICENSE +21 -22
  6. data/README.md +51 -43
  7. data/aspose_pdf_cloud.gemspec +32 -8
  8. data/lib/aspose_pdf_cloud.rb +93 -58
  9. data/lib/aspose_pdf_cloud/api/pdf_api.rb +2624 -1916
  10. data/lib/aspose_pdf_cloud/api_client.rb +280 -92
  11. data/lib/aspose_pdf_cloud/api_error.rb +29 -6
  12. data/lib/aspose_pdf_cloud/configuration.rb +86 -63
  13. data/lib/aspose_pdf_cloud/models/annotation.rb +218 -45
  14. data/lib/aspose_pdf_cloud/models/annotation_response.rb +195 -34
  15. data/lib/aspose_pdf_cloud/models/annotations.rb +187 -25
  16. data/lib/aspose_pdf_cloud/models/annotations_response.rb +195 -34
  17. data/lib/aspose_pdf_cloud/models/append_document.rb +198 -23
  18. data/lib/aspose_pdf_cloud/models/attachment.rb +224 -49
  19. data/lib/aspose_pdf_cloud/models/attachment_response.rb +195 -34
  20. data/lib/aspose_pdf_cloud/models/attachments.rb +187 -25
  21. data/lib/aspose_pdf_cloud/models/attachments_response.rb +195 -34
  22. data/lib/aspose_pdf_cloud/models/bookmarks.rb +187 -25
  23. data/lib/aspose_pdf_cloud/models/bookmarks_response.rb +195 -34
  24. data/lib/aspose_pdf_cloud/models/color.rb +214 -31
  25. data/lib/aspose_pdf_cloud/models/document.rb +194 -29
  26. data/lib/aspose_pdf_cloud/models/document_pages_response.rb +195 -34
  27. data/lib/aspose_pdf_cloud/models/document_properties.rb +187 -25
  28. data/lib/aspose_pdf_cloud/models/document_properties_response.rb +195 -34
  29. data/lib/aspose_pdf_cloud/models/document_property.rb +197 -33
  30. data/lib/aspose_pdf_cloud/models/document_property_response.rb +195 -34
  31. data/lib/aspose_pdf_cloud/models/document_response.rb +206 -34
  32. data/lib/aspose_pdf_cloud/models/document_text_replace_response.rb +203 -41
  33. data/lib/aspose_pdf_cloud/models/field.rb +213 -42
  34. data/lib/aspose_pdf_cloud/models/field_response.rb +195 -34
  35. data/lib/aspose_pdf_cloud/models/field_type.rb +44 -0
  36. data/lib/aspose_pdf_cloud/models/fields.rb +187 -25
  37. data/lib/aspose_pdf_cloud/models/fields_response.rb +195 -34
  38. data/lib/aspose_pdf_cloud/models/font_styles.rb +43 -0
  39. data/lib/aspose_pdf_cloud/models/horizontal_alignment.rb +44 -0
  40. data/lib/aspose_pdf_cloud/models/http_status_code.rb +87 -0
  41. data/lib/aspose_pdf_cloud/models/image.rb +194 -29
  42. data/lib/aspose_pdf_cloud/models/image_response.rb +195 -34
  43. data/lib/aspose_pdf_cloud/models/images.rb +187 -25
  44. data/lib/aspose_pdf_cloud/models/images_list_request.rb +205 -0
  45. data/lib/aspose_pdf_cloud/models/images_response.rb +195 -34
  46. data/lib/aspose_pdf_cloud/models/line_spacing.rb +43 -0
  47. data/lib/aspose_pdf_cloud/models/link.rb +194 -27
  48. data/lib/aspose_pdf_cloud/models/link_action_type.rb +46 -0
  49. data/lib/aspose_pdf_cloud/models/link_annotation.rb +204 -39
  50. data/lib/aspose_pdf_cloud/models/link_annotation_response.rb +195 -34
  51. data/lib/aspose_pdf_cloud/models/link_annotations.rb +187 -25
  52. data/lib/aspose_pdf_cloud/models/link_annotations_response.rb +195 -34
  53. data/lib/aspose_pdf_cloud/models/link_element.rb +175 -14
  54. data/lib/aspose_pdf_cloud/models/link_highlighting_mode.rb +45 -0
  55. data/lib/aspose_pdf_cloud/models/merge_documents.rb +175 -14
  56. data/lib/aspose_pdf_cloud/models/optimize_options.rb +293 -0
  57. data/lib/aspose_pdf_cloud/models/page.rb +198 -33
  58. data/lib/aspose_pdf_cloud/models/page_text_replace_response.rb +203 -41
  59. data/lib/aspose_pdf_cloud/models/page_word_count.rb +192 -19
  60. data/lib/aspose_pdf_cloud/models/pages.rb +187 -25
  61. data/lib/aspose_pdf_cloud/models/paragraph.rb +303 -0
  62. data/lib/aspose_pdf_cloud/models/rectangle.rb +210 -27
  63. data/lib/aspose_pdf_cloud/models/rotation.rb +44 -0
  64. data/lib/aspose_pdf_cloud/models/saa_spose_response.rb +213 -0
  65. data/lib/aspose_pdf_cloud/models/segment.rb +206 -0
  66. data/lib/aspose_pdf_cloud/models/signature.rb +278 -54
  67. data/lib/aspose_pdf_cloud/models/signature_type.rb +42 -0
  68. data/lib/aspose_pdf_cloud/models/split_result.rb +175 -14
  69. data/lib/aspose_pdf_cloud/models/split_result_document.rb +206 -37
  70. data/lib/aspose_pdf_cloud/models/split_result_response.rb +196 -34
  71. data/lib/aspose_pdf_cloud/models/stamp.rb +312 -104
  72. data/lib/aspose_pdf_cloud/models/stamp_type.rb +44 -0
  73. data/lib/aspose_pdf_cloud/models/text_format.rb +197 -33
  74. data/lib/aspose_pdf_cloud/models/text_format_response.rb +195 -34
  75. data/lib/aspose_pdf_cloud/models/text_horizontal_alignment.rb +47 -0
  76. data/lib/aspose_pdf_cloud/models/text_item.rb +192 -29
  77. data/lib/aspose_pdf_cloud/models/text_item_response.rb +195 -34
  78. data/lib/aspose_pdf_cloud/models/text_items.rb +187 -25
  79. data/lib/aspose_pdf_cloud/models/text_items_response.rb +195 -34
  80. data/lib/aspose_pdf_cloud/models/text_line.rb +210 -0
  81. data/lib/aspose_pdf_cloud/models/text_rect.rb +220 -0
  82. data/lib/aspose_pdf_cloud/models/text_rects.rb +199 -0
  83. data/lib/aspose_pdf_cloud/models/text_rects_response.rb +222 -0
  84. data/lib/aspose_pdf_cloud/models/text_replace.rb +225 -25
  85. data/lib/aspose_pdf_cloud/models/text_replace_list_request.rb +209 -16
  86. data/lib/aspose_pdf_cloud/models/text_replace_request.rb +262 -0
  87. data/lib/aspose_pdf_cloud/models/text_replace_response.rb +222 -0
  88. data/lib/aspose_pdf_cloud/models/text_state.rb +211 -32
  89. data/lib/aspose_pdf_cloud/models/tiff_export_options.rb +266 -75
  90. data/lib/aspose_pdf_cloud/models/vertical_alignment.rb +45 -0
  91. data/lib/aspose_pdf_cloud/models/word_count.rb +174 -14
  92. data/lib/aspose_pdf_cloud/models/word_count_response.rb +196 -34
  93. data/lib/aspose_pdf_cloud/models/wrap_mode.rb +44 -0
  94. data/lib/aspose_pdf_cloud/version.rb +23 -2
  95. data/test/pdf_tests.rb +1268 -623
  96. data/test_data/33539.jpg +0 -0
  97. data/test_data/44781.jpg +0 -0
  98. data/test_data/4pages.pdf +0 -0
  99. data/test_data/BlankWithSignature.pdf +0 -0
  100. data/test_data/Hello_world.pdf +0 -0
  101. data/test_data/HtmlExample1.html +27 -0
  102. data/test_data/Koala.jpg +0 -0
  103. data/test_data/PdfWithAcroForm.pdf +321 -0
  104. data/test_data/PdfWithAnnotations.pdf +720 -1
  105. data/test_data/PdfWithBookmarks.pdf +0 -0
  106. data/test_data/PdfWithEmbeddedFiles.pdf +0 -0
  107. data/test_data/PdfWithImages2.pdf +0 -0
  108. data/test_data/PdfWithLinks.pdf +0 -0
  109. data/test_data/Penguins.jpg +0 -0
  110. data/test_data/butterfly.jpg +0 -0
  111. data/test_data/marketing.pdf +0 -0
  112. data/test_data/rusdoc.pdf +0 -0
  113. data/test_data/test1234.pfx +0 -0
  114. metadata +54 -13
  115. data/lib/aspose_pdf_cloud/models/barcode_response_list.rb +0 -45
  116. data/lib/aspose_pdf_cloud/models/base_object.rb +0 -86
  117. data/lib/aspose_pdf_cloud/models/base_response.rb +0 -61
  118. data/lib/aspose_pdf_cloud/models/page_response.rb +0 -61
@@ -1,3 +1,24 @@
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
+
1
22
  require 'date'
2
23
  require 'json'
3
24
  require 'logger'
@@ -8,93 +29,136 @@ require 'openssl'
8
29
  require 'base64'
9
30
  require 'rexml/document'
10
31
 
11
- module AsposePDFCloud
32
+ module AsposePdfCloud
12
33
  class ApiClient
13
-
34
+
14
35
  include AsposeStorageCloud
15
-
16
- attr_accessor :host
36
+
37
+ # The Configuration object holding settings to be used in the API client.
38
+ attr_accessor :config
17
39
 
18
40
  # Defines the headers to be used in HTTP requests of all API calls by default.
19
41
  #
20
42
  # @return [Hash]
21
43
  attr_accessor :default_headers
22
44
 
23
- # Stores the HTTP response from the last API call using this API client.
24
- attr_accessor :last_response
25
-
26
- def initialize(host = nil)
27
- @host = host || Configuration.base_url
28
- @format = 'json'
29
- @user_agent = "ruby-swagger-#{VERSION}"
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"
30
50
  @default_headers = {
31
- 'Content-Type' => "application/#{@format.downcase}",
51
+ 'Content-Type' => "application/json",
32
52
  'User-Agent' => @user_agent
33
53
  }
34
54
  end
35
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.
36
64
  def call_api(http_method, path, opts = {})
37
65
  request = build_request(http_method, path, opts)
38
66
  response = request.run
39
67
 
40
- # record as last response
41
- @last_response = response
42
-
43
- if Configuration.debugging
44
- Configuration.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
68
+ if @config.debugging
69
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
45
70
  end
46
71
 
47
72
  unless response.success?
48
- fail ApiError.new(:code => response.code,
49
- :response_headers => response.headers,
50
- :response_body => response.body),
51
- response.status_message
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
52
85
  end
53
86
 
54
87
  if opts[:return_type]
55
- deserialize(response, opts[:return_type])
88
+ data = deserialize(response, opts[:return_type])
56
89
  else
57
- nil
90
+ data = nil
58
91
  end
92
+ return data, response.code, response.headers
59
93
  end
60
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
61
104
  def build_request(http_method, path, opts = {})
62
105
  url = build_request_url(path)
63
106
  http_method = http_method.to_sym.downcase
64
-
107
+
65
108
  header_params = @default_headers.merge(opts[:header_params] || {})
66
109
  query_params = {}
67
110
  form_params = opts[:form_params] || {}
68
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
+
69
117
  req_opts = {
70
118
  :method => http_method,
71
119
  :headers => header_params,
72
120
  :params => query_params,
73
- :ssl_verifypeer => Configuration.verify_ssl,
74
- :sslcert => Configuration.cert_file,
75
- :sslkey => Configuration.key_file,
76
- :cainfo => Configuration.ssl_ca_cert,
77
- :verbose => Configuration.debugging
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
78
128
  }
79
129
 
130
+ # set custom cert, if provided
131
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
132
+
80
133
  if [:post, :patch, :put, :delete].include?(http_method)
81
134
  req_body = build_request_body(header_params, form_params, opts[:body])
82
135
  req_opts.update :body => req_body
83
- if Configuration.debugging
84
- Configuration.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
136
+ if @config.debugging
137
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
85
138
  end
86
139
  end
87
-
88
- url = sign(url, opts[:query_params])
89
- Typhoeus::Request.new(url, req_opts)
90
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
91
155
  end
92
-
156
+
93
157
  # Signs a URI with your appSID and Key.
94
158
  # * :url describes the URL to sign
95
159
 
96
160
  def sign(url, query_params)
97
-
161
+
98
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?
99
163
 
100
164
  url = url[0..-2] if url[-1].eql? '/'
@@ -106,10 +170,9 @@ module AsposePDFCloud
106
170
  }
107
171
  url = url[0..-2]
108
172
  end
109
-
110
- url = URI.escape(url)
173
+
111
174
  parsed_url = URI.parse(url)
112
-
175
+
113
176
  url_to_sign = "#{parsed_url.scheme}://#{parsed_url.host}#{parsed_url.path}"
114
177
  url_to_sign += "?#{parsed_url.query}" if parsed_url.query
115
178
  if parsed_url.query
@@ -139,23 +202,39 @@ module AsposePDFCloud
139
202
  url_to_sign + "&signature=#{signature}"
140
203
 
141
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
142
217
 
143
218
  # Deserialize the response to the given return type.
144
219
  #
220
+ # @param [Response] response HTTP response
145
221
  # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
146
222
  def deserialize(response, return_type)
147
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
+
148
229
  return nil if body.nil? || body.empty?
149
230
 
150
- # handle file downloading - save response body into a tmp file and return the File instance
151
- return download_file(response) if return_type == 'File'
231
+ # return response body directly for String return type
232
+ return body if return_type == 'String'
152
233
 
153
234
  # ensuring a default content type
154
235
  content_type = response.headers['Content-Type'] || 'application/json'
155
236
 
156
- unless content_type.start_with?('application/json')
157
- fail "Content-Type is not supported: #{content_type}"
158
- end
237
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
159
238
 
160
239
  begin
161
240
  data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
@@ -171,6 +250,9 @@ module AsposePDFCloud
171
250
  end
172
251
 
173
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
174
256
  def convert_to_type(data, return_type)
175
257
  return nil if data.nil?
176
258
  case return_type
@@ -189,7 +271,7 @@ module AsposePDFCloud
189
271
  # parse date time (expecting ISO 8601 format)
190
272
  Date.parse data
191
273
  when 'Object'
192
- # generic object, return directly
274
+ # generic object (usually a Hash), return directly
193
275
  data
194
276
  when /\AArray<(.+)>\z/
195
277
  # e.g. Array<Pet>
@@ -203,7 +285,7 @@ module AsposePDFCloud
203
285
  end
204
286
  else
205
287
  # models, e.g. Pet
206
- AsposePDFCloud.const_get(return_type).new.tap do |model|
288
+ AsposePdfCloud.const_get(return_type).new.tap do |model|
207
289
  model.build_from_hash data
208
290
  end
209
291
  end
@@ -211,46 +293,73 @@ module AsposePDFCloud
211
293
 
212
294
  # Save response body into a file in (the defined) temporary folder, using the filename
213
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.
214
299
  #
215
300
  # @see Configuration#temp_folder_path
216
- # @return [File] the file downloaded
217
- def download_file(response)
218
- tmp_file = Tempfile.new '', Configuration.temp_folder_path
219
- content_disposition = response.headers['Content-Disposition']
220
-
221
- if content_disposition
222
- filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
223
- path = File.join File.dirname(tmp_file), filename
224
- else
225
- path = tmp_file.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
226
316
  end
227
- # close and delete temp file
228
- tmp_file.close!
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
229
329
 
230
- File.open(path, 'wb') { |file| file.write(response.body) }
231
- Configuration.logger.info "File written to #{path}. Please move the file to a proper "\
232
- "folder for further processing and delete the temp afterwards"
233
- File.new(path)
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(/.*[\/\\]/, '')
234
337
  end
235
338
 
236
339
  def build_request_url(path)
237
340
  # Add leading and trailing slashes to path
238
341
  path = "/#{path}".gsub(/\/+/, '/')
239
- URI.encode(host + path)
342
+ URI.encode(@config.base_url + path)
240
343
  end
241
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
242
351
  def build_request_body(header_params, form_params, body)
243
352
  # http form
244
353
  if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
245
354
  header_params['Content-Type'] == 'multipart/form-data'
246
-
247
- data = form_params.dup
248
- data.each do |key, value|
249
- if key.eql? "file"
250
- data = value
251
- break
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
252
361
  else
253
- data[key] = value.to_s if value && !value.is_a?(File)
362
+ data[key] = value.to_s
254
363
  end
255
364
  end
256
365
  elsif body
@@ -262,9 +371,13 @@ module AsposePDFCloud
262
371
  end
263
372
 
264
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
265
378
  def update_params_for_auth!(header_params, query_params, auth_names)
266
379
  Array(auth_names).each do |auth_name|
267
- auth_setting = Configuration.auth_settings[auth_name]
380
+ auth_setting = @config.auth_settings[auth_name]
268
381
  next unless auth_setting
269
382
  case auth_setting[:in]
270
383
  when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
@@ -274,6 +387,9 @@ module AsposePDFCloud
274
387
  end
275
388
  end
276
389
 
390
+ # Sets user agent in HTTP header
391
+ #
392
+ # @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
277
393
  def user_agent=(user_agent)
278
394
  @user_agent = user_agent
279
395
  @default_headers['User-Agent'] = @user_agent
@@ -283,40 +399,35 @@ module AsposePDFCloud
283
399
  # @param [Array] accepts array for Accept
284
400
  # @return [String] the Accept header (e.g. application/json)
285
401
  def select_header_accept(accepts)
286
- if accepts.empty?
287
- return
288
- elsif accepts.any?{ |s| s.casecmp('application/json') == 0 }
289
- 'application/json' # look for json data by default
290
- else
291
- accepts.join(',')
292
- end
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(',')
293
406
  end
294
407
 
295
408
  # Return Content-Type header based on an array of content types provided.
296
409
  # @param [Array] content_types array for Content-Type
297
410
  # @return [String] the Content-Type header (e.g. application/json)
298
411
  def select_header_content_type(content_types)
299
- if content_types.empty?
300
- 'application/json' # use application/json by default
301
- elsif content_types.any?{ |s| s.casecmp('application/json')==0 }
302
- 'application/json' # use application/json if it's included
303
- else
304
- content_types[0] # otherwise, use the first one
305
- end
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
306
417
  end
307
418
 
308
419
  # Convert object (array, hash, object, etc) to JSON string.
309
420
  # @param [Object] model object to be converted into JSON string
310
421
  # @return [String] JSON string representation of the object
311
422
  def object_to_http_body(model)
312
- return if model.nil?
313
- _body = nil
423
+ return model if model.nil? || model.is_a?(String)
424
+ local_body = nil
314
425
  if model.is_a?(Array)
315
- _body = model.map{|m| object_to_hash(m) }
426
+ local_body = model.map{|m| object_to_hash(m) }
316
427
  else
317
- _body = object_to_hash(model)
428
+ local_body = object_to_hash(model)
318
429
  end
319
- _body.to_json
430
+ local_body.to_json
320
431
  end
321
432
 
322
433
  # Convert object(non-array) to hash.
@@ -329,7 +440,84 @@ module AsposePDFCloud
329
440
  obj
330
441
  end
331
442
  end
332
-
333
-
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
+
334
522
  end
335
523
  end