doc_raptor 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +46 -4
  2. data/lib/doc_raptor.rb +43 -1
  3. metadata +7 -5
data/README.md CHANGED
@@ -11,16 +11,22 @@ not there, you can set it directly by calling:
11
11
 
12
12
  DocRaptor.api_key "My API Key Here"
13
13
 
14
- Once an API key is set, you can create documents by calling
14
+ Once an API key is set, you can create documents by calling:
15
15
 
16
- DocRaptor.create(:document_content => content, :document_type => "pdf", :test => false)
16
+ DocRaptor.create(:document_content => content, :document_type => "pdf", :test => false, :async => false, :callback_url => nil)
17
17
 
18
18
  This will return an HTTParty respsonse object, the body of which will be the new file
19
19
  (or errors, if the request was not valid). You can pass in "pdf" or "xls" as the
20
20
  :document_type - default is pdf. This determines the type of file that DocRaptor will create.
21
21
  You can pass in true or false for :test - default is false - and this turns on or off
22
- test mode for DocRaptor. The only required parameter is :document_content, which should be a
23
- string of html - this will be what DocRaptor turns into your document.
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.
24
30
 
25
31
  The create call can also take a block, like so:
26
32
 
@@ -29,6 +35,42 @@ The create call can also take a block, like so:
29
35
  #reponse is the HTTParty response object
30
36
  end
31
37
 
38
+ To get the status of an async request, you can call:
39
+ DocRaptor.status # uses the id of the most recently created async job
40
+ DocRaptor.status(status_id) # query some other async job and make it the "active" async job for the DocRaptor class
41
+
42
+ status_id is the value returned from DocRaptor.create when :async is true. If you have
43
+ just created a document, status_id defaults to the last status_id received from DocRaptor.
44
+ This will return a hash containing, at the very least, a key of "status" with a value of
45
+ one of the following: { "completed", "failed", "killed", "queued", "working" }. If the
46
+ status is "queued", no other information will be available. If the status is "working",
47
+ there will be a human readable message contained in "message" that gives further details
48
+ as to the state of the document. If the status is "complete" there will be a key of
49
+ "download_url" that will contain a 2 time use URL that, when visited, will provide your
50
+ document. There will also be a key of "download_key" that can be given to the
51
+ DocRaptor.download function to obtain your document. If the status is "killed", it means
52
+ the system had to abort your document generation process for an unknown reason, most
53
+ likely it was taking too long to generate. If the status is "failed" you can check the
54
+ "messages" value for a message and the "validation_errors" value for a more detailed reason
55
+ for the failure to generate your document.
56
+
57
+ To download an async document, you can visit the URL (download_url) provided via the status
58
+ function or you can call:
59
+
60
+ DocRaptor.download # uses the key of the most recently checked async job which is complete
61
+ DocRaptor.download(download_key) # use some other complete doc's download key
62
+
63
+ download_key is the value from the status hash of a call to DocRaptor.status of a
64
+ completed job. If you have just checked the status of a document and it is completed,
65
+ download_key defaults to that of the document you just checked. The download function
66
+ works like DocRaptor.create in that you get back either an HTTParty response object or
67
+ you can give it a block.
68
+
69
+ ## Examples ###################################################################
70
+ 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).
71
+
72
+ For more examples including a full rails example, check https://github.com/expectedbehavior/doc_raptor_examples.
73
+
32
74
  ## Meta #######################################################################
33
75
 
34
76
  Maintained by Expected Behavior
data/lib/doc_raptor.rb CHANGED
@@ -20,11 +20,18 @@ class DocRaptor
20
20
  :name => "default",
21
21
  :document_type => "pdf",
22
22
  :test => false,
23
+ :async => false,
23
24
  }
24
25
  options = default_options.merge(options)
26
+
27
+ query = { }
28
+ if options[:async]
29
+ query[:output => 'json']
30
+ end
25
31
 
26
- response = post("/docs", :body => {:doc => options}, :basic_auth => { :username => api_key })
27
32
 
33
+ response = post("/docs", :body => {:doc => options}, :basic_auth => { :username => api_key }, :query => query)
34
+
28
35
  if block_given?
29
36
  ret_val = nil
30
37
  Tempfile.open("docraptor") do |f|
@@ -35,6 +42,8 @@ class DocRaptor
35
42
  ret_val = yield f, response
36
43
  end
37
44
  ret_val
45
+ elsif options[:async]
46
+ self.status_id = response.parsed_response["status_id"]
38
47
  else
39
48
  response
40
49
  end
@@ -50,6 +59,39 @@ class DocRaptor
50
59
  get("/docs", :query => options, :basic_auth => { :username => api_key })
51
60
  end
52
61
 
62
+ def self.status(id = self.status_id)
63
+ response = get("/status/#{id}", :basic_auth => { :username => api_key }, :output => 'json')
64
+
65
+ json = response.parsed_response
66
+ if json['status'] == 'completed'
67
+ self.download_key = json['download_url'].match(/.*?\/download\/(.+)/)[1]
68
+ json['download_key'] = self.download_key
69
+ end
70
+ json
71
+ end
72
+
73
+ def self.download(key = self.download_key)
74
+ response = get("/download/#{key}")
75
+ if block_given?
76
+ ret_val = nil
77
+ Tempfile.open("docraptor") do |f|
78
+ f.sync = true
79
+ f.write(response.body)
80
+ f.rewind
81
+
82
+ ret_val = yield f, response
83
+ end
84
+ ret_val
85
+ else
86
+ response
87
+ end
88
+ end
89
+
90
+ class << self
91
+ attr_accessor :status_id
92
+ attr_accessor :download_key
93
+ end
94
+
53
95
  base_uri ENV["DOCRAPTOR_URL"] || "https://docraptor.com/"
54
96
  api_key ENV["DOCRAPTOR_API_KEY"]
55
97
 
metadata CHANGED
@@ -1,21 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: doc_raptor
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 4
10
+ version: 0.1.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Kuehl
14
+ - Joel Meador
15
+ - Chris Moore
14
16
  autorequire:
15
17
  bindir: bin
16
18
  cert_chain: []
17
19
 
18
- date: 2011-01-29 00:00:00 -05:00
20
+ date: 2011-06-24 00:00:00 -04:00
19
21
  default_executable:
20
22
  dependencies:
21
23
  - !ruby/object:Gem::Dependency
@@ -76,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
78
  requirements: []
77
79
 
78
80
  rubyforge_project:
79
- rubygems_version: 1.4.2
81
+ rubygems_version: 1.5.2
80
82
  signing_key:
81
83
  specification_version: 3
82
84
  summary: wrap up the api for DocRaptor nicely