crocodoc 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+ .DS_Store
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://gems.github.com"
2
+ gemspec
@@ -0,0 +1,195 @@
1
+ # crocodoc-ruby
2
+
3
+ ## Introduction
4
+
5
+ crocodoc-ruby is a Ruby wrapper for the Crocodoc API.
6
+ The Crocodoc API lets you upload documents and then generate secure and customized viewing sessions for them.
7
+ Our API is based on REST principles and generally returns JSON encoded responses,
8
+ and in Ruby are converted to hashes unless otherwise noted.
9
+
10
+ ## Installation
11
+ You can get the library by cloning or downloading the repo.
12
+
13
+ To clone:
14
+
15
+ git clone git@github.com:crocodoc/crocodoc-ruby.git
16
+
17
+ To download:
18
+
19
+ wget https://github.com/crocodoc/crocodoc-ruby/zipball/master -O crocodoc-ruby.zip
20
+ unzip crocodoc-ruby.zip
21
+ mv crocodoc-crocodoc-ruby-* crocodoc-ruby
22
+
23
+ Require the library into any of your Ruby files.
24
+
25
+ If you have the gem installed:
26
+
27
+ require 'crocodoc'
28
+
29
+ If you have the files locally:
30
+
31
+ require_relative /path/to/crocodoc-ruby/crocodoc.rb
32
+
33
+ Please note that this library also requires that the 'rest-client' gem is installed, and a version of Ruby >= 1.9.
34
+
35
+ ## Getting Started
36
+
37
+ You can see a number of examples on how to use this library in examples.rb.
38
+ These examples are interactive and you can run this file to see crocodoc-ruby in action.
39
+
40
+ To run these examples, open up examples.rb and change this line to show your API token:
41
+
42
+ Crocodoc.api_token = 'YOUR_API_TOKEN'
43
+
44
+ Save the file, make sure the example-files directory is writeable, and then run examples.rb:
45
+
46
+ ruby examples.rb
47
+
48
+ You should see 15 examples run with output in your terminal.
49
+ You can inspect the examples.rb code to see each API call being used.
50
+
51
+ To start using crocodoc-ruby in your code, set your API token:
52
+
53
+ Crocodoc.api_token = 'YOUR_API_TOKEN'
54
+
55
+ And now you can start using the methods in Crocodoc::Document, Crocodoc::Download, and Crocodoc::Session.
56
+
57
+ Read on to find out more how to use crocodoc-ruby.
58
+ You can also find more detailed information about our API here:
59
+ https://crocodoc.com/docs/api/
60
+
61
+ ## Using the Crocodoc API Library
62
+
63
+ ### Errors
64
+
65
+ Errors are handled by throwing exceptions.
66
+ We throw instances of CrocodocError.
67
+
68
+ Note that any Crocodoc API call can throw an exception.
69
+ When making API calls, put them in a begin/rescue block.
70
+ You can see examples.rb to see working code for each method using begin/rescue blocks.
71
+
72
+ ### Document
73
+
74
+ These methods allow you to upload, check the status of, and delete documents.
75
+
76
+ #### Upload
77
+
78
+ https://crocodoc.com/docs/api/#doc-upload
79
+ To upload a document, use Crocodoc::Document.upload().
80
+ Pass in a url (as a string) or a file resource object.
81
+ This function returns a UUID of the file.
82
+
83
+ // with a url
84
+ uuid = Crocodoc::Document.upload(url)
85
+
86
+ // with a file
87
+ file_handle = File.open(file_path, 'r')
88
+ uuid = Crocodoc::Document.upload(file_handle)
89
+
90
+ #### Status
91
+
92
+ https://crocodoc.com/docs/api/#doc-status
93
+ To check the status of one or more documents, use Crocodoc::Document.status().
94
+ Pass in the UUID of the file or an array of UUIDS you want to check the status of.
95
+ This function returns a hash containing a "status" string" and a "viewable" boolean.
96
+ If you passed in an array instead of a string, this function returns an array of hashes containing the status for each file.
97
+
98
+ // $status contains status['status'] and status['viewable']
99
+ status = Crocodoc::Document.status(uuid)
100
+
101
+ // statuses contains an array of status hashes
102
+ statuses = Crocodoc::Document.status([uuid, uuid2])
103
+
104
+ #### Delete
105
+
106
+ https://crocodoc.com/docs/api/#doc-delete
107
+ To delete a document, use Crocodoc::Document.delete().
108
+ Pass in the UUID of the file you want to delete.
109
+ This function returns a boolean of whether the document was successfully deleted or not.
110
+
111
+ deleted = Crocodoc::Document.delete(uuid)
112
+
113
+ ### Download
114
+
115
+ These methods allow you to download documents from Crocodoc in different ways.
116
+ You can download originals, PDFs, extracted text, and thumbnails.
117
+
118
+ #### Document
119
+
120
+ https://crocodoc.com/docs/api/#dl-doc
121
+ To download a document, use Crocodoc::Download.document().
122
+ Pass in the uuid,
123
+ an optional boolean of whether or not the file should be downloaded as a PDF,
124
+ an optional boolean of whether or not the file should be annotated,
125
+ and an optional filter string.
126
+ This function returns the file contents as a string, which you probably want to save to a file.
127
+
128
+ // with no optional arguments
129
+ file = Crocodoc::Download.document(uuid)
130
+ file_handle.write(file)
131
+
132
+ // with all optional arguments
133
+ file = Crocodoc::Download.document(uuid, true, true, 'all')
134
+ file_handle.write(file)
135
+
136
+ #### Thumbnail
137
+
138
+ https://crocodoc.com/docs/api/#dl-thumb
139
+ To download a thumbnail, use Crocodoc::Download.thumbnail().
140
+ Pass in the uuid and optionally the width and height.
141
+ This function returns the file contents as a string, which you probably want to save to a file.
142
+
143
+ // with no optional size arguments
144
+ thumbnail = Crocodoc::Download.thumbnail(uuid)
145
+ file_handle.write(thumbnail)
146
+
147
+ // with optional size arguments (width 77, height 100)
148
+ thumbnail = Crocodoc::Download.thumbnail(uuid, 77, 100)
149
+ file_handle.write(thumbnail)
150
+
151
+ #### Text
152
+
153
+ https://crocodoc.com/docs/api/#dl-text
154
+ To download extracted text from a document, use Crocodoc::Download.text().
155
+ Pass in the uuid.
156
+ This function returns the extracted text as a string.
157
+
158
+ text = Crocodoc::Download.text(uuid)
159
+
160
+ ### Session
161
+
162
+ The session method allows you to create a session for viewing documents in a secure manner.
163
+
164
+ #### Create
165
+
166
+ https://crocodoc.com/docs/api/#session-create
167
+ To get a session key, use Crocodoc::Session.create().
168
+ Pass in the uuid and optionally a params hash.
169
+ The params hash can contain an "is_editable" boolean,
170
+ a "user" hash with "id" and "name" fields,
171
+ a "filter" string, a "sidebar" string,
172
+ and booleans for "is_admin", "is_downloadable", "is_copyprotected", and "is_demo".
173
+ This function returns a session key.
174
+
175
+ // without optional params
176
+ session_key = Crocodoc::Session.create(uuid)
177
+
178
+ // with optional params
179
+ session_key = Crocodoc::Session.create(uuid, {
180
+ 'is_editable' => true,
181
+ 'user' => {
182
+ 'id' => 1,
183
+ 'name' => 'John Crocodile'
184
+ },
185
+ 'filter' => 'all',
186
+ 'is_admin' => true,
187
+ 'is_downloadable' => true,
188
+ 'is_copyprotected' => false,
189
+ 'is_demo' => false,
190
+ 'sidebar' => 'visible'
191
+ ))
192
+
193
+ ## Support
194
+
195
+ Please use github's issue tracker for API library support.
@@ -0,0 +1,18 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), 'lib'))
2
+
3
+ spec = Gem::Specification.new do |s|
4
+ s.name = 'crocodoc'
5
+ s.version = '1.0.0'
6
+ s.summary = 'Ruby wrapper for the Crocodoc API'
7
+ s.description = 'The Crocodoc API lets you upload documents and then generate secure and customized viewing sessions for them. See https://crocodoc.com for details.'
8
+ s.authors = ['Brandon Goldman']
9
+ s.email = ['brandon.goldman@gmail.com']
10
+ s.homepage = 'https://crocodoc.com/docs/api/'
11
+ s.require_paths = %w{lib}
12
+
13
+ s.add_dependency('rest-client', '~> 1.4')
14
+ s.add_dependency('json', '~> 1.1')
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.require_paths = ['lib']
18
+ end
Binary file
@@ -0,0 +1,363 @@
1
+ # = Examples
2
+
3
+ # Require libraries and set API token
4
+ require 'pathname'
5
+ require_relative 'lib/crocodoc'
6
+ Crocodoc.api_token = 'YOUR_API_TOKEN'
7
+
8
+ # == Example #1
9
+ #
10
+ # Upload a file to Crocodoc. We're uploading Form W4 from the IRS by URL.
11
+ puts 'Example #1 - Upload Form W4 from the IRS by URL.'
12
+ form_w4_url = 'http://www.irs.gov/pub/irs-pdf/fw4.pdf'
13
+ print ' Uploading... '
14
+ uuid = nil
15
+
16
+ begin
17
+ uuid = Crocodoc::Document.upload(form_w4_url)
18
+ puts 'success :)'
19
+ puts ' UUID is ' + uuid
20
+ rescue CrocodocError => e
21
+ puts 'failed :('
22
+ puts ' Error Code: ' + e.code
23
+ puts ' Error Message: ' + e.message
24
+ end
25
+
26
+ # == Example #2
27
+ #
28
+ # Check the status of the file from Example #1.
29
+ puts ''
30
+ puts 'Example #2 - Check the status of the file we just uploaded.'
31
+ print ' Checking status... '
32
+
33
+ begin
34
+ status = Crocodoc::Document.status(uuid)
35
+
36
+ unless status.has_key? 'error'
37
+ puts 'success :)'
38
+ puts ' File status is ' + status['status'] + '.'
39
+ puts ' File ' + (status['viewable'] ? 'is' : 'is not') + ' viewable.'
40
+ else
41
+ puts 'failed :('
42
+ puts ' Error Message: ' + status['error']
43
+ end
44
+ rescue CrocodocError => e
45
+ puts 'failed :('
46
+ puts ' Error Code: ' + e.code
47
+ puts ' Error Message: ' + e.message
48
+ end
49
+
50
+ # == Example #3
51
+ #
52
+ # Upload another file to Crocodoc. We're uploading Form W4 from the IRS as a PDF.
53
+ puts ''
54
+ puts 'Example #3 - Upload a sample .pdf as a file.'
55
+ uuid2 = nil
56
+ file_path = String(Pathname.new(File.expand_path(__FILE__)).dirname) + '/example-files/form-w4.pdf'
57
+
58
+ if File.exists? file_path
59
+ file_handle = File.open(file_path, 'r+')
60
+ print ' Uploading... '
61
+
62
+ begin
63
+ uuid2 = Crocodoc::Document.upload(file_handle)
64
+ puts 'success :)'
65
+ puts ' UUID is ' + uuid2
66
+ rescue CrocodocError => e
67
+ puts 'failed :('
68
+ puts ' Error Code: ' + e.code
69
+ puts ' Error Message: ' + e.message
70
+ end
71
+ else
72
+ puts ' Skipping because the sample pdf can\'t be found.'
73
+ end
74
+
75
+ # == Example #4
76
+ #
77
+ # Check the status of both files we uploaded in Examples #1 and #3.
78
+ puts ''
79
+ puts 'Example #4 - Check the status of both files at the same time.'
80
+ print ' Checking statuses... '
81
+
82
+ begin
83
+ statuses = Crocodoc::Document.status([uuid, uuid2])
84
+
85
+ if statuses
86
+ puts 'success :)'
87
+
88
+ unless statuses[0].has_key? 'error'
89
+ puts ' File #1 status is ' + statuses[0]['status'] + '.'
90
+ puts ' File #1 ' + (statuses[0]['viewable'] ? 'is' : 'is not') + ' viewable.'
91
+ else
92
+ puts ' File #1 failed :('
93
+ puts ' Error Message: ' . statuses[0]['error']
94
+ end
95
+
96
+ unless statuses[1].has_key? 'error'
97
+ puts ' File #2 status is ' + statuses[1]['status'] + '.'
98
+ puts ' File #2 ' + (statuses[1]['viewable'] ? 'is' : 'is not') + ' viewable.'
99
+ else
100
+ puts ' File #2 failed :('
101
+ puts ' Error Message: ' . statuses[1]['error']
102
+ end
103
+ else
104
+ puts 'failed :('
105
+ puts ' Statuses were not returned.'
106
+ end
107
+ rescue CrocodocError => e
108
+ puts 'failed :('
109
+ puts ' Error Code: ' + e.code
110
+ puts ' Error Message: ' + e.message
111
+ end
112
+
113
+ # == Example #5
114
+ #
115
+ # Wait ten seconds and check the status of both files again.
116
+ puts ''
117
+ puts 'Example #5 - Wait ten seconds and check the statuses again.'
118
+ print ' Waiting... '
119
+ sleep(10)
120
+ puts 'done.'
121
+ print ' Checking statuses... '
122
+
123
+ begin
124
+ statuses = Crocodoc::Document.status([uuid, uuid2])
125
+
126
+ if statuses
127
+ puts 'success :)'
128
+
129
+ unless statuses[0].has_key? 'error'
130
+ puts ' File #1 status is ' + statuses[0]['status'] + '.'
131
+ puts ' File #1 ' + (statuses[0]['viewable'] ? 'is' : 'is not') + ' viewable.'
132
+ else
133
+ puts ' File #1 failed :('
134
+ puts ' Error Message: ' . statuses[0]['error']
135
+ end
136
+
137
+ unless statuses[1].has_key? 'error'
138
+ puts ' File #2 status is ' + statuses[1]['status'] + '.'
139
+ puts ' File #2 ' + (statuses[1]['viewable'] ? 'is' : 'is not') + ' viewable.'
140
+ else
141
+ puts ' File #2 failed :('
142
+ puts ' Error Message: ' . statuses[1]['error']
143
+ end
144
+ else
145
+ puts 'failed :('
146
+ puts ' Statuses were not returned.'
147
+ end
148
+ rescue CrocodocError => e
149
+ puts 'failed :('
150
+ puts ' Error Code: ' + e.code
151
+ puts ' Error Message: ' + e.message
152
+ end
153
+
154
+ # == Example #6
155
+ #
156
+ # Delete the file we uploaded from Example #1.
157
+ puts ''
158
+ puts 'Example #6 - Delete the first file we uploaded.'
159
+ print ' Deleting... '
160
+
161
+ begin
162
+ deleted = Crocodoc::Document.delete(uuid)
163
+
164
+ if deleted
165
+ puts 'success :)'
166
+ puts ' File was deleted.'
167
+ else
168
+ print 'failed :('
169
+ end
170
+ rescue CrocodocError => e
171
+ puts 'failed :('
172
+ puts ' Error Code: ' + e.code
173
+ puts ' Error Message: ' + e.message
174
+ end
175
+
176
+ # == Example #7
177
+ #
178
+ # Download the file we uploaded from Example #3 as an original
179
+ puts ''
180
+ puts 'Example #7 - Download a file as an original.'
181
+ print ' Downloading... '
182
+
183
+ begin
184
+ file = Crocodoc::Download.document(uuid2)
185
+ filename = String(Pathname.new(File.expand_path(__FILE__)).dirname) + '/example-files/test-original.pdf'
186
+ file_handle = File.open(filename, 'w')
187
+ file_handle.write(file)
188
+ puts 'success :)'
189
+ puts ' File was downloaded to ' + filename + '.'
190
+ rescue CrocodocError => e
191
+ puts 'failed :('
192
+ puts ' Error Code: ' + e.code
193
+ puts ' Error Message: ' + e.message
194
+ end
195
+
196
+ # == Example #8
197
+ #
198
+ # Download the file we uploaded from Example #3 as a PDF
199
+ puts ''
200
+ puts 'Example #8 - Download a file as a PDF.'
201
+ print ' Downloading... '
202
+
203
+ begin
204
+ file = Crocodoc::Download.document(uuid2, true)
205
+ filename = String(Pathname.new(File.expand_path(__FILE__)).dirname) + '/example-files/test.pdf'
206
+ file_handle = File.open(filename, 'w')
207
+ file_handle.write(file)
208
+ puts 'success :)'
209
+ puts ' File was downloaded to ' + filename + '.'
210
+ rescue CrocodocError => e
211
+ puts 'failed :('
212
+ puts ' Error Code: ' + e.code
213
+ puts ' Error Message: ' + e.message
214
+ end
215
+
216
+ # == Example #9
217
+ #
218
+ # Download the file we uploaded from Example #3 with all options
219
+ puts ''
220
+ puts 'Example #9 - Download a file with all options.'
221
+ print ' Downloading... '
222
+
223
+ begin
224
+ file = Crocodoc::Download.document(uuid2, true, true, 'all')
225
+ filename = String(Pathname.new(File.expand_path(__FILE__)).dirname) + '/example-files/test-with-options.pdf'
226
+ file_handle = File.open(filename, 'w')
227
+ file_handle.write(file)
228
+ puts 'success :)'
229
+ puts ' File was downloaded to ' + filename + '.'
230
+ rescue CrocodocError => e
231
+ puts 'failed :('
232
+ puts ' Error Code: ' + e.code
233
+ puts ' Error Message: ' + e.message
234
+ end
235
+
236
+ # == Example #10
237
+ #
238
+ # Download the file we uploaded from Example #3 as a default thumbnail
239
+ puts ''
240
+ puts 'Example #10 - Download a default thumbnail from a file.'
241
+ print ' Downloading... '
242
+
243
+ begin
244
+ file = Crocodoc::Download.thumbnail(uuid2)
245
+ filename = String(Pathname.new(File.expand_path(__FILE__)).dirname) + '/example-files/thumbnail.png'
246
+ file_handle = File.open(filename, 'w')
247
+ file_handle.write(file)
248
+ puts 'success :)'
249
+ puts ' File was downloaded to ' + filename + '.'
250
+ rescue CrocodocError => e
251
+ puts 'failed :('
252
+ puts ' Error Code: ' + e.code
253
+ puts ' Error Message: ' + e.message
254
+ end
255
+
256
+ # == Example #11
257
+ #
258
+ # Download the file we uploaded from Example #3 as a large thumbnail
259
+ puts ''
260
+ puts 'Example #11 - Download a large thumbnail from a file.'
261
+ print ' Downloading... '
262
+
263
+ begin
264
+ file = Crocodoc::Download.thumbnail(uuid2, 250, 250)
265
+ filename = String(Pathname.new(File.expand_path(__FILE__)).dirname) + '/example-files/thumbnail-large.png'
266
+ file_handle = File.open(filename, 'w')
267
+ file_handle.write(file)
268
+ puts 'success :)'
269
+ puts ' File was downloaded to ' + filename + '.'
270
+ rescue CrocodocError => e
271
+ puts 'failed :('
272
+ puts ' Error Code: ' + e.code
273
+ puts ' Error Message: ' + e.message
274
+ end
275
+
276
+ # == Example #12
277
+ #
278
+ # Download extracted text from the file we uploaded from Example #3
279
+ puts ''
280
+ puts 'Example #12 - Download extracted text from a file.'
281
+ print ' Downloading... '
282
+
283
+ begin
284
+ file = Crocodoc::Download.text(uuid2)
285
+ filename = String(Pathname.new(File.expand_path(__FILE__)).dirname) + '/example-files/text.txt'
286
+ file_handle = File.open(filename, 'w')
287
+ file_handle.write(file)
288
+ puts 'success :)'
289
+ puts ' File was downloaded to ' + filename + '.'
290
+ rescue CrocodocError => e
291
+ puts 'failed :('
292
+ puts ' Error Code: ' + e.code
293
+ puts ' Error Message: ' + e.message
294
+ end
295
+
296
+ # == Example #13
297
+ #
298
+ # Create a session key for the file we uploaded from Example #3 with default
299
+ # options.
300
+ puts ''
301
+ puts 'Example #13 - Create a session key for a file with default options.'
302
+ print ' Creating... '
303
+ session_key = nil
304
+
305
+ begin
306
+ session_key = Crocodoc::Session.create(uuid2)
307
+ puts 'success :)'
308
+ puts ' The session key is ' + session_key + '.'
309
+ rescue CrocodocError => e
310
+ puts 'failed :('
311
+ puts ' Error Code: ' + e.code
312
+ puts ' Error Message: ' + e.message
313
+ end
314
+
315
+ # == Example #14
316
+ #
317
+ # Create a session key for the file we uploaded from Example #3 all of the
318
+ # options.
319
+ puts ''
320
+ puts 'Example #14 - Create a session key for a file with all of the options.'
321
+ print ' Creating...'
322
+ session_key = nil
323
+
324
+ begin
325
+ user = {'id' => 1,
326
+ 'name' => 'John Crocodoc'}
327
+ session_key = Crocodoc::Session.create(uuid2, {'isEditable' => true,
328
+ 'user' => user,
329
+ 'filter' => 'all',
330
+ 'is_admin' => true,
331
+ 'is_downloadable' => true,
332
+ 'is_copyprotected' => false,
333
+ 'is_demo' => false,
334
+ 'sidebar' => 'visible'})
335
+ puts 'success :)'
336
+ puts ' The session key is ' + session_key + '.'
337
+ rescue CrocodocError => e
338
+ puts 'failed :('
339
+ puts ' Error Code: ' + e.code
340
+ puts ' Error Message: ' + e.message
341
+ end
342
+
343
+ # == Example #15
344
+ #
345
+ # Delete the file we uploaded from Example #2.
346
+ puts ''
347
+ puts 'Example #15 - Delete the second file we uploaded.'
348
+ print ' Deleting... '
349
+
350
+ begin
351
+ deleted = Crocodoc::Document.delete(uuid2)
352
+
353
+ if deleted
354
+ puts 'success :)'
355
+ puts ' File was deleted.'
356
+ else
357
+ print 'failed :('
358
+ end
359
+ rescue CrocodocError => e
360
+ puts 'failed :('
361
+ puts ' Error Code: ' + e.code
362
+ puts ' Error Message: ' + e.message
363
+ end
@@ -0,0 +1,180 @@
1
+ # require core functionality
2
+ require 'json'
3
+ require 'rest-client'
4
+
5
+ # require our exception class
6
+ require_relative 'crocodoc_error'
7
+
8
+ # require the different crocodoc clients
9
+ require_relative 'crocodoc/document'
10
+ require_relative 'crocodoc/download'
11
+ require_relative 'crocodoc/session'
12
+
13
+ module Crocodoc
14
+ # The developer's Crocodoc API token
15
+ @@api_token = nil
16
+
17
+ # The default protocol (Crocodoc uses HTTPS)
18
+ @@protocol = 'https'
19
+
20
+ # The default host
21
+ @@host = 'crocodoc.com'
22
+
23
+ # The default base path on the server where the API lives
24
+ @@base_path = '/api/v2'
25
+
26
+ # Set the API token
27
+ def self.api_token=(api_token)
28
+ @@api_token = api_token
29
+ end
30
+
31
+ # Get the API token
32
+ def self.api_token
33
+ @@api_token
34
+ end
35
+
36
+ # Set the protocol
37
+ def self.protocol=(protocol)
38
+ @@protocol = protocol
39
+ end
40
+
41
+ # Get the protocol
42
+ def self.protocol
43
+ @@protocol
44
+ end
45
+
46
+ # Set the host
47
+ def self.host=(host)
48
+ @@host = host
49
+ end
50
+
51
+ # Get the host
52
+ def self.host
53
+ @@host
54
+ end
55
+
56
+ # Set the base path
57
+ def self.base_path=(base_path)
58
+ @@base_path = base_path
59
+ end
60
+
61
+ # Get the base path
62
+ def self.base_path
63
+ @@base_path
64
+ end
65
+
66
+ # Handle an error. We handle errors by throwing an exception.
67
+ #
68
+ # @param [String] error An error code representing the error
69
+ # (use_underscore_separators)
70
+ # @param [String] client Which API client the error is being called from
71
+ # @param [String] method Which method the error is being called from
72
+ # @param [Hash<String,>, String] response This is a hash of the response,
73
+ # usually from JSON, but can also be a string
74
+ #
75
+ # @raise [CrocodocError]
76
+ def self._error(error, client, method, response)
77
+ message = self.name + ': [' + error + '] ' + client + '.' + String(method) + "\r\n\r\n"
78
+ response = JSON.generate(response) if response.is_a? Hash
79
+ message += response
80
+ raise CrocodocError.new(message, error)
81
+ end
82
+
83
+ # Make an HTTP request. Some of the params are polymorphic - get_params and
84
+ # post_params.
85
+ #
86
+ # @param [String] path The path on the server to make the request to
87
+ # relative to the base path
88
+ # @param [String] method This is just an addition to the path, for example,
89
+ # in "/documents/upload" the method would be "upload"
90
+ # @param [Hash<String, String>] get_params A hash of GET params to be added
91
+ # to the URL
92
+ # @param [Hash<String, String>] post_params A hash of GET params to be added
93
+ # to the URL
94
+ # @param [Boolean] is_json Should the file be converted from JSON? Defaults to
95
+ # true.
96
+ #
97
+ # @return [Hash<String,>, String] The response hash is usually converted from
98
+ # JSON, but sometimes we just return the raw response from the server
99
+ # @raise [CrocodocError]
100
+ def self._request(path, method, get_params, post_params, is_json=true)
101
+ url = @@protocol + '://' + @@host + @@base_path + path + method
102
+
103
+ # add the API token to get_params
104
+ get_params = {} unless get_params
105
+ get_params['token'] = @@api_token
106
+
107
+ # add the API token to post_params
108
+ if post_params and post_params.length > 0
109
+ # add the API token to post_params
110
+ post_params['token'] = @@api_token
111
+ end
112
+
113
+ result = nil
114
+ http_code = nil
115
+
116
+ if post_params && post_params.length > 0
117
+ response = RestClient.post(url, post_params, params: get_params){|response, request, result| result }
118
+ result = RestClient::Request.decode(response['content-encoding'], response.body)
119
+ http_code = Integer(response.code)
120
+ else
121
+ response = RestClient.get(url, params: get_params){|response, request, result| result }
122
+ result = RestClient::Request.decode(response['content-encoding'], response.body)
123
+ http_code = Integer(response.code)
124
+ end
125
+
126
+ if is_json
127
+ json_decoded = false
128
+
129
+ if result == 'true'
130
+ json_decoded = true
131
+ elsif result == 'false'
132
+ json_decoded = false
133
+ else
134
+ json_decoded = JSON.parse(result)
135
+ end
136
+
137
+ if json_decoded == false
138
+ return self._error('server_response_not_valid_json', self.name, __method__, {
139
+ response => result,
140
+ get_params => get_params,
141
+ post_params => post_params
142
+ })
143
+ end
144
+
145
+ if json_decoded.is_a? Hash and json_decoded.has_key? 'error'
146
+ return self._error(json_decoded['error'], self.name, __method__, {
147
+ get_params => get_params,
148
+ post_params => post_params
149
+ })
150
+ end
151
+
152
+ result = json_decoded
153
+ end
154
+
155
+ http_4xx_error_codes = {'400' => 'bad_request',
156
+ '401' => 'unauthorized',
157
+ '404' => 'not_found',
158
+ '405' => 'method_not_allowed'}
159
+
160
+ if http_4xx_error_codes.has_key? http_code
161
+ error = 'server_error_' + http_code + '_' + http_4xx_error_codes[http_code]
162
+ return self._error(error, self.name, __method__, {
163
+ url => url,
164
+ get_params => get_params,
165
+ postParams => post_params
166
+ })
167
+ end
168
+
169
+ if http_code >= 500 and http_code < 600
170
+ error = 'server_error_' + http_code + '_unknown'
171
+ return self._error(error, self.name, __method__, {
172
+ url => url,
173
+ get_params => get_params,
174
+ post_params => post_params
175
+ })
176
+ end
177
+
178
+ result
179
+ end
180
+ end
@@ -0,0 +1,75 @@
1
+ module Crocodoc
2
+ # Provides access to the Crocodoc Document API. The Document API is used for
3
+ # uploading, checking status, and deleting documents.
4
+ class Document
5
+ # The Document API path relative to the base API path
6
+ @@path = '/document/'
7
+
8
+ # Set the path
9
+ def self.path=(path)
10
+ @@path = path
11
+ end
12
+
13
+ # Get the path
14
+ def self.path
15
+ @@path
16
+ end
17
+
18
+ # Delete a file on Crocodoc by UUID.
19
+ #
20
+ # @param [String] uuid The uuid of the file to delete
21
+ #
22
+ # @return [Boolean] Was the file deleted?
23
+ # @raise [CrocodocError]
24
+ def self.delete(uuid)
25
+ post_params = {'uuid' => uuid}
26
+ Crocodoc._request(self.path, 'delete', nil, post_params)
27
+ end
28
+
29
+ # Check the status of a file on Crocodoc by UUID. This method is
30
+ # polymorphic and can take an array of UUIDs and return an array of status
31
+ # hashes about those UUIDs, or can also take a one UUID string and return
32
+ # one status hash for that UUID.
33
+ #
34
+ # @param [Array<String>, String] uuids An array of the uuids of the file to
35
+ # check the status of - this can also be a single uuid string
36
+ #
37
+ # @return [Array<Hash<String,>>, Hash<String,>] An array of hashes (or just
38
+ # an hash if you passed in a string) of the uuid, status, and viewable
39
+ # bool, or an array of the uuid and an error
40
+ # @raise [CrocodocError]
41
+ def self.status(uuids)
42
+ is_single_uuid = uuids.is_a? String
43
+ uuids = [uuids] if is_single_uuid
44
+ get_params = {'uuids' => uuids.join(',')}
45
+ response = Crocodoc._request(self.path, 'status', get_params, nil)
46
+ is_single_uuid ? response[0] : response
47
+ end
48
+
49
+ # Upload a file to Crocodoc with a URL.
50
+ #
51
+ # @param url_or_file [String, File] The url of the file to upload or a file resource
52
+ #
53
+ # @return [String] The uuid of the newly-uploaded file
54
+ # @raise [CrocodocError]
55
+ def self.upload(url_or_file)
56
+ post_params = {}
57
+
58
+ if url_or_file.is_a? String
59
+ post_params['url'] = url_or_file
60
+ elsif url_or_file.is_a? File
61
+ post_params['file'] = url_or_file
62
+ else
63
+ return Crocodoc::_error('invalid_url_or_file_param', self.name, __method__, nil)
64
+ end
65
+
66
+ response = Crocodoc::_request(self.path, 'upload', nil, post_params)
67
+
68
+ unless response.has_key? 'uuid'
69
+ return Crocodoc::_error('missing_uuid', self.name, __method__, response)
70
+ end
71
+
72
+ response['uuid']
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,74 @@
1
+ module Crocodoc
2
+ # Provides access to the Crocodoc Download API. The Download API is used for
3
+ # downloading an original of a document, a PDF of a document, a thumbnail of a
4
+ # document, and text extracted from a document.
5
+ class Download
6
+ # The Download API path relative to the base API path
7
+ @@path = '/download/'
8
+
9
+ # Set the path
10
+ def self.path=(path)
11
+ @@path = path
12
+ end
13
+
14
+ # Get the path
15
+ def self.path
16
+ @@path
17
+ end
18
+
19
+ # Download a document's original file from Crocodoc. The file can
20
+ # optionally be downloaded as a PDF, as another filename, with
21
+ # annotations, and with filtered annotations.
22
+ #
23
+ # @param [String] uuid The uuid of the file to download
24
+ # @param [Boolean] is_pdf Should the file be downloaded as a PDF?
25
+ # @param [Boolean] is_annotated Should the file be downloaded with annotations?
26
+ # @param [String, Array<String>] filter Which annotations should be
27
+ # included if any - this is usually a string, but could also be an array
28
+ # if it's a comma-separated list of user IDs as the filter
29
+ #
30
+ # @return [String] The downloaded file contents as a string
31
+ # @raise CrocodocError
32
+ def self.document(uuid, is_pdf=false, is_annotated=false, filter=nil)
33
+ get_params = {'uuid' => uuid}
34
+ get_params['pdf'] = 'true' if is_pdf
35
+ get_params['annotated'] = 'true' if is_annotated
36
+
37
+ if filter
38
+ filter = filter.join(',') if filter.is_a? Array
39
+ get_params['filter'] = filter
40
+ end
41
+
42
+ Crocodoc._request(self.path, 'document', get_params, nil, false)
43
+ end
44
+
45
+ # Download a document's extracted text from Crocodoc.
46
+ #
47
+ # @param [String] uuid The uuid of the file to extract text from
48
+ #
49
+ # @return [String] The file's extracted text
50
+ # @raise CrocodocError
51
+ def self.text(uuid)
52
+ get_params = {'uuid' => uuid}
53
+ Crocodoc._request(self.path, 'text', get_params, nil, false)
54
+ end
55
+
56
+ # Download a document's thumbnail from Crocodoc with an optional size.
57
+ #
58
+ # @param [String] uuid The uuid of the file to download the thumbnail from
59
+ # @param [Integer] width The width you want the thumbnail to be
60
+ # @param [Integer] height The height you want the thumbnail to be
61
+ #
62
+ # @return [String] The downloaded thumbnail contents
63
+ # @raise CrocodocError
64
+ def self.thumbnail(uuid, width=nil, height=nil)
65
+ get_params = {'uuid' => uuid}
66
+
67
+ if width != nil and height != nil
68
+ get_params['size'] = String(width) + 'x' + String(height)
69
+ end
70
+
71
+ Crocodoc._request(self.path, 'thumbnail', get_params, nil, false)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,97 @@
1
+ module Crocodoc
2
+ # Provides access to the Crocodoc Session API. The Session API is used to
3
+ # to create sessions for specific documents that can be used to view a
4
+ # document using a specific session-based URL.
5
+ class Session
6
+ # The Session API path relative to the base API path
7
+ @@path = '/session/'
8
+
9
+ # Set the path
10
+ def self.path=(path)
11
+ @@path = path
12
+ end
13
+
14
+ # Get the path
15
+ def self.path
16
+ @@path
17
+ end
18
+
19
+ # Create a session for a specific document by UUID that is optionally
20
+ # editable and can use user ID and name info from your application,
21
+ # can filter annotations, can grant admin permissions, can be
22
+ # downloadable, can be copy-protected, and can prevent changes from being
23
+ # persisted.
24
+ #
25
+ # @param [String] uuid The uuid of the file to create a session for
26
+ # @param [Hash<String,>] params A hash representing:
27
+ # [Boolean] 'is_editable' Can users create annotations and comments while
28
+ # viewing the document with this session key?
29
+ # [Hash<String, String>] 'user' A hash with keys "id" and "name"
30
+ # representing a user's unique ID and name in your application; "id"
31
+ # must be a non-negative signed 32-bit integer; this field is required
32
+ # if is_editable is true
33
+ # [String, Array<String>] 'filter' Which annotations should be included
34
+ # if any - this is usually a string, but could also be an array if it's
35
+ # a comma-separated list of user IDs as the filter
36
+ # [Boolean] 'is_admin' Can users modify or delete any annotations or comments
37
+ # belonging to other users?
38
+ # [Boolean] 'is_downloadable' Can users download the original document?
39
+ # [Boolean] 'is_copyprotected' Can text be selected in the document?
40
+ # [Boolean] 'is_demo' Should we prevent any changes from being persisted?
41
+ # [String] 'sidebar' Sets if and how the viewer sidebar is included
42
+ #
43
+ # @return [String] A unique session key for the document
44
+ # @raise [CrocodocError]
45
+ def self.create(uuid, params = {})
46
+ post_params = {'uuid' => uuid}
47
+
48
+ if params.has_key? 'is_editable'
49
+ post_params['editable'] = params['is_editable'] ? 'true' : 'false'
50
+ end
51
+
52
+ if (
53
+ params.has_key? 'user' \
54
+ and params['user'] \
55
+ and params['user'].is_a? Hash \
56
+ and params['user'].has_key? 'id' \
57
+ and params['user'].has_key? 'name' \
58
+ )
59
+ post_params['user'] = String(params['user']['id']) + ',' + params['user']['name']
60
+ end
61
+
62
+ if params.has_key? 'filter'
63
+ if params['filter'].is_a? Array
64
+ params['filter'] = params['filter'].join(',')
65
+ end
66
+
67
+ post_params['filter'] = params['filter']
68
+ end
69
+
70
+ if params.has_key? 'is_admin'
71
+ post_params['admin'] = params['is_admin'] ? 'true' : 'false'
72
+ end
73
+
74
+ if params.has_key? 'is_downloadable'
75
+ post_params['downloadable'] = params['is_downloadable'] ? 'true' : 'false'
76
+ end
77
+
78
+ if params.has_key? 'is_copyprotected'
79
+ post_params['copyprotected'] = params['is_copyprotected'] ? 'true' : 'false'
80
+ end
81
+
82
+ if params.has_key? 'is_demo'
83
+ post_params['demo'] = params['is_demo'] ? 'true' : 'false'
84
+ end
85
+
86
+ post_params['sidebar'] = params['sidebar'] if params.has_key? 'sidebar'
87
+
88
+ session = Crocodoc._request(self.path, 'create', nil, post_params)
89
+
90
+ unless session.is_a? Hash and session.has_key? 'session'
91
+ return Crocodoc._error('missing_session_key', self.name, __method__, session)
92
+ end
93
+
94
+ session['session']
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,16 @@
1
+ # CrocodocError extends the default exception class.
2
+ # It adds a code field.
3
+ class CrocodocError < Exception
4
+ # An error code string
5
+ @code = nil
6
+
7
+ # Get the error code
8
+ def code
9
+ @code
10
+ end
11
+
12
+ def initialize(message, code)
13
+ @message = message
14
+ @code = code
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crocodoc
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Brandon Goldman
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-10-19 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rest-client
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 1
31
+ - 4
32
+ version: "1.4"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: json
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 13
44
+ segments:
45
+ - 1
46
+ - 1
47
+ version: "1.1"
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ description: The Crocodoc API lets you upload documents and then generate secure and customized viewing sessions for them. See https://crocodoc.com for details.
51
+ email:
52
+ - brandon.goldman@gmail.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files: []
58
+
59
+ files:
60
+ - .gitignore
61
+ - Gemfile
62
+ - README.md
63
+ - crocodoc.gemspec
64
+ - example-files/form-w4.pdf
65
+ - examples.rb
66
+ - lib/crocodoc.rb
67
+ - lib/crocodoc/document.rb
68
+ - lib/crocodoc/download.rb
69
+ - lib/crocodoc/session.rb
70
+ - lib/crocodoc_error.rb
71
+ homepage: https://crocodoc.com/docs/api/
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options: []
76
+
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.7.2
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Ruby wrapper for the Crocodoc API
104
+ test_files: []
105
+