Class: DocRaptor
- Includes:
- HTTParty
- Defined in:
- lib/doc_raptor.rb,
lib/doc_raptor/version.rb
Constant Summary collapse
- VERSION =
"0.4.0"
Class Attribute Summary collapse
-
.download_key ⇒ Object
Returns the value of attribute download_key.
-
.status_id ⇒ Object
Returns the value of attribute status_id.
Class Method Summary collapse
- .api_key(key = nil) ⇒ Object
-
.create(options = { }) ⇒ Object
when given a block, hands the block a TempFile of the resulting document otherwise, just returns the response.
- .create!(options = {}) ⇒ Object
- .disable_agent_tracking ⇒ Object
- .download(key = self.download_key, raise_exception_on_failure = false) ⇒ Object
- .download!(key = self.download_key) ⇒ Object
- .list_docs(options = { }) ⇒ Object
- .list_docs!(options = { }) ⇒ Object
- .status(id = self.status_id, raise_exception_on_failure = false) ⇒ Object
- .status!(id = self.status_id) ⇒ Object
Class Attribute Details
.download_key ⇒ Object
Returns the value of attribute download_key.
154 155 156 |
# File 'lib/doc_raptor.rb', line 154 def download_key @download_key end |
.status_id ⇒ Object
Returns the value of attribute status_id.
153 154 155 |
# File 'lib/doc_raptor.rb', line 153 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) [:api_key] = key ? key : [:api_key] || ENV["DOCRAPTOR_API_KEY"] [: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( = { }) raise ArgumentError.new "please pass in an options hash" unless .is_a? Hash if [:document_content].blank? && [:document_url].blank? raise DocRaptorError::NoContentError.new("must supply :document_content or :document_url") end = { :name => "default", :document_type => "pdf", :test => false, :async => false, :raise_exception_on_failure => false } = .merge() raise_exception_on_failure = [:raise_exception_on_failure] .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) .map{|k,v| [k] = [k].to_str if [k].is_a?(ActiveSupport::SafeBuffer)} end # /HOTFIX response = post("/docs", :body => {:doc => }, :basic_auth => {:username => api_key}) if raise_exception_on_failure && !response.success? raise DocRaptorException::DocumentCreationFailure.new response.body, response.code end if response.success? && [: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!( = {}) raise ArgumentError.new "please pass in an options hash" unless .is_a? Hash self.create(.merge({:raise_exception_on_failure => true})) end |
.disable_agent_tracking ⇒ Object
20 21 22 |
# File 'lib/doc_raptor.rb', line 20 def self.disable_agent_tracking [:headers].delete("User-Agent") end |
.download(key = self.download_key, raise_exception_on_failure = false) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/doc_raptor.rb', line 130 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
126 127 128 |
# File 'lib/doc_raptor.rb', line 126 def self.download!(key = self.download_key) self.download(key, true) end |
.list_docs(options = { }) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/doc_raptor.rb', line 88 def self.list_docs( = { }) raise ArgumentError.new "please pass in an options hash" unless .is_a? Hash = { :page => 1, :per_page => 100, :raise_exception_on_failure => false } = .merge() raise_exception_on_failure = [:raise_exception_on_failure] .delete :raise_exception_on_failure response = get("/docs", :query => , :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!( = { }) raise ArgumentError.new "please pass in an options hash" unless .is_a? Hash self.list_docs(.merge({:raise_exception_on_failure => true})) end |
.status(id = self.status_id, raise_exception_on_failure = false) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/doc_raptor.rb', line 111 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
107 108 109 |
# File 'lib/doc_raptor.rb', line 107 def self.status!(id = self.status_id) self.status(id, true) end |