docraptor 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ module DocRaptor
2
+ class ApiError < StandardError
3
+ attr_reader :code, :response_headers, :response_body
4
+
5
+ # Usage examples:
6
+ # ApiError.new
7
+ # ApiError.new("message")
8
+ # ApiError.new(:code => 500, :response_headers => {}, :response_body => "")
9
+ # ApiError.new(:code => 404, :message => "Not Found")
10
+ def initialize(arg = nil)
11
+ if arg.is_a? Hash
12
+ arg.each do |k, v|
13
+ if k.to_s == 'message'
14
+ super v
15
+ else
16
+ instance_variable_set "@#{k}", v
17
+ end
18
+ end
19
+ else
20
+ super arg
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,173 @@
1
+ require 'uri'
2
+ require 'singleton'
3
+
4
+ module DocRaptor
5
+ class Configuration
6
+
7
+ include Singleton
8
+
9
+ # Default api client
10
+ attr_accessor :api_client
11
+
12
+ # Defines url scheme
13
+ attr_accessor :scheme
14
+
15
+ # Defines url host
16
+ attr_accessor :host
17
+
18
+ # Defines url base path
19
+ attr_accessor :base_path
20
+
21
+ # Defines API keys used with API Key authentications.
22
+ #
23
+ # @return [Hash] key: parameter name, value: parameter value (API key)
24
+ #
25
+ # @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
26
+ # config.api_key['api_key'] = 'xxx'
27
+ attr_accessor :api_key
28
+
29
+ # Defines API key prefixes used with API Key authentications.
30
+ #
31
+ # @return [Hash] key: parameter name, value: API key prefix
32
+ #
33
+ # @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
34
+ # config.api_key_prefix['api_key'] = 'Token'
35
+ attr_accessor :api_key_prefix
36
+
37
+ # Defines the username used with HTTP basic authentication.
38
+ #
39
+ # @return [String]
40
+ attr_accessor :username
41
+
42
+ # Defines the password used with HTTP basic authentication.
43
+ #
44
+ # @return [String]
45
+ attr_accessor :password
46
+
47
+ # Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
48
+ # details will be logged with `logger.debug` (see the `logger` attribute).
49
+ # Default to false.
50
+ #
51
+ # @return [true, false]
52
+ attr_accessor :debugging
53
+
54
+ # Defines the logger used for debugging.
55
+ # Default to `Rails.logger` (when in Rails) or logging to STDOUT.
56
+ #
57
+ # @return [#debug]
58
+ attr_accessor :logger
59
+
60
+ # Defines the temporary folder to store downloaded files
61
+ # (for API endpoints that have file response).
62
+ # Default to use `Tempfile`.
63
+ #
64
+ # @return [String]
65
+ attr_accessor :temp_folder_path
66
+
67
+ ### TLS/SSL
68
+ # Set this to false to skip verifying SSL certificate when calling API from https server.
69
+ # Default to true.
70
+ #
71
+ # @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
72
+ #
73
+ # @return [true, false]
74
+ attr_accessor :verify_ssl
75
+
76
+ # Set this to customize the certificate file to verify the peer.
77
+ #
78
+ # @return [String] the path to the certificate file
79
+ #
80
+ # @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
81
+ # https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
82
+ attr_accessor :ssl_ca_cert
83
+
84
+ # Client certificate file (for client certificate)
85
+ attr_accessor :cert_file
86
+
87
+ # Client private key file (for client certificate)
88
+ attr_accessor :key_file
89
+
90
+ attr_accessor :inject_format
91
+
92
+ attr_accessor :force_ending_format
93
+
94
+ class << self
95
+ def method_missing(method_name, *args, &block)
96
+ config = Configuration.instance
97
+ if config.respond_to?(method_name)
98
+ config.send(method_name, *args, &block)
99
+ else
100
+ super
101
+ end
102
+ end
103
+ end
104
+
105
+ def initialize
106
+ @scheme = 'http'
107
+ @host = 'localhost:3000'
108
+ @base_path = '/'
109
+ @api_key = {}
110
+ @api_key_prefix = {}
111
+ @verify_ssl = true
112
+ @cert_file = nil
113
+ @key_file = nil
114
+ @debugging = false
115
+ @inject_format = false
116
+ @force_ending_format = false
117
+ @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
118
+ end
119
+
120
+ def api_client
121
+ @api_client ||= ApiClient.new
122
+ end
123
+
124
+ def scheme=(scheme)
125
+ # remove :// from scheme
126
+ @scheme = scheme.sub(/:\/\//, '')
127
+ end
128
+
129
+ def host=(host)
130
+ # remove http(s):// and anything after a slash
131
+ @host = host.sub(/https?:\/\//, '').split('/').first
132
+ end
133
+
134
+ def base_path=(base_path)
135
+ # Add leading and trailing slashes to base_path
136
+ @base_path = "/#{base_path}".gsub(/\/+/, '/')
137
+ @base_path = "" if @base_path == "/"
138
+ end
139
+
140
+ def base_url
141
+ url = "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '')
142
+ URI.encode(url)
143
+ end
144
+
145
+ # Gets API key (with prefix if set).
146
+ # @param [String] param_name the parameter name of API key auth
147
+ def api_key_with_prefix(param_name)
148
+ if @api_key_prefix[param_name]
149
+ "#{@api_key_prefix[param_name]} #{@api_key[param_name]}"
150
+ else
151
+ @api_key[param_name]
152
+ end
153
+ end
154
+
155
+ # Gets Basic Auth token string
156
+ def basic_auth_token
157
+ 'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
158
+ end
159
+
160
+ # Returns Auth Settings hash for api client.
161
+ def auth_settings
162
+ {
163
+ 'basicAuth' =>
164
+ {
165
+ type: 'basic',
166
+ in: 'header',
167
+ key: 'Authorization',
168
+ value: basic_auth_token
169
+ },
170
+ }
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,37 @@
1
+ module DocRaptor
2
+ #
3
+ class AsyncDoc < BaseObject
4
+ attr_accessor :status_id
5
+ # attribute mapping from ruby-style variable name to JSON key
6
+ def self.attribute_map
7
+ {
8
+
9
+ # The identifier used to get the status of the document using the status api.
10
+ :'status_id' => :'status_id'
11
+
12
+ }
13
+ end
14
+
15
+ # attribute type
16
+ def self.swagger_types
17
+ {
18
+ :'status_id' => :'String'
19
+
20
+ }
21
+ end
22
+
23
+ def initialize(attributes = {})
24
+ return if !attributes.is_a?(Hash) || attributes.empty?
25
+
26
+ # convert string to symbol for hash key
27
+ attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
28
+
29
+
30
+ if attributes[:'status_id']
31
+ self.status_id = attributes[:'status_id']
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,77 @@
1
+ module DocRaptor
2
+ #
3
+ class AsyncDocStatus < BaseObject
4
+ attr_accessor :status, :download_url, :download_id, :message, :number_of_pages, :validation_errors
5
+ # attribute mapping from ruby-style variable name to JSON key
6
+ def self.attribute_map
7
+ {
8
+
9
+ # The present status of the document. Can be queued, working, completed, and failed.
10
+ :'status' => :'status',
11
+
12
+ # The URL where the document can be retrieved. This URL may only be used a few times.
13
+ :'download_url' => :'download_url',
14
+
15
+ # The identifier for downloading the document with the download api.
16
+ :'download_id' => :'download_id',
17
+
18
+ # Additional information.
19
+ :'message' => :'message',
20
+
21
+ # Number of PDF pages in document.
22
+ :'number_of_pages' => :'number_of_pages',
23
+
24
+ # Error information.
25
+ :'validation_errors' => :'validation_errors'
26
+
27
+ }
28
+ end
29
+
30
+ # attribute type
31
+ def self.swagger_types
32
+ {
33
+ :'status' => :'String',
34
+ :'download_url' => :'String',
35
+ :'download_id' => :'String',
36
+ :'message' => :'String',
37
+ :'number_of_pages' => :'Integer',
38
+ :'validation_errors' => :'String'
39
+
40
+ }
41
+ end
42
+
43
+ def initialize(attributes = {})
44
+ return if !attributes.is_a?(Hash) || attributes.empty?
45
+
46
+ # convert string to symbol for hash key
47
+ attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
48
+
49
+
50
+ if attributes[:'status']
51
+ self.status = attributes[:'status']
52
+ end
53
+
54
+ if attributes[:'download_url']
55
+ self.download_url = attributes[:'download_url']
56
+ end
57
+
58
+ if attributes[:'download_id']
59
+ self.download_id = attributes[:'download_id']
60
+ end
61
+
62
+ if attributes[:'message']
63
+ self.message = attributes[:'message']
64
+ end
65
+
66
+ if attributes[:'number_of_pages']
67
+ self.number_of_pages = attributes[:'number_of_pages']
68
+ end
69
+
70
+ if attributes[:'validation_errors']
71
+ self.validation_errors = attributes[:'validation_errors']
72
+ end
73
+
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,86 @@
1
+ require 'date'
2
+
3
+ module DocRaptor
4
+ # base class containing fundamental method such as to_hash, build_from_hash and more
5
+ class BaseObject
6
+
7
+ # build the object from hash
8
+ def build_from_hash(attributes)
9
+ return nil unless attributes.is_a?(Hash)
10
+ self.class.swagger_types.each_pair do |key, type|
11
+ if type =~ /^Array<(.*)>/i
12
+ if attributes[self.class.attribute_map[key]].is_a?(Array)
13
+ self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } )
14
+ else
15
+ #TODO show warning in debug mode
16
+ end
17
+ elsif !attributes[self.class.attribute_map[key]].nil?
18
+ self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
19
+ else
20
+ # data not found in attributes(hash), not an issue as the data can be optional
21
+ end
22
+ end
23
+
24
+ self
25
+ end
26
+
27
+ def _deserialize(type, value)
28
+ case type.to_sym
29
+ when :DateTime
30
+ DateTime.parse(value)
31
+ when :Date
32
+ Date.parse(value)
33
+ when :String
34
+ value.to_s
35
+ when :Integer
36
+ value.to_i
37
+ when :Float
38
+ value.to_f
39
+ when :BOOLEAN
40
+ if value =~ /^(true|t|yes|y|1)$/i
41
+ true
42
+ else
43
+ false
44
+ end
45
+ else # model
46
+ _model = DocRaptor.const_get(type).new
47
+ _model.build_from_hash(value)
48
+ end
49
+ end
50
+
51
+ def to_s
52
+ to_hash.to_s
53
+ end
54
+
55
+ # to_body is an alias to to_body (backward compatibility))
56
+ def to_body
57
+ to_hash
58
+ end
59
+
60
+ # return the object in the form of hash
61
+ def to_hash
62
+ hash = {}
63
+ self.class.attribute_map.each_pair do |attr, param|
64
+ value = self.send(attr)
65
+ next if value.nil?
66
+ if value.is_a?(Array)
67
+ hash[param] = value.compact.map{ |v| _to_hash(v) }
68
+ else
69
+ hash[param] = _to_hash(value)
70
+ end
71
+ end
72
+ hash
73
+ end
74
+
75
+ # Method to output non-array value in the form of hash
76
+ # For object, use to_hash. Otherwise, just return the value
77
+ def _to_hash(value)
78
+ if value.respond_to? :to_hash
79
+ value.to_hash
80
+ else
81
+ value
82
+ end
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,141 @@
1
+ module DocRaptor
2
+ #
3
+ class Doc < BaseObject
4
+ attr_accessor :name, :document_type, :document_content, :document_url, :test, :strict, :tag, :help, :javascript, :referrer, :callback_url, :prince_options
5
+ # attribute mapping from ruby-style variable name to JSON key
6
+ def self.attribute_map
7
+ {
8
+
9
+ # A name for identifying your document.
10
+ :'name' => :'name',
11
+
12
+ # The type of document being created.
13
+ :'document_type' => :'document_type',
14
+
15
+ # The HTML data to be transformed into a document.\nYou must supply content using document_content or document_url.
16
+ :'document_content' => :'document_content',
17
+
18
+ # The URL to fetch the HTML data to be transformed into a document.\nYou must supply content using document_content or document_url.
19
+ :'document_url' => :'document_url',
20
+
21
+ # Enable test mode for this document. Test documents are not charged for but include a watermark.
22
+ :'test' => :'test',
23
+
24
+ # Force strict HTML validation.
25
+ :'strict' => :'strict',
26
+
27
+ # A field for storing a small amount of metadata with this document.
28
+ :'tag' => :'tag',
29
+
30
+ # Request support help with this request if it succeeds.
31
+ :'help' => :'help',
32
+
33
+ # Enable DocRaptor JavaScript parsing. PrinceXML JavaScript parsing is also available elsewhere.
34
+ :'javascript' => :'javascript',
35
+
36
+ # Set HTTP referrer when generating this document.
37
+ :'referrer' => :'referrer',
38
+
39
+ # A URL that will receive a POST request after successfully completing an asynchronous document.\nThe POST data will include download_url and download_id similar to status api responses.\nWARNING: this only works on asynchronous documents.
40
+ :'callback_url' => :'callback_url',
41
+
42
+ #
43
+ :'prince_options' => :'prince_options'
44
+
45
+ }
46
+ end
47
+
48
+ # attribute type
49
+ def self.swagger_types
50
+ {
51
+ :'name' => :'String',
52
+ :'document_type' => :'String',
53
+ :'document_content' => :'String',
54
+ :'document_url' => :'String',
55
+ :'test' => :'BOOLEAN',
56
+ :'strict' => :'String',
57
+ :'tag' => :'String',
58
+ :'help' => :'BOOLEAN',
59
+ :'javascript' => :'BOOLEAN',
60
+ :'referrer' => :'String',
61
+ :'callback_url' => :'String',
62
+ :'prince_options' => :'PrinceOptions'
63
+
64
+ }
65
+ end
66
+
67
+ def initialize(attributes = {})
68
+ return if !attributes.is_a?(Hash) || attributes.empty?
69
+
70
+ # convert string to symbol for hash key
71
+ attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
72
+
73
+
74
+ if attributes[:'name']
75
+ self.name = attributes[:'name']
76
+ end
77
+
78
+ if attributes[:'document_type']
79
+ self.document_type = attributes[:'document_type']
80
+ end
81
+
82
+ if attributes[:'document_content']
83
+ self.document_content = attributes[:'document_content']
84
+ end
85
+
86
+ if attributes[:'document_url']
87
+ self.document_url = attributes[:'document_url']
88
+ end
89
+
90
+ if attributes[:'test']
91
+ self.test = attributes[:'test']
92
+ end
93
+
94
+ if attributes[:'strict']
95
+ self.strict = attributes[:'strict']
96
+ end
97
+
98
+ if attributes[:'tag']
99
+ self.tag = attributes[:'tag']
100
+ end
101
+
102
+ if attributes[:'help']
103
+ self.help = attributes[:'help']
104
+ end
105
+
106
+ if attributes[:'javascript']
107
+ self.javascript = attributes[:'javascript']
108
+ end
109
+
110
+ if attributes[:'referrer']
111
+ self.referrer = attributes[:'referrer']
112
+ end
113
+
114
+ if attributes[:'callback_url']
115
+ self.callback_url = attributes[:'callback_url']
116
+ end
117
+
118
+ if attributes[:'prince_options']
119
+ self.prince_options = attributes[:'prince_options']
120
+ end
121
+
122
+ end
123
+
124
+ def document_type=(document_type)
125
+ allowed_values = ["pdf", "xls", "xlsx"]
126
+ if document_type && !allowed_values.include?(document_type)
127
+ fail "invalid value for 'document_type', must be one of #{allowed_values}"
128
+ end
129
+ @document_type = document_type
130
+ end
131
+
132
+ def strict=(strict)
133
+ allowed_values = ["none"]
134
+ if strict && !allowed_values.include?(strict)
135
+ fail "invalid value for 'strict', must be one of #{allowed_values}"
136
+ end
137
+ @strict = strict
138
+ end
139
+
140
+ end
141
+ end