Class: PrintNode::Client
- Inherits:
-
Object
- Object
- PrintNode::Client
- Defined in:
- lib/printnode/client.rb
Overview
Handles all requests and API access.
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
Instance Method Summary collapse
-
#apikeys(description) ⇒ Object
Sends a GET request to /account/apikey/(description).
-
#child_account_by_creator_ref(creator_ref) ⇒ Object
Sets authentication via the creator reference of a Child Account.
-
#child_account_by_email(email) ⇒ Object
Sets authentication via an email of a Child Account.
-
#child_account_by_id(id) ⇒ Object
Sets authentication via an id of a Child Account.
-
#clientkeys(uuid, edition, version) ⇒ Object
Sends a GET request to /client/key/(uuid)?edition=(edition)&version=(version).
-
#clients(client_set = '') ⇒ Object
Sends a GET request to /download/clients/(client_set).
-
#computers(computer_set = '') ⇒ Object
Sends a GET request to /computers/(computer_set).
-
#create_account(account, options = {}) ⇒ Object
Sends a POST request to /account/.
-
#create_apikey(description) ⇒ Object
Sends a POST request to /account/apikey/(description).
-
#create_printjob(printjob, options = {}) ⇒ Object
Sends a POST request to /printjobs/.
-
#delete(end_point_url) ⇒ Object
Sends a DELETE request to the specified URL.
-
#delete_account? ⇒ Boolean
Sends a DELETE request to /account/.
-
#delete_apikey?(description) ⇒ Boolean
Sends a DELETE request to /account/apikey/(description).
-
#delete_tag?(tag_name) ⇒ Boolean
Sends a DELETE request to /account/tag/(tag_name).
- #escape_with_types(obj) ⇒ Object
-
#get(end_point_url) ⇒ Object
Sends a GET request to the specified URL.
-
#http_error_handler(response) ⇒ Object
Handles HTTP errors in the code.
-
#initialize(auth, api_url = 'https://api.printnode.com') ⇒ Client
constructor
Initializes auth object, api url and headers.
-
#latest_client(os = 'windows') ⇒ Object
Sends a GET request to /download/client/(os).
-
#modify_account(options = {}) ⇒ Object
Sends a PATCH request to /account/.
-
#modify_client_downloads(client_set, enabled) ⇒ Object
Sends a PATCH request to /download/clients/(client_set).
-
#parse_array_to_struct(array) ⇒ Object
parses any hashes in an array to OpenStructs.
-
#parse_hash_to_struct(hash) ⇒ Object
parses any hashes in a hash to OpenStructs.
-
#patch(end_point_url, data = nil) ⇒ Object
Sends a PATCH request to the specified URL.
-
#post(end_point_url, data = nil) ⇒ Object
Sends a POST request to the specified URL.
-
#printers(set_a = '', set_b = nil) ⇒ Object
Sends a GET request to /printers/(set_a), or: /computers/(set_a)/printers/(set_b) if set_b is used.
-
#printjobs(set_a = '', set_b = nil) ⇒ Object
Sends a GET request to /printjobs/(set_a), or: /printers/(set_a)/printjobs/(set_b) if set_b is used.
- #scales(computer_id) ⇒ Object
-
#set_tag(tag_name, tag_value) ⇒ Object
Sends a POST request to /account/tag/(tag_name).
-
#start_response(request, uri) ⇒ Object
Creates a response object out of a Net::HTTP::(request method).
-
#states(printjob_set = '') ⇒ Object
sends a GET request to /printjobs/(printjob_set)/states.
-
#tags(tag_name) ⇒ Object
Sends a GET request to /account/tag/(tag_name).
-
#whoami ⇒ Object
Sends a GET request to /whoami/.
Constructor Details
#initialize(auth, api_url = 'https://api.printnode.com') ⇒ Client
Initializes auth object, api url and headers.
25 26 27 28 29 |
# File 'lib/printnode/client.rb', line 25 def initialize(auth, api_url = 'https://api.printnode.com') @auth = auth @api_url = api_url @headers = {} end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
18 19 20 |
# File 'lib/printnode/client.rb', line 18 def headers @headers end |
Instance Method Details
#apikeys(description) ⇒ Object
Sends a GET request to /account/apikey/(description).
Returns:
The API-Key itself.
263 264 265 266 |
# File 'lib/printnode/client.rb', line 263 def apikeys(description) end_point_url = '/account/apikey/' + escape_with_types(description) JSON.parse('[' + get(end_point_url).body + ']')[0] end |
#child_account_by_creator_ref(creator_ref) ⇒ Object
Sets authentication via the creator reference of a Child Account.
82 83 84 |
# File 'lib/printnode/client.rb', line 82 def child_account_by_creator_ref(creator_ref) @headers = { 'X-Child-Account-By-CreatorRef' => creator_ref } end |
#child_account_by_email(email) ⇒ Object
Sets authentication via an email of a Child Account.
75 76 77 |
# File 'lib/printnode/client.rb', line 75 def child_account_by_email(email) @headers = { 'X-Child-Account-By-Email' => email } end |
#child_account_by_id(id) ⇒ Object
Sets authentication via an id of a Child Account.
68 69 70 |
# File 'lib/printnode/client.rb', line 68 def child_account_by_id(id) @headers = { 'X-Child-Account-By-Id' => id } end |
#clientkeys(uuid, edition, version) ⇒ Object
Sends a GET request to /client/key/(uuid)?edition=(edition)&version=(version)
Returns:
The Client-key that was gotten.
298 299 300 301 302 303 304 305 306 |
# File 'lib/printnode/client.rb', line 298 def clientkeys(uuid, edition, version) end_point_url = '/client/key/' + escape_with_types(uuid) + '?edition=' + escape_with_types(edition) + '&version=' + escape_with_types(version) JSON.parse('[' + get(end_point_url).body + ']')[0] end |
#clients(client_set = '') ⇒ Object
Sends a GET request to /download/clients/(client_set)
Returns:
An Array of OpenStruct objects. The design of this Object will be the same as the ones on the PrintNode API docs.
327 328 329 330 331 |
# File 'lib/printnode/client.rb', line 327 def clients(client_set = '') end_point_url = '/download/clients/' + escape_with_types(client_set) response_object = JSON.parse(get(end_point_url).body) parse_array_to_struct(response_object) end |
#computers(computer_set = '') ⇒ Object
Sends a GET request to /computers/(computer_set)
Returns:
An Array of OpenStruct objects. The design of this Object will be the same as the ones on the PrintNode API docs.
353 354 355 356 357 |
# File 'lib/printnode/client.rb', line 353 def computers(computer_set = '') end_point_url = '/computers/' + escape_with_types(computer_set) response_object = JSON.parse(get(end_point_url).body) parse_array_to_struct(response_object) end |
#create_account(account, options = {}) ⇒ Object
Sends a POST request to /account/.
Returns:
An OpenStruct object of the response. The design of this Object will be the same as the ones on the PrintNode API docs.
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/printnode/client.rb', line 190 def create_account(account, = {}) hash = {} hash['Account'] = account.to_hash if .each do |(k, v)| hash[k] = v end end response_object = JSON.parse(post('/account/', hash).body) parse_hash_to_struct(response_object) end |
#create_apikey(description) ⇒ Object
Sends a POST request to /account/apikey/(description).
Returns:
The API-Key that was created.
274 275 276 277 |
# File 'lib/printnode/client.rb', line 274 def create_apikey(description) end_point_url = '/account/apikey/' + escape_with_types(description) JSON.parse('[' + post(end_point_url).body + ']')[0] end |
#create_printjob(printjob, options = {}) ⇒ Object
Sends a POST request to /printjobs/.
Returns:
The id of the printjob that was created.
424 425 426 427 428 429 430 431 432 |
# File 'lib/printnode/client.rb', line 424 def create_printjob(printjob, = {}) hash = printjob.to_hash if .each do |(k, v)| hash[k] = v end end JSON.parse('[' + post('/printjobs/', hash).body + ']')[0] end |
#delete(end_point_url) ⇒ Object
Sends a DELETE request to the specified URL.
Returns:
A response object of the request.
108 109 110 111 112 113 114 115 116 |
# File 'lib/printnode/client.rb', line 108 def delete(end_point_url) uri = URI(@api_url + end_point_url) request = Net::HTTP::Delete.new(uri) @headers.each_with_index do |(k, v)| request[k] = v end request.basic_auth(@auth.credentials[0], @auth.credentials[1]) start_response(request, uri) end |
#delete_account? ⇒ Boolean
Sends a DELETE request to /account/.
Returns:
A boolean of whether the account was deleted or not.
222 223 224 |
# File 'lib/printnode/client.rb', line 222 def delete_account? JSON.parse('[' + delete('/account/').body + ']')[0] end |
#delete_apikey?(description) ⇒ Boolean
Sends a DELETE request to /account/apikey/(description).
Returns:
A boolean of whether the API-Key was deleted or not.
285 286 287 288 |
# File 'lib/printnode/client.rb', line 285 def delete_apikey?(description) end_point_url = '/account/apikey/' + escape_with_types(description) JSON.parse('[' + delete(end_point_url).body + ']')[0] end |
#delete_tag?(tag_name) ⇒ Boolean
Sends a DELETE request to /account/tag/(tag_name).
Returns:
A boolean of whether the tag was deleted or not.
252 253 254 255 |
# File 'lib/printnode/client.rb', line 252 def delete_tag?(tag_name) end_point_url = '/account/tag/' + escape_with_types(tag_name) JSON.parse('[' + delete(end_point_url).body + ']')[0] end |
#escape_with_types(obj) ⇒ Object
13 14 15 16 |
# File 'lib/printnode/client.rb', line 13 def escape_with_types(obj) obj = obj.to_s unless obj.is_a?(String) CGI.escape(obj) end |
#get(end_point_url) ⇒ Object
Sends a GET request to the specified URL.
Returns:
A response object of the request.
124 125 126 127 128 129 130 131 132 |
# File 'lib/printnode/client.rb', line 124 def get(end_point_url) uri = URI(@api_url + end_point_url) request = Net::HTTP::Get.new(uri) @headers.each_with_index do |(k, v)| request[k] = v end request.basic_auth(@auth.credentials[0], @auth.credentials[1]) start_response(request, uri) end |
#http_error_handler(response) ⇒ Object
Handles HTTP errors in the code. If the HTTP status code is not 2xx (OK), it will raise an error.
457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/printnode/client.rb', line 457 def http_error_handler(response) begin unless response.code.to_s.match('^2..') fail APIError.new(response.code), response.body end rescue APIError => e puts 'HTTP Error found: ' + e.object puts 'This was the body of the response: ' puts e. end end |
#latest_client(os = 'windows') ⇒ Object
Sends a GET request to /download/client/(os)
Returns:
An OpenStruct object of the response. The design of this Object will be the same as the ones on the PrintNode API docs.
315 316 317 318 |
# File 'lib/printnode/client.rb', line 315 def latest_client(os = 'windows') end_point_url = '/download/client/' + escape_with_types(os.downcase) OpenStruct.new JSON.parse(get(end_point_url).body) end |
#modify_account(options = {}) ⇒ Object
Sends a PATCH request to /account/.
Returns:
An OpenStruct object of the response. The design of this Object will be the same as the ones on the PrintNode API docs.
212 213 214 215 216 |
# File 'lib/printnode/client.rb', line 212 def modify_account( = {}) hash = .dup response_object = JSON.parse(patch('/account/', hash).body) parse_hash_to_struct(response_object) end |
#modify_client_downloads(client_set, enabled) ⇒ Object
Sends a PATCH request to /download/clients/(client_set)
Returns:
An Array of ints that are ids that were changed.
340 341 342 343 344 |
# File 'lib/printnode/client.rb', line 340 def modify_client_downloads(client_set, enabled) hash = { 'enabled' => enabled } end_point_url = '/download/clients/' + escape_with_types(client_set) JSON.parse(patch(end_point_url, hash).body) end |
#parse_array_to_struct(array) ⇒ Object
parses any hashes in an array to OpenStructs.
Returns:
An array with all hashes inside it made into OpenStructs.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/printnode/client.rb', line 37 def parse_array_to_struct(array) output = [] array.each do |h| if h.is_a?(Hash) output.push(parse_hash_to_struct(h)) elsif h.is_a?(Array) output.push(parse_array_to_struct(h)) else output.push(h) end end output end |
#parse_hash_to_struct(hash) ⇒ Object
parses any hashes in a hash to OpenStructs. Parses any arrays to check if they have hashes to parse. Creates an OpenStruct for the hash.
Returns:
A hash that is an OpenStruct, with all hashes inside it made into OpenStructs.
57 58 59 60 61 62 63 |
# File 'lib/printnode/client.rb', line 57 def parse_hash_to_struct(hash) hash.each do |(k, v)| hash[k] = parse_hash_to_struct(v) if v.is_a?(Hash) hash[k] = parse_array_to_struct(v) if v.is_a?(Array) end OpenStruct.new hash end |
#patch(end_point_url, data = nil) ⇒ Object
Sends a PATCH request to the specified URL.
Returns:
A response object of the request.
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/printnode/client.rb', line 141 def patch(end_point_url, data = nil) uri = URI(@api_url + end_point_url) request = Net::HTTP::Patch.new uri @headers.each_with_index do |(k, v)| request[k] = v end request.basic_auth(@auth.credentials[0], @auth.credentials[1]) request['Content-Type'] = 'application/json' request.body = data.to_json if data start_response(request, uri) end |
#post(end_point_url, data = nil) ⇒ Object
Sends a POST request to the specified URL.
Returns:
A response object of the request.
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/printnode/client.rb', line 160 def post(end_point_url, data = nil) uri = URI(@api_url + end_point_url) request = Net::HTTP::Post.new uri @headers.each_with_index do |(k, v)| request[k] = v end request.basic_auth(@auth.credentials[0], @auth.credentials[1]) request['Content-Type'] = 'application/json' request.body = data.to_json if data start_response(request, uri) end |
#printers(set_a = '', set_b = nil) ⇒ Object
Sends a GET request to /printers/(set_a), or: /computers/(set_a)/printers/(set_b) if set_b is used.
if set_b unused: set of printers to be got.
Returns:
An Array of OpenStruct objects. The design of this Object will be the same as the ones on the PrintNode API docs.
376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/printnode/client.rb', line 376 def printers(set_a = '', set_b = nil) if set_b end_point_url = '/computers/' + escape_with_types(set_a) + '/printers/' + escape_with_types(set_b) else end_point_url = '/printers/' + escape_with_types(set_a) end response_object = JSON.parse(get(end_point_url).body) parse_array_to_struct(response_object) end |
#printjobs(set_a = '', set_b = nil) ⇒ Object
Sends a GET request to /printjobs/(set_a), or: /printers/(set_a)/printjobs/(set_b) if set_b is used.
if set_b unused: set of printjobs to be got.
Returns:
An Array of OpenStruct objects. The design of this Object will be the same as the ones on the PrintNode API docs.
399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/printnode/client.rb', line 399 def printjobs(set_a = '', set_b = nil) if set_b end_point_url = '/printers/' + escape_with_types(set_a) + '/printjobs/' + escape_with_types(set_b) else end_point_url = '/printjobs/' + escape_with_types(set_a) end response_object = JSON.parse(get(end_point_url).body) parse_array_to_struct(response_object) end |
#scales(computer_id) ⇒ Object
359 360 361 362 363 364 365 |
# File 'lib/printnode/client.rb', line 359 def scales(computer_id) end_point_url = '/computer/' + escape_with_types(computer_id) + '/scales/' response_object = JSON.parse(get(end_point_url).body) parse_array_to_struct(response_object) end |
#set_tag(tag_name, tag_value) ⇒ Object
Sends a POST request to /account/tag/(tag_name).
Returns:
If this creates a tag, a String ‘created’ will be returned. If it updates one, ‘updated’ will be returned.
243 244 245 246 |
# File 'lib/printnode/client.rb', line 243 def set_tag(tag_name, tag_value) end_point_url = '/account/tag/' + escape_with_types(tag_name) JSON.parse('[' + post(end_point_url, tag_value).body + ']')[0] end |
#start_response(request, uri) ⇒ Object
Creates a response object out of a Net::HTTP::(request method).
Returns:
The response of this request.
92 93 94 95 96 97 98 99 100 |
# File 'lib/printnode/client.rb', line 92 def start_response(request, uri) response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme = 'https') do |http| http.request(request) end http_error_handler(response) response end |
#states(printjob_set = '') ⇒ Object
sends a GET request to /printjobs/(printjob_set)/states
Returns:
An Array of OpenStruct objects. The design of this Object will be the same as the ones on the PrintNode API docs.
441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/printnode/client.rb', line 441 def states(printjob_set = '') if printjob_set == '' end_point_url = '/printjobs/states/' else end_point_url = '/printjobs/' + escape_with_types(printjob_set) + '/states/' end response_object = JSON.parse(get(end_point_url).body) parse_array_to_struct(response_object) end |
#tags(tag_name) ⇒ Object
Sends a GET request to /account/tag/(tag_name).
Returns:
A string which is the value of the tag requested.
232 233 234 235 |
# File 'lib/printnode/client.rb', line 232 def (tag_name) end_point_url = '/account/tag/' + escape_with_types(tag_name) JSON.parse('[' + get(end_point_url).body + ']')[0] end |
#whoami ⇒ Object
Sends a GET request to /whoami/.
Returns:
An OpenStruct object of the response. The design of this Object will be the same as the ones on the PrintNode API docs.
177 178 179 |
# File 'lib/printnode/client.rb', line 177 def whoami OpenStruct.new JSON.parse(get('/whoami/').body) end |