doc_raptor 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/Changelog.md +31 -0
  2. data/README.md +47 -36
  3. data/lib/doc_raptor.rb +86 -16
  4. metadata +4 -3
data/Changelog.md ADDED
@@ -0,0 +1,31 @@
1
+ # DocRaptor gem Change Log
2
+
3
+ ## doc_raptor 0.2.0
4
+ * tests!
5
+ * added a create! method which will raise an exception when doc creation fails
6
+ * added a list_docs! method which will raise an exception when doc listing fails
7
+ * added a status! method which will raise an exception when getting an async status fails
8
+ * added a download! method which will raise an exception when getting an async status fails
9
+
10
+ ## doc_raptor 0.1.6
11
+ * allow the gem to be used outside of places with activesupport
12
+
13
+ ## doc_raptor 0.1.5
14
+ * fix bug caused by activesupport safebuffers in rails ~3.0.6 and up
15
+
16
+ ## doc_raptor 0.1.4
17
+ * add support for creating async jobs
18
+
19
+ ## doc_raptor 0.1.3
20
+ * if a block is given to "create", make the value the block returns be the
21
+ value create returns
22
+ * add list_doc method, for the new api call to list created documents
23
+
24
+ ## doc_raptor 0.1.2
25
+ * add support for the the document_url parameter to create
26
+
27
+ ## doc_raptor 0.1.1
28
+ * reduce httparty requirement to 0.4.3
29
+
30
+ ## doc_raptor 0.1
31
+ * Initial release
data/README.md CHANGED
@@ -9,40 +9,54 @@ This is a Ruby gem providing a simple wrapper around the DocRaptor API. DocRapto
9
9
  The gem will look for your api key in the ENV variable "DOCRAPTOR_API_KEY". If it is
10
10
  not there, you can set it directly by calling:
11
11
 
12
- DocRaptor.api_key "My API Key Here"
12
+ ```ruby
13
+ DocRaptor.api_key "My API Key Here"
14
+ ```
13
15
 
14
- Once an API key is set, you can create documents by calling:
16
+ Once an API key is set, you can create a PDF document by calling:
15
17
 
16
- DocRaptor.create(:document_content => content, :document_type => "pdf", :test => false, :async => false, :callback_url => nil)
18
+ ```ruby
19
+ DocRaptor.create(:document_content => content)
20
+ ```
17
21
 
18
- This will return an HTTParty respsonse object, the body of which will be the new file
19
- (or errors, if the request was not valid). You can pass in "pdf" or "xls" as the
20
- :document_type - default is pdf. This determines the type of file that DocRaptor will create.
21
- You can pass in true or false for :test - default is false - and this turns on or off
22
- test mode for DocRaptor. You can pass in true or false for :async - default is false - and
23
- this submits the document request to be processed asynchronously. If the document is processed
24
- asynchronously, a status id will be returned as opposed to the contents of the document. You can
25
- then use <METHOD NAME> to get the status of the document. You can pass in a URL to :callback_url
26
- to be called once an asynchronous job is complete. It will be passed a value of "download_url"
27
- which will contain a URL that when visited will provide you with your document. This option
28
- does nothing if :async is not true. The only required parameter is :document_content, which
29
- should be a string of html - this will be what DocRaptor turns into your document.
22
+ You might want to set other options in that hash:
30
23
 
31
- The create call can also take a block, like so:
24
+ * `:document_content` - a string containing the content for creating the document
25
+ * `:document_url` - a url to visit to get the content for creating the document
26
+ * `:document_type` - "pdf" or "xls"; controls the type of document generated; default is "pdf"
27
+ * `:name` - an identifier you can use for the document; shows up in doc logs; default is "default"
28
+ * `:test` - test mode flag; set to true to while doing testing so the docs won't count against your monthly count; default is false
29
+ * `:async` - create the document asynchonously; default is false
30
+ * `:callback_url` - a url that we will hit with a status once the asynchronous document has been fully processed
31
+ * `:prince_options` - see http://docraptor.com/documentation#pdf_options (PDFs only)
32
32
 
33
- DocRaptor.create(:document_content => content) do |file, response|
34
- #file is a tempfile holding the response body
35
- #reponse is the HTTParty response object
36
- end
33
+ The only required parameter is one of `:document_content` or `:document_url`.
34
+
35
+ `create` will return an HTTParty response object, the body of which will be the new file (or errors, if the request was not valid).
36
+
37
+ `create!` will raise an exception instead of return errors if there is a failure of any sort in the document generation process. It otherwise works in the same way as `create`.
38
+
39
+ If the document is processed asynchronously, a status id will be returned as opposed to the contents of the document. You can then use <METHOD NAME> to get the status of the document. You can pass in a URL to `:callback_url` to be called once an asynchronous job is complete. It will be passed a value of `download_url` which will contain a URL that when visited will provide you with your document. This option does nothing if `:async` is not true.
40
+
41
+ The `create` call can also take a block, like so:
42
+
43
+ ```ruby
44
+ DocRaptor.create(:document_content => content) do |file, response|
45
+ #file is a tempfile holding the response body
46
+ #reponse is the HTTParty response object
47
+ end
48
+ ```
37
49
 
38
50
  To get the status of an async request, you can call:
39
-
40
- # uses the id of the most recently created async job
41
- DocRaptor.status
42
- # query some other async job and make it the "active" async job for the DocRaptor class
43
- DocRaptor.status(status_id)
44
51
 
45
- status_id is the value returned from DocRaptor.create when :async is true. If you have
52
+ ```ruby
53
+ # uses the id of the most recently created async job
54
+ DocRaptor.status
55
+ # query some other async job and make it the "active" async job for the DocRaptor class
56
+ DocRaptor.status(status_id)
57
+ ```
58
+
59
+ `status_id` is the value returned from `DocRaptor.create` when `:async` is true. If you have
46
60
  just created a document, status_id defaults to the last status_id received from DocRaptor.
47
61
  This will return a hash containing, at the very least, a key of "status" with a value of
48
62
  one of the following: { "completed", "failed", "killed", "queued", "working" }. If the
@@ -60,17 +74,14 @@ for the failure to generate your document.
60
74
  To download an async document, you can visit the URL (download_url) provided via the status
61
75
  function or you can call:
62
76
 
63
- # uses the key of the most recently checked async job which is complete
64
- DocRaptor.download
65
- # use some other complete doc's download key
66
- DocRaptor.download(download_key)
67
-
77
+ ```ruby
78
+ # uses the key of the most recently checked async job which is complete
79
+ DocRaptor.download
80
+ # use some other complete doc's download key
81
+ DocRaptor.download(download_key)
82
+ ```
68
83
 
69
- download_key is the value from the status hash of a call to DocRaptor.status of a
70
- completed job. If you have just checked the status of a document and it is completed,
71
- download_key defaults to that of the document you just checked. The download function
72
- works like DocRaptor.create in that you get back either an HTTParty response object or
73
- you can give it a block.
84
+ `download_key` is the value from the status hash of a call to DocRaptor.status of a completed job. If you have just checked the status of a document and it is completed, `download_key` defaults to that of the document you just checked. The download function works like DocRaptor.create in that you get back either an HTTParty response object or you can give it a block.
74
85
 
75
86
  ## Examples ###################################################################
76
87
  Check the examples directory for some simple examples. To make them work, you will need to have the docraptor gem installed (via bundler or gem install).
data/lib/doc_raptor.rb CHANGED
@@ -1,28 +1,66 @@
1
1
  require "httparty"
2
2
  require "tempfile"
3
3
 
4
+ module DocRaptorError
5
+ class NoApiKeyProvidedError < RuntimeError; end
6
+ class NoContentError < ArgumentError; end
7
+ end
8
+
9
+ module DocRaptorException
10
+ class DocRaptorRequestException < StandardError
11
+ attr_accessor :status_code
12
+ attr_accessor :message
13
+ def initialize(message, status_code)
14
+ self.message = message
15
+ self.status_code = status_code
16
+ super message
17
+ end
18
+
19
+ def to_s
20
+ "#{self.class.name}\nHTTP Status: #{status_code}\nReturned: #{message}"
21
+ end
22
+
23
+ def inspect
24
+ self.to_s
25
+ end
26
+ end
27
+ class DocumentCreationFailure < DocRaptorRequestException; end
28
+ class DocumentListingFailure < DocRaptorRequestException; end
29
+ class DocumentStatusFailure < DocRaptorRequestException; end
30
+ class DocumentDownloadFailure < DocRaptorRequestException; end
31
+ end
32
+
4
33
  class DocRaptor
5
34
  include HTTParty
6
35
 
7
36
  def self.api_key(key = nil)
8
- return default_options[:api_key] unless key
9
- default_options[:api_key] = key
37
+ default_options[:api_key] = key ? key : default_options[:api_key] || ENV["DOCRAPTOR_API_KEY"]
38
+ default_options[:api_key] || raise(DocRaptorError::NoApiKeyProvidedError.new("No API key provided"))
39
+ end
40
+
41
+ def self.create!(options = {})
42
+ raise ArgumentError.new "please pass in an options hash" unless options.is_a? Hash
43
+ self.create(options.merge({:raise_exception_on_failure => true}))
10
44
  end
11
45
 
12
46
  # when given a block, hands the block a TempFile of the resulting document
13
47
  # otherwise, just returns the response
14
48
  def self.create(options = { })
49
+ raise ArgumentError.new "please pass in an options hash" unless options.is_a? Hash
15
50
  if options[:document_content].blank? && options[:document_url].blank?
16
- raise "must supply :document_content or :document_url"
51
+ raise DocRaptorError::NoContentError.new("must supply :document_content or :document_url")
17
52
  end
18
-
53
+
19
54
  default_options = {
20
- :name => "default",
21
- :document_type => "pdf",
22
- :test => false,
23
- :async => false
55
+ :name => "default",
56
+ :document_type => "pdf",
57
+ :test => false,
58
+ :async => false,
59
+ :raise_exception_on_failure => false
24
60
  }
25
61
  options = default_options.merge(options)
62
+ raise_exception_on_failure = options[:raise_exception_on_failure]
63
+ options.delete :raise_exception_on_failure
26
64
 
27
65
  query = { }
28
66
  if options[:async]
@@ -41,6 +79,10 @@ class DocRaptor
41
79
 
42
80
  response = post("/docs", :body => {:doc => options}, :basic_auth => { :username => api_key }, :query => query)
43
81
 
82
+ if raise_exception_on_failure && !response.success?
83
+ raise DocRaptorException::DocumentCreationFailure.new response.body, response.code
84
+ end
85
+
44
86
  if block_given?
45
87
  ret_val = nil
46
88
  Tempfile.open("docraptor") do |f|
@@ -58,19 +100,40 @@ class DocRaptor
58
100
  end
59
101
  end
60
102
 
103
+ def self.list_docs!(options = { })
104
+ raise ArgumentError.new "please pass in an options hash" unless options.is_a? Hash
105
+ self.list_docs(options.merge({:raise_exception_on_failure => true}))
106
+ end
107
+
61
108
  def self.list_docs(options = { })
109
+ raise ArgumentError.new "please pass in an options hash" unless options.is_a? Hash
62
110
  default_options = {
63
111
  :page => 1,
64
- :per_page => 100
112
+ :per_page => 100,
113
+ :raise_exception_on_failure => false
65
114
  }
66
115
  options = default_options.merge(options)
67
-
68
- get("/docs", :query => options, :basic_auth => { :username => api_key })
116
+ raise_exception_on_failure = options[:raise_exception_on_failure]
117
+ options.delete :raise_exception_on_failure
118
+
119
+ response = get("/docs", :query => options, :basic_auth => { :username => api_key })
120
+ if raise_exception_on_failure && !response.success?
121
+ raise DocRaptorException::DocumentListingFailure.new response.body, response.code
122
+ end
123
+ response
69
124
  end
70
-
71
- def self.status(id = self.status_id)
125
+
126
+ def self.status!(id = self.status_id)
127
+ self.status(id, true)
128
+ end
129
+
130
+ def self.status(id = self.status_id, raise_exception_on_failure = false)
72
131
  response = get("/status/#{id}", :basic_auth => { :username => api_key }, :output => 'json')
73
132
 
133
+ if raise_exception_on_failure && !response.success?
134
+ raise DocRaptorException::DocumentStatusFailure.new response.body, response.code
135
+ end
136
+
74
137
  json = response.parsed_response
75
138
  if json['status'] == 'completed'
76
139
  self.download_key = json['download_url'].match(/.*?\/download\/(.+)/)[1]
@@ -79,8 +142,17 @@ class DocRaptor
79
142
  json
80
143
  end
81
144
 
82
- def self.download(key = self.download_key)
145
+ def self.download!(key = self.download_key)
146
+ self.download(key, true)
147
+ end
148
+
149
+ def self.download(key = self.download_key, raise_exception_on_failure = false)
83
150
  response = get("/download/#{key}")
151
+
152
+ if raise_exception_on_failure && !response.success?
153
+ raise DocRaptorException::DocumentDownloadFailure.new response.body, response.code
154
+ end
155
+
84
156
  if block_given?
85
157
  ret_val = nil
86
158
  Tempfile.open("docraptor") do |f|
@@ -102,6 +174,4 @@ class DocRaptor
102
174
  end
103
175
 
104
176
  base_uri ENV["DOCRAPTOR_URL"] || "https://docraptor.com/"
105
- api_key ENV["DOCRAPTOR_API_KEY"]
106
-
107
177
  end
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 6
10
- version: 0.1.6
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Kuehl
@@ -45,6 +45,7 @@ extensions: []
45
45
  extra_rdoc_files: []
46
46
 
47
47
  files:
48
+ - Changelog.md
48
49
  - README.md
49
50
  - MIT-LICENSE
50
51
  - lib/doc_raptor.rb