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
If an argument is not a string, map it to a string so it can be escaped and put into a URL.
-
#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
Sends a GET request to /computer/(computer_id)/scales.
-
#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.
33 34 35 36 37 |
# File 'lib/printnode/client.rb', line 33 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.
13 14 15 |
# File 'lib/printnode/client.rb', line 13 def headers @headers end |
Instance Method Details
#apikeys(description) ⇒ Object
Sends a GET request to /account/apikey/(description).
Returns:
The API-Key itself.
271 272 273 274 |
# File 'lib/printnode/client.rb', line 271 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.
90 91 92 |
# File 'lib/printnode/client.rb', line 90 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.
83 84 85 |
# File 'lib/printnode/client.rb', line 83 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.
76 77 78 |
# File 'lib/printnode/client.rb', line 76 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.
306 307 308 309 310 311 312 313 314 |
# File 'lib/printnode/client.rb', line 306 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.
335 336 337 338 339 |
# File 'lib/printnode/client.rb', line 335 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.
361 362 363 364 365 |
# File 'lib/printnode/client.rb', line 361 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.
198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/printnode/client.rb', line 198 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.
282 283 284 285 |
# File 'lib/printnode/client.rb', line 282 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.
440 441 442 443 444 445 446 447 448 |
# File 'lib/printnode/client.rb', line 440 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.
116 117 118 119 120 121 122 123 124 |
# File 'lib/printnode/client.rb', line 116 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.
230 231 232 |
# File 'lib/printnode/client.rb', line 230 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.
293 294 295 296 |
# File 'lib/printnode/client.rb', line 293 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.
260 261 262 263 |
# File 'lib/printnode/client.rb', line 260 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
If an argument is not a string, map it to a string so it can be escaped and put into a URL.
Returns:
CGI.escaped object.
22 23 24 25 |
# File 'lib/printnode/client.rb', line 22 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.
132 133 134 135 136 137 138 139 140 |
# File 'lib/printnode/client.rb', line 132 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.
473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/printnode/client.rb', line 473 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.
323 324 325 326 |
# File 'lib/printnode/client.rb', line 323 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.
220 221 222 223 224 |
# File 'lib/printnode/client.rb', line 220 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.
348 349 350 351 352 |
# File 'lib/printnode/client.rb', line 348 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.
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/printnode/client.rb', line 45 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.
65 66 67 68 69 70 71 |
# File 'lib/printnode/client.rb', line 65 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.
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/printnode/client.rb', line 149 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.
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/printnode/client.rb', line 168 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.
392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/printnode/client.rb', line 392 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.
415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/printnode/client.rb', line 415 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
Sends a GET request to /computer/(computer_id)/scales
Returns:
An Array of OpenStruct objects. The design of this Object will be the same as the ones on the PrintNode API docs.
374 375 376 377 378 379 380 |
# File 'lib/printnode/client.rb', line 374 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.
251 252 253 254 |
# File 'lib/printnode/client.rb', line 251 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.
100 101 102 103 104 105 106 107 108 |
# File 'lib/printnode/client.rb', line 100 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.
457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/printnode/client.rb', line 457 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.
240 241 242 243 |
# File 'lib/printnode/client.rb', line 240 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.
185 186 187 |
# File 'lib/printnode/client.rb', line 185 def whoami OpenStruct.new JSON.parse(get('/whoami/').body) end |