docraptor 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 520005260c4eb699f6ac0c8b223def5e9624e57d
4
- data.tar.gz: 6a976eea99f53053665f0f4995b690e606c3ecc0
3
+ metadata.gz: f5a1c61577a464a76a8c99838bf03671dacf4868
4
+ data.tar.gz: fd38c94ea003066f08f23bbecda331d12f3d942e
5
5
  SHA512:
6
- metadata.gz: eaa8cd06bd4ef88d656e7342941684c38513edeb6c33851aec486b18815c829edd436df1fea2d35adf491cd4a5afb51e04418bc10dbc6caea9b0c990a5c5a75a
7
- data.tar.gz: 109acdcb676b246f7124371d8a5b4dcf1bb5bcf2dc2d223862a7135951c546f24ee49e150f9e564f6ab66f11ef216f3ba8c157fd3404e0166bbde90d6d785f9d
6
+ metadata.gz: cb72fe8bf315cde69177f0277ee357b995cb03f4b26a1e69d9b4f5149bb6a3299ec5106e54c50296cd999f9a1bb9eff8027526e6f18731269e0e623103a9ff60
7
+ data.tar.gz: a9ad7e0ff6509865334d9294f560130a6011d4e6ac2dd2600f9712fcbdb35f13c5de7d91feefe30ee69c66f2b9a3312cff75c71092ae9c7e5ff8bb1266e60375
@@ -5,7 +5,6 @@ require 'docraptor/version'
5
5
  require 'docraptor/configuration'
6
6
 
7
7
  # Models
8
- require 'docraptor/models/base_object'
9
8
  require 'docraptor/models/doc'
10
9
  require 'docraptor/models/prince_options'
11
10
  require 'docraptor/models/async_doc'
@@ -16,17 +15,17 @@ require 'docraptor/api/client_api'
16
15
 
17
16
  module DocRaptor
18
17
  class << self
19
- # Configure sdk using block.
20
- # DocRaptor.configure do |config|
21
- # config.username = "xxx"
22
- # config.password = "xxx"
23
- # end
24
- # If no block given, return the configuration singleton instance.
18
+ # Customize default settings for the SDK using block.
19
+ # DocRaptor.configure do |config|
20
+ # config.username = "xxx"
21
+ # config.password = "xxx"
22
+ # end
23
+ # If no block given, return the default Configuration object.
25
24
  def configure
26
25
  if block_given?
27
- yield Configuration.instance
26
+ yield(Configuration.default)
28
27
  else
29
- Configuration.instance
28
+ Configuration.default
30
29
  end
31
30
  end
32
31
  end
@@ -4,8 +4,8 @@ module DocRaptor
4
4
  class ClientApi
5
5
  attr_accessor :api_client
6
6
 
7
- def initialize(api_client = nil)
8
- @api_client = api_client || Configuration.api_client
7
+ def initialize(api_client = ApiClient.default)
8
+ @api_client = api_client
9
9
  end
10
10
 
11
11
  #
@@ -14,8 +14,18 @@ module DocRaptor
14
14
  # @param [Hash] opts the optional parameters
15
15
  # @return [AsyncDoc]
16
16
  def async_docs_post(doc, opts = {})
17
- if Configuration.debugging
18
- Configuration.logger.debug "Calling API: ClientApi#async_docs_post ..."
17
+ data, status_code, headers = async_docs_post_with_http_info(doc, opts)
18
+ return data
19
+ end
20
+
21
+ #
22
+ # Creates a document asynchronously.\nYou must use a callback url or the the returned status id and the status api to find out when it completes. Then use the download api to get the document.
23
+ # @param doc The document to be created.
24
+ # @param [Hash] opts the optional parameters
25
+ # @return [Array<(AsyncDoc, Fixnum, Hash)>] AsyncDoc data, response status code and response headers
26
+ def async_docs_post_with_http_info(doc, opts = {})
27
+ if @api_client.config.debugging
28
+ @api_client.config.logger.debug "Calling API: ClientApi#async_docs_post ..."
19
29
  end
20
30
 
21
31
  # verify the required parameter 'doc' is set
@@ -46,17 +56,17 @@ module DocRaptor
46
56
 
47
57
 
48
58
  auth_names = ['basicAuth']
49
- result = @api_client.call_api(:POST, path,
59
+ data, status_code, headers = @api_client.call_api(:POST, path,
50
60
  :header_params => header_params,
51
61
  :query_params => query_params,
52
62
  :form_params => form_params,
53
63
  :body => post_body,
54
64
  :auth_names => auth_names,
55
65
  :return_type => 'AsyncDoc')
56
- if Configuration.debugging
57
- Configuration.logger.debug "API called: ClientApi#async_docs_post. Result: #{result.inspect}"
66
+ if @api_client.config.debugging
67
+ @api_client.config.logger.debug "API called: ClientApi#async_docs_post\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
58
68
  end
59
- return result
69
+ return data, status_code, headers
60
70
  end
61
71
 
62
72
  #
@@ -65,8 +75,18 @@ module DocRaptor
65
75
  # @param [Hash] opts the optional parameters
66
76
  # @return [File]
67
77
  def docs_post(doc, opts = {})
68
- if Configuration.debugging
69
- Configuration.logger.debug "Calling API: ClientApi#docs_post ..."
78
+ data, status_code, headers = docs_post_with_http_info(doc, opts)
79
+ return data
80
+ end
81
+
82
+ #
83
+ # Creates a document synchronously.
84
+ # @param doc The document to be created.
85
+ # @param [Hash] opts the optional parameters
86
+ # @return [Array<(File, Fixnum, Hash)>] File data, response status code and response headers
87
+ def docs_post_with_http_info(doc, opts = {})
88
+ if @api_client.config.debugging
89
+ @api_client.config.logger.debug "Calling API: ClientApi#docs_post ..."
70
90
  end
71
91
 
72
92
  # verify the required parameter 'doc' is set
@@ -97,17 +117,17 @@ module DocRaptor
97
117
 
98
118
 
99
119
  auth_names = ['basicAuth']
100
- result = @api_client.call_api(:POST, path,
120
+ data, status_code, headers = @api_client.call_api(:POST, path,
101
121
  :header_params => header_params,
102
122
  :query_params => query_params,
103
123
  :form_params => form_params,
104
124
  :body => post_body,
105
125
  :auth_names => auth_names,
106
126
  :return_type => 'File')
107
- if Configuration.debugging
108
- Configuration.logger.debug "API called: ClientApi#docs_post. Result: #{result.inspect}"
127
+ if @api_client.config.debugging
128
+ @api_client.config.logger.debug "API called: ClientApi#docs_post\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
109
129
  end
110
- return result
130
+ return data, status_code, headers
111
131
  end
112
132
 
113
133
  #
@@ -116,8 +136,18 @@ module DocRaptor
116
136
  # @param [Hash] opts the optional parameters
117
137
  # @return [File]
118
138
  def download_id_get(id, opts = {})
119
- if Configuration.debugging
120
- Configuration.logger.debug "Calling API: ClientApi#download_id_get ..."
139
+ data, status_code, headers = download_id_get_with_http_info(id, opts)
140
+ return data
141
+ end
142
+
143
+ #
144
+ # Downloads a document.
145
+ # @param id The download_id returned from status request or a callback.
146
+ # @param [Hash] opts the optional parameters
147
+ # @return [Array<(File, Fixnum, Hash)>] File data, response status code and response headers
148
+ def download_id_get_with_http_info(id, opts = {})
149
+ if @api_client.config.debugging
150
+ @api_client.config.logger.debug "Calling API: ClientApi#download_id_get ..."
121
151
  end
122
152
 
123
153
  # verify the required parameter 'id' is set
@@ -148,17 +178,17 @@ module DocRaptor
148
178
 
149
179
 
150
180
  auth_names = ['basicAuth']
151
- result = @api_client.call_api(:GET, path,
181
+ data, status_code, headers = @api_client.call_api(:GET, path,
152
182
  :header_params => header_params,
153
183
  :query_params => query_params,
154
184
  :form_params => form_params,
155
185
  :body => post_body,
156
186
  :auth_names => auth_names,
157
187
  :return_type => 'File')
158
- if Configuration.debugging
159
- Configuration.logger.debug "API called: ClientApi#download_id_get. Result: #{result.inspect}"
188
+ if @api_client.config.debugging
189
+ @api_client.config.logger.debug "API called: ClientApi#download_id_get\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
160
190
  end
161
- return result
191
+ return data, status_code, headers
162
192
  end
163
193
 
164
194
  #
@@ -167,8 +197,18 @@ module DocRaptor
167
197
  # @param [Hash] opts the optional parameters
168
198
  # @return [AsyncDocStatus]
169
199
  def status_id_get(id, opts = {})
170
- if Configuration.debugging
171
- Configuration.logger.debug "Calling API: ClientApi#status_id_get ..."
200
+ data, status_code, headers = status_id_get_with_http_info(id, opts)
201
+ return data
202
+ end
203
+
204
+ #
205
+ # Check on the status of an asynchronously created document.
206
+ # @param id The status_id returned when creating an asynchronous document.
207
+ # @param [Hash] opts the optional parameters
208
+ # @return [Array<(AsyncDocStatus, Fixnum, Hash)>] AsyncDocStatus data, response status code and response headers
209
+ def status_id_get_with_http_info(id, opts = {})
210
+ if @api_client.config.debugging
211
+ @api_client.config.logger.debug "Calling API: ClientApi#status_id_get ..."
172
212
  end
173
213
 
174
214
  # verify the required parameter 'id' is set
@@ -199,17 +239,17 @@ module DocRaptor
199
239
 
200
240
 
201
241
  auth_names = ['basicAuth']
202
- result = @api_client.call_api(:GET, path,
242
+ data, status_code, headers = @api_client.call_api(:GET, path,
203
243
  :header_params => header_params,
204
244
  :query_params => query_params,
205
245
  :form_params => form_params,
206
246
  :body => post_body,
207
247
  :auth_names => auth_names,
208
248
  :return_type => 'AsyncDocStatus')
209
- if Configuration.debugging
210
- Configuration.logger.debug "API called: ClientApi#status_id_get. Result: #{result.inspect}"
249
+ if @api_client.config.debugging
250
+ @api_client.config.logger.debug "API called: ClientApi#status_id_get\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
211
251
  end
212
- return result
252
+ return data, status_code, headers
213
253
  end
214
254
  end
215
255
  end
@@ -7,36 +7,37 @@ require 'uri'
7
7
 
8
8
  module DocRaptor
9
9
  class ApiClient
10
-
11
- attr_accessor :host
10
+ # The Configuration object holding settings to be used in the API client.
11
+ attr_accessor :config
12
12
 
13
13
  # Defines the headers to be used in HTTP requests of all API calls by default.
14
14
  #
15
15
  # @return [Hash]
16
16
  attr_accessor :default_headers
17
17
 
18
- # Stores the HTTP response from the last API call using this API client.
19
- attr_accessor :last_response
20
-
21
- def initialize(host = nil)
22
- @host = host || Configuration.base_url
23
- @format = 'json'
18
+ def initialize(config = Configuration.default)
19
+ @config = config
24
20
  @user_agent = "ruby-swagger-#{VERSION}"
25
21
  @default_headers = {
26
- 'Content-Type' => "application/#{@format.downcase}",
22
+ 'Content-Type' => "application/json",
27
23
  'User-Agent' => @user_agent
28
24
  }
29
25
  end
30
26
 
27
+ def self.default
28
+ @@default ||= ApiClient.new
29
+ end
30
+
31
+ # Call an API with given options.
32
+ #
33
+ # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
34
+ # the data deserialized from response body (could be nil), response status code and response headers.
31
35
  def call_api(http_method, path, opts = {})
32
36
  request = build_request(http_method, path, opts)
33
37
  response = request.run
34
38
 
35
- # record as last response
36
- @last_response = response
37
-
38
- if Configuration.debugging
39
- Configuration.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
39
+ if @config.debugging
40
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
40
41
  end
41
42
 
42
43
  unless response.success?
@@ -47,10 +48,11 @@ module DocRaptor
47
48
  end
48
49
 
49
50
  if opts[:return_type]
50
- deserialize(response, opts[:return_type])
51
+ data = deserialize(response, opts[:return_type])
51
52
  else
52
- nil
53
+ data = nil
53
54
  end
55
+ return data, response.code, response.headers
54
56
  end
55
57
 
56
58
  def build_request(http_method, path, opts = {})
@@ -69,24 +71,34 @@ module DocRaptor
69
71
  :method => http_method,
70
72
  :headers => header_params,
71
73
  :params => query_params,
72
- :ssl_verifypeer => Configuration.verify_ssl,
73
- :sslcert => Configuration.cert_file,
74
- :sslkey => Configuration.key_file,
75
- :cainfo => Configuration.ssl_ca_cert,
76
- :verbose => Configuration.debugging
74
+ :timeout => @config.timeout,
75
+ :ssl_verifypeer => @config.verify_ssl,
76
+ :sslcert => @config.cert_file,
77
+ :sslkey => @config.key_file,
78
+ :cainfo => @config.ssl_ca_cert,
79
+ :verbose => @config.debugging
77
80
  }
78
81
 
79
82
  if [:post, :patch, :put, :delete].include?(http_method)
80
83
  req_body = build_request_body(header_params, form_params, opts[:body])
81
84
  req_opts.update :body => req_body
82
- if Configuration.debugging
83
- Configuration.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
85
+ if @config.debugging
86
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
84
87
  end
85
88
  end
86
89
 
87
90
  Typhoeus::Request.new(url, req_opts)
88
91
  end
89
92
 
93
+ # Check if the given MIME is a JSON MIME.
94
+ # JSON MIME examples:
95
+ # application/json
96
+ # application/json; charset=UTF8
97
+ # APPLICATION/JSON
98
+ def json_mime?(mime)
99
+ !!(mime =~ /\Aapplication\/json(;.*)?\z/i)
100
+ end
101
+
90
102
  # Deserialize the response to the given return type.
91
103
  #
92
104
  # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
@@ -100,9 +112,7 @@ module DocRaptor
100
112
  # ensuring a default content type
101
113
  content_type = response.headers['Content-Type'] || 'application/json'
102
114
 
103
- unless content_type.start_with?('application/json')
104
- fail "Content-Type is not supported: #{content_type}"
105
- end
115
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
106
116
 
107
117
  begin
108
118
  data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
@@ -162,7 +172,7 @@ module DocRaptor
162
172
  # @see Configuration#temp_folder_path
163
173
  # @return [File] the file downloaded
164
174
  def download_file(response)
165
- tmp_file = Tempfile.new '', Configuration.temp_folder_path
175
+ tmp_file = Tempfile.new '', @config.temp_folder_path
166
176
  content_disposition = response.headers['Content-Disposition']
167
177
  if content_disposition
168
178
  filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
@@ -174,24 +184,30 @@ module DocRaptor
174
184
  tmp_file.close!
175
185
 
176
186
  File.open(path, 'w') { |file| file.write(response.body) }
177
- Configuration.logger.info "File written to #{path}. Please move the file to a proper "\
178
- "folder for further processing and delete the temp afterwards"
187
+ @config.logger.info "File written to #{path}. Please move the file to a proper folder "\
188
+ "for further processing and delete the temp afterwards"
179
189
  File.new(path)
180
190
  end
181
191
 
182
192
  def build_request_url(path)
183
193
  # Add leading and trailing slashes to path
184
194
  path = "/#{path}".gsub(/\/+/, '/')
185
- URI.encode(host + path)
195
+ URI.encode(@config.base_url + path)
186
196
  end
187
197
 
188
198
  def build_request_body(header_params, form_params, body)
189
199
  # http form
190
200
  if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
191
201
  header_params['Content-Type'] == 'multipart/form-data'
192
- data = form_params.dup
193
- data.each do |key, value|
194
- data[key] = value.to_s if value && !value.is_a?(File)
202
+ data = {}
203
+ form_params.each do |key, value|
204
+ case value
205
+ when File, Array, nil
206
+ # let typhoeus handle File, Array and nil parameters
207
+ data[key] = value
208
+ else
209
+ data[key] = value.to_s
210
+ end
195
211
  end
196
212
  elsif body
197
213
  data = body.is_a?(String) ? body : body.to_json
@@ -204,7 +220,7 @@ module DocRaptor
204
220
  # Update hearder and query params based on authentication settings.
205
221
  def update_params_for_auth!(header_params, query_params, auth_names)
206
222
  Array(auth_names).each do |auth_name|
207
- auth_setting = Configuration.auth_settings[auth_name]
223
+ auth_setting = @config.auth_settings[auth_name]
208
224
  next unless auth_setting
209
225
  case auth_setting[:in]
210
226
  when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
@@ -223,26 +239,21 @@ module DocRaptor
223
239
  # @param [Array] accepts array for Accept
224
240
  # @return [String] the Accept header (e.g. application/json)
225
241
  def select_header_accept(accepts)
226
- if accepts.empty?
227
- return
228
- elsif accepts.any?{ |s| s.casecmp('application/json') == 0 }
229
- 'application/json' # look for json data by default
230
- else
231
- accepts.join(',')
232
- end
242
+ return nil if accepts.nil? || accepts.empty?
243
+ # use JSON when present, otherwise use all of the provided
244
+ json_accept = accepts.find { |s| json_mime?(s) }
245
+ return json_accept || accepts.join(',')
233
246
  end
234
247
 
235
248
  # Return Content-Type header based on an array of content types provided.
236
249
  # @param [Array] content_types array for Content-Type
237
250
  # @return [String] the Content-Type header (e.g. application/json)
238
251
  def select_header_content_type(content_types)
239
- if content_types.empty?
240
- 'application/json' # use application/json by default
241
- elsif content_types.any?{ |s| s.casecmp('application/json')==0 }
242
- 'application/json' # use application/json if it's included
243
- else
244
- content_types[0] # otherwise, use the first one
245
- end
252
+ # use application/json by default
253
+ return 'application/json' if content_types.nil? || content_types.empty?
254
+ # use JSON when present, otherwise use the first one
255
+ json_content_type = content_types.find { |s| json_mime?(s) }
256
+ return json_content_type || content_types.first
246
257
  end
247
258
 
248
259
  # Convert object (array, hash, object, etc) to JSON string.
@@ -269,5 +280,25 @@ module DocRaptor
269
280
  obj
270
281
  end
271
282
  end
283
+
284
+ # Build parameter value according to the given collection format.
285
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
286
+ def build_collection_param(param, collection_format)
287
+ case collection_format
288
+ when :csv
289
+ param.join(',')
290
+ when :ssv
291
+ param.join(' ')
292
+ when :tsv
293
+ param.join("\t")
294
+ when :pipes
295
+ param.join('|')
296
+ when :multi
297
+ # return the array directly as typhoeus will handle it as expected
298
+ param
299
+ else
300
+ fail "unknown collection format: #{collection_format.inspect}"
301
+ end
302
+ end
272
303
  end
273
304
  end