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
-
._error(error, client, method, response) ⇒ Object
Handle an error.
-
._request(path, method, get_params, post_params, is_json = true) ⇒ Hash<String,>, String
Make an HTTP request.
-
.api_token ⇒ Object
Get the API token.
-
.api_token=(api_token) ⇒ Object
Set the API token.
-
.base_path ⇒ Object
Get the base path.
-
.base_path=(base_path) ⇒ Object
Set the base path.
-
.host ⇒ Object
Get the host.
-
.host=(host) ⇒ Object
Set the host.
-
.protocol ⇒ Object
Get the protocol.
-
.protocol=(protocol) ⇒ Object
Set the protocol.
Class Method Details
._error(error, client, method, response) ⇒ Object
Handle an error. We handle errors by throwing an exception.
76 77 78 79 80 81 |
# File 'lib/crocodoc.rb', line 76 def self._error(error, client, method, response) = self.name + ': [' + error + '] ' + client + '.' + String(method) + "\r\n\r\n" response = JSON.generate(response) if response.is_a? Hash += response unless response.nil? raise CrocodocError.new(, 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.
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_token ⇒ Object
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_path ⇒ Object
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 |
.host ⇒ Object
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 |
.protocol ⇒ Object
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 |