Class: DocRaptor

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

Constant Summary collapse

VERSION =
"0.5.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.download_keyObject

Returns the value of attribute download_key.



181
182
183
# File 'lib/doc_raptor.rb', line 181

def download_key
  @download_key
end

.status_idObject

Returns the value of attribute status_id.



180
181
182
# File 'lib/doc_raptor.rb', line 180

def status_id
  @status_id
end

Class Method Details

.api_key(key = nil) ⇒ Object



15
16
17
18
# File 'lib/doc_raptor.rb', line 15

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



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
76
77
78
79
80
81
# File 'lib/doc_raptor.rb', line 31

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", :encoding => "ascii-8bit") 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



24
25
26
27
# File 'lib/doc_raptor.rb', line 24

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

.disable_agent_trackingObject



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

def self.disable_agent_tracking
  default_options[:headers].delete("User-Agent")
end

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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/doc_raptor.rb', line 157

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



153
154
155
# File 'lib/doc_raptor.rb', line 153

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

.list_doc_logs(options = { }) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/doc_raptor.rb', line 113

def self.list_doc_logs(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,
    :output_format              => "xml",
  }

  options = default_options.merge(options)
  output_format = options.delete(:output_format)
  raise_exception_on_failure = options.delete(:raise_exception_on_failure)

  response = get("/doc_logs.#{output_format}", :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_doc_logs!(options = { }) ⇒ Object



108
109
110
111
# File 'lib/doc_raptor.rb', line 108

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

.list_docs(options = { }) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/doc_raptor.rb', line 88

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,
    :output_format              => "xml",
  }
  options = default_options.merge(options)
  output_format = options.delete(:output_format)
  raise_exception_on_failure = options.delete(:raise_exception_on_failure)

  response = get("/docs.#{output_format}", :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



83
84
85
86
# File 'lib/doc_raptor.rb', line 83

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



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/doc_raptor.rb', line 138

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



134
135
136
# File 'lib/doc_raptor.rb', line 134

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