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.3.2"

Class Method Summary collapse

Class Method Details

.api_key(key = nil) ⇒ Object



27
28
29
30
# File 'lib/docmago_client.rb', line 27

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



22
23
24
25
# File 'lib/docmago_client.rb', line 22

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



39
40
41
42
43
44
45
46
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
# File 'lib/docmago_client.rb', line 39

def self.create(options={})
  raise ArgumentError.new "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[:content], options[:resource_path])
      options[:content] = File.new(resource_archiver.create_zip("#{tmp_dir}/document.zip"))
  
      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



32
33
34
35
# File 'lib/docmago_client.rb', line 32

def self.create!(options={})
  raise ArgumentError.new "please pass in an options hash" unless options.is_a? Hash
  self.create options.merge(raise_exception_on_failure: true)
end

.list_docs(options = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/docmago_client.rb', line 94

def self.list_docs(options={})
  raise ArgumentError.new "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



89
90
91
92
# File 'lib/docmago_client.rb', line 89

def self.list_docs!(options={})
  raise ArgumentError.new "please pass in an options hash" unless options.is_a? Hash
  self.list_docs options.merge(raise_exception_on_failure: true)
end