Class: Crocodoc::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/crocodoc/document.rb

Overview

Provides access to the Crocodoc Document API. The Document API is used for uploading, checking status, and deleting documents.

Constant Summary collapse

@@path =

The Document API path relative to the base API path

'/document/'

Class Method Summary collapse

Class Method Details

.delete(uuid) ⇒ Boolean

Delete a file on Crocodoc by UUID.

Parameters:

  • uuid (String)

    The uuid of the file to delete

Returns:

  • (Boolean)

    Was the file deleted?

Raises:



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

def self.delete(uuid)
  post_params = {uuid: uuid}
  Crocodoc._request(self.path, 'delete', nil, post_params)
end

.pathObject

Get the path



14
15
16
# File 'lib/crocodoc/document.rb', line 14

def self.path
  @@path
end

.path=(path) ⇒ Object

Set the path



9
10
11
# File 'lib/crocodoc/document.rb', line 9

def self.path=(path)
  @@path = path
end

.status(uuids) ⇒ Array<Hash<String,>>, Hash<String,>

Check the status of a file on Crocodoc by UUID. This method is polymorphic and can take an array of UUIDs and return an array of status hashes about those UUIDs, or can also take a one UUID string and return one status hash for that UUID.

Parameters:

  • uuids (Array<String>, String)

    An array of the uuids of the file to check the status of - this can also be a single uuid string

Returns:

  • (Array<Hash<String,>>, Hash<String,>)

    An array of hashes (or just an hash if you passed in a string) of the uuid, status, and viewable bool, or an array of the uuid and an error

Raises:



41
42
43
44
45
46
47
# File 'lib/crocodoc/document.rb', line 41

def self.status(uuids)
  is_single_uuid = !(uuids.is_a? Array)
  uuids = [uuids] if is_single_uuid
  get_params = {uuids: uuids.join(',')}
  response = Crocodoc._request(self.path, 'status', get_params, nil)
  is_single_uuid ? response[0] : response
end

.upload(url_or_file) ⇒ String

Returns The uuid of the newly-uploaded file.

Parameters:

  • url_or_file (String, File)

    The url of the file to upload or a file resource

Returns:

  • (String)

    The uuid of the newly-uploaded file

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/crocodoc/document.rb', line 55

def self.upload(url_or_file)
  post_params = {}
  
  if url_or_file.is_a? String
    post_params['url'] = url_or_file
  elsif url_or_file.respond_to?(:read) && url_or_file.respond_to?(:path)
  	post_params['file'] = url_or_file
  else
  	return Crocodoc::_error('invalid_url_or_file_param', self.name, __method__, nil)
  end
  
  response = Crocodoc::_request(self.path, 'upload', nil, post_params)
  
  unless response.has_key? 'uuid'
  	return Crocodoc::_error('missing_uuid', self.name, __method__, response)
  end
  
  response['uuid']
end