Module: DocmagoClient
- Includes:
- HTTMultiParty
- Defined in:
- lib/docmago_client.rb,
lib/docmago_client/error.rb,
lib/docmago_client/railtie.rb,
lib/docmago_client/version.rb,
lib/docmago_client/exception.rb,
lib/docmago_client/html_resource_archiver.rb
Defined Under Namespace
Modules: Error, Exception
Classes: HTMLResourceArchiver, Railtie
Constant Summary
collapse
- VERSION =
'0.4.2'.freeze
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.logger ⇒ Object
25
26
27
|
# File 'lib/docmago_client.rb', line 25
def logger
@logger ||= Logger.new($stdout)
end
|
Class Method Details
.api_key(key = nil) ⇒ Object
35
36
37
38
|
# File 'lib/docmago_client.rb', line 35
def self.api_key(key = nil)
default_options[:api_key] = key ? key : default_options[:api_key] || ENV['DOCMAGO_API_KEY']
default_options[:api_key] || raise(DocmagoClient::Error::NoApiKeyProvidedError.new('No API key provided'))
end
|
.base_uri(uri = nil) ⇒ Object
30
31
32
33
|
# File 'lib/docmago_client.rb', line 30
def self.base_uri(uri = nil)
default_options[:base_uri] = uri ? uri : default_options[:base_uri] || ENV['DOCMAGO_URL']
default_options[:base_uri]
end
|
.create(options = {}) ⇒ Object
when given a block, hands the block a TempFile of the resulting document otherwise, just returns the response
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/docmago_client.rb', line 47
def self.create(options = {})
raise ArgumentError, 'please pass in an options hash' unless options.is_a? Hash
if options[:content].nil? || options[:content].empty?
raise DocmagoClient::Error::NoContentError.new, 'must supply :content'
end
default_options = {
name: 'default',
type: 'pdf',
test_mode: false,
raise_exception_on_failure: false
}
options = default_options.merge(options)
raise_exception_on_failure = options[:raise_exception_on_failure]
options.delete :raise_exception_on_failure
if options[:zip_resources]
tmp_dir = Dir.mktmpdir
begin
resource_archiver = HTMLResourceArchiver.new(options)
options[:content] = File.new(resource_archiver.create_zip("#{tmp_dir}/document.zip"))
options.delete :assets
response = post('/documents', body: { document: options }, basic_auth: { username: api_key })
ensure
FileUtils.remove_entry_secure tmp_dir
end
else
response = post('/documents', body: { document: options }, basic_auth: { username: api_key })
end
if raise_exception_on_failure && !response.success?
raise DocmagoClient::Exception::DocumentCreationFailure.new response.body, response.code
end
if block_given?
ret_val = nil
Tempfile.open('docmago') do |f|
f.sync = true
f.write(response.body)
f.rewind
ret_val = yield f, response
end
ret_val
else
response
end
end
|
.create!(options = {}) ⇒ Object
40
41
42
43
|
# File 'lib/docmago_client.rb', line 40
def self.create!(options = {})
raise ArgumentError, 'please pass in an options hash' unless options.is_a? Hash
create options.merge(raise_exception_on_failure: true)
end
|
.list_docs(options = {}) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/docmago_client.rb', line 103
def self.list_docs(options = {})
raise ArgumentError, 'please pass in an options hash' unless options.is_a? Hash
default_options = {
page: 1,
per_page: 100,
raise_exception_on_failure: false
}
options = default_options.merge(options)
raise_exception_on_failure = options[:raise_exception_on_failure]
options.delete :raise_exception_on_failure
response = get('/documents', query: options, basic_auth: { username: api_key })
if raise_exception_on_failure && !response.success?
raise DocmagoClient::Exception::DocumentListingFailure.new response.body, response.code
end
response
end
|
.list_docs!(options = {}) ⇒ Object
98
99
100
101
|
# File 'lib/docmago_client.rb', line 98
def self.list_docs!(options = {})
raise ArgumentError, 'please pass in an options hash' unless options.is_a? Hash
list_docs options.merge(raise_exception_on_failure: true)
end
|