Module: Crocodoc

Defined in:
lib/crocodoc.rb,
lib/crocodoc/session.rb,
lib/crocodoc/version.rb,
lib/crocodoc/document.rb,
lib/crocodoc/download.rb

Defined Under Namespace

Classes: Document, Download, Session

Constant Summary collapse

VERSION =
'1.0.2'
@@api_token =

The developer’s Crocodoc API token

nil
@@protocol =

The default protocol (Crocodoc uses HTTPS)

'https'
@@host =

The default host

'crocodoc.com'
@@base_path =

The default base path on the server where the API lives

'/api/v2'

Class Method Summary collapse

Class Method Details

._error(error, client, method, response) ⇒ Object

Handle an error. We handle errors by throwing an exception.

Parameters:

  • error (String)

    An error code representing the error (use_underscore_separators)

  • client (String)

    Which API client the error is being called from

  • method (String)

    Which method the error is being called from

  • response (Hash<String,>, String)

    This is a hash of the response, usually from JSON, but can also be a string

Raises:



76
77
78
79
80
81
# File 'lib/crocodoc.rb', line 76

def self._error(error, client, method, response)
  message = self.name + ': [' + error + '] ' + client + '.' + String(method) + "\r\n\r\n"
  response = JSON.generate(response) if response.is_a? Hash
  message += response unless response.nil?
  raise CrocodocError.new(message, error)
end

._request(path, method, get_params, post_params, is_json = true) ⇒ Hash<String,>, String

Make an HTTP request. Some of the params are polymorphic - get_params and post_params.

Parameters:

  • path (String)

    The path on the server to make the request to relative to the base path

  • method (String)

    This is just an addition to the path, for example, in “/documents/upload” the method would be “upload”

  • get_params (Hash<String, String>)

    A hash of GET params to be added to the URL

  • post_params (Hash<String, String>)

    A hash of GET params to be added to the URL

  • is_json (Boolean) (defaults to: true)

    Should the file be converted from JSON? Defaults to true.

Returns:

  • (Hash<String,>, String)

    The response hash is usually converted from JSON, but sometimes we just return the raw response from the server

Raises:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/crocodoc.rb', line 100

def self._request(path, method, get_params, post_params, is_json=true)
  url = @@protocol + '://' + @@host + @@base_path + path + method

  # add the API token to get_params
  get_params = {} unless get_params
  get_params['token'] = @@api_token
  
  # add the API token to post_params
  if post_params and post_params.length > 0
    # add the API token to post_params
    post_params['token'] = @@api_token
  end

  result = nil
  http_code = nil
  
  if post_params && post_params.length > 0
    response = RestClient.post(url, post_params, params: get_params){|response, request, result| result }
    result = RestClient::Request.decode(response['content-encoding'], response.body)
    http_code = Integer(response.code)
  else
    response = RestClient.get(url, params: get_params){|response, request, result| result }
    result = RestClient::Request.decode(response['content-encoding'], response.body)
    http_code = Integer(response.code)
  end
  
  if is_json
    json_decoded = false
    
    if result == 'true'
      json_decoded = true
    elsif result == 'false' or result == ''
      json_decoded = false
    else
      json_decoded = JSON.parse(result)
    end

    if json_decoded == false
      return self._error('server_response_not_valid_json', self.name, __method__, {
        response: result,
        get_params: get_params,
        post_params: post_params
      })
    end
    
    if json_decoded.is_a? Hash and json_decoded.has_key? 'error'
      return self._error(json_decoded['error'], self.name, __method__, {
        get_params: get_params,
        post_params: post_params
      })
    end
      
    result = json_decoded
  end

  http_4xx_error_codes = {400 => 'bad_request',
                          401 => 'unauthorized',
                          404 => 'not_found',
                          405 => 'method_not_allowed'}
  
  if http_4xx_error_codes.has_key? http_code
    error = 'server_error_' + http_code.to_s + '_' + http_4xx_error_codes[http_code]
    return self._error(error, self.name, __method__, {
      url: url,
      get_params: get_params,
      post_params: post_params
    })
  end
  
  if http_code >= 500 and http_code < 600
    error = 'server_error_' + http_code.to_s + '_unknown'
    return self._error(error, self.name, __method__, {
      url: url,
      get_params: get_params,
      post_params: post_params
    })
  end
  
  result
end

.api_tokenObject

Get the API token



32
33
34
# File 'lib/crocodoc.rb', line 32

def self.api_token
  @@api_token
end

.api_token=(api_token) ⇒ Object

Set the API token



27
28
29
# File 'lib/crocodoc.rb', line 27

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

.base_pathObject

Get the base path



62
63
64
# File 'lib/crocodoc.rb', line 62

def self.base_path
  @@base_path
end

.base_path=(base_path) ⇒ Object

Set the base path



57
58
59
# File 'lib/crocodoc.rb', line 57

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

.hostObject

Get the host



52
53
54
# File 'lib/crocodoc.rb', line 52

def self.host
  @@host
end

.host=(host) ⇒ Object

Set the host



47
48
49
# File 'lib/crocodoc.rb', line 47

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

.protocolObject

Get the protocol



42
43
44
# File 'lib/crocodoc.rb', line 42

def self.protocol
  @@protocol
end

.protocol=(protocol) ⇒ Object

Set the protocol



37
38
39
# File 'lib/crocodoc.rb', line 37

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