Class: DocRaptor

Inherits:
Object show all
Includes:
HTTParty
Defined in:
lib/doc_raptor.rb,
lib/doc_raptor/version.rb

Constant Summary collapse

VERSION =
"0.3.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.download_keyObject

Returns the value of attribute download_key.



148
149
150
# File 'lib/doc_raptor.rb', line 148

def download_key
  @download_key
end

.status_idObject

Returns the value of attribute status_id.



147
148
149
# File 'lib/doc_raptor.rb', line 147

def status_id
  @status_id
end

Class Method Details

.api_key(key = nil) ⇒ Object



13
14
15
16
# File 'lib/doc_raptor.rb', line 13

def self.api_key(key = nil)
  default_options[:api_key] = key ? key : default_options[:api_key] || ENV["DOCRAPTOR_API_KEY"]
  default_options[:api_key] || raise(DocRaptorError::NoApiKeyProvidedError.new("No API key provided"))
end

.create(options = { }) ⇒ Object

when given a block, hands the block a TempFile of the resulting document otherwise, just returns the response



25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
# File 'lib/doc_raptor.rb', line 25

def self.create(options = { })
  raise ArgumentError.new "please pass in an options hash" unless options.is_a? Hash
  if options[:document_content].blank? && options[:document_url].blank?
    raise DocRaptorError::NoContentError.new("must supply :document_content or :document_url")
  end

  default_options = {
    :name                       => "default",
    :document_type              => "pdf",
    :test                       => false,
    :async                      => 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

  # HOTFIX
  # convert safebuffers to plain old strings so the gsub'ing that has to occur
  # for url encoding works
  # Broken by: https://github.com/rails/rails/commit/1300c034775a5d52ad9141fdf5bbdbb9159df96a#activesupport/lib/active_support/core_ext/string/output_safety.rb
  # Discussion: https://github.com/rails/rails/issues/1555
  if defined?(ActiveSupport) && defined?(ActiveSupport::SafeBuffer)
    options.map{|k,v| options[k] = options[k].to_str if options[k].is_a?(ActiveSupport::SafeBuffer)}
  end
  # /HOTFIX

  response = post("/docs", :body => {:doc => options}, :basic_auth => {:username => api_key})

  if raise_exception_on_failure && !response.success?
    raise DocRaptorException::DocumentCreationFailure.new response.body, response.code
  end

  if response.success? && options[:async]
    self.status_id = response.parsed_response["status_id"]
  end

  if block_given?
    ret_val = nil
    Tempfile.open("docraptor") 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



18
19
20
21
# File 'lib/doc_raptor.rb', line 18

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

.download(key = self.download_key, raise_exception_on_failure = false) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/doc_raptor.rb', line 124

def self.download(key = self.download_key, raise_exception_on_failure = false)
  response = get("/download/#{key}")

  if raise_exception_on_failure && !response.success?
    raise DocRaptorException::DocumentDownloadFailure.new response.body, response.code
  end

  if block_given?
    ret_val = nil
    Tempfile.open("docraptor") do |f|
      f.sync = true
      f.write(response.body)
      f.rewind

      ret_val = yield f, response
    end
    ret_val
  else
    response
  end
end

.download!(key = self.download_key) ⇒ Object



120
121
122
# File 'lib/doc_raptor.rb', line 120

def self.download!(key = self.download_key)
  self.download(key, true)
end

.list_docs(options = { }) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/doc_raptor.rb', line 82

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("/docs", :query => options, :basic_auth => { :username => api_key })
  if raise_exception_on_failure && !response.success?
    raise DocRaptorException::DocumentListingFailure.new response.body, response.code
  end

  response
end

.list_docs!(options = { }) ⇒ Object



77
78
79
80
# File 'lib/doc_raptor.rb', line 77

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

.status(id = self.status_id, raise_exception_on_failure = false) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/doc_raptor.rb', line 105

def self.status(id = self.status_id, raise_exception_on_failure = false)
  response = get("/status/#{id}", :basic_auth => { :username => api_key }, :output => 'json')

  if raise_exception_on_failure && !response.success?
    raise DocRaptorException::DocumentStatusFailure.new response.body, response.code
  end

  json = response.parsed_response
  if json['status'] == 'completed'
    self.download_key = json['download_url'].match(/.*?\/download\/(.+)/)[1]
    json['download_key'] = self.download_key
  end
  json
end

.status!(id = self.status_id) ⇒ Object



101
102
103
# File 'lib/doc_raptor.rb', line 101

def self.status!(id = self.status_id)
  self.status(id, true)
end