Class: PrintNode::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/printnode/client.rb

Overview

Handles all requests and API access.

Author:

  • Jake Torrance

  • PrintNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth, api_url = 'https://api.printnode.com') ⇒ Client

Initializes auth object, api url and headers.

Parameters:

  • auth (PrintNode::Auth)

    auth object with credentials.

  • api_url (String) (defaults to: 'https://api.printnode.com')

    api_url to be used in requests.

See Also:



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

#headersObject (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.

Parameters:

  • description (String)

    Description of the API-Key to be gotten.



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.

Parameters:

  • creator_ref (String)

    the creator reference of the Child Account.



82
83
84
# File 'lib/printnode/client.rb', line 82

def (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.

Parameters:

  • email (String)

    the email of the Child Account.



75
76
77
# File 'lib/printnode/client.rb', line 75

def (email)
  @headers = { 'X-Child-Account-By-Email' => email }
end

#child_account_by_id(id) ⇒ Object

Sets authentication via an id of a Child Account.

Parameters:

  • id (int)

    The id of the Child Account.



68
69
70
# File 'lib/printnode/client.rb', line 68

def (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.

Parameters:

  • uuid (String)

    the UUID of the client

  • edition (String)

    the edition of the client

  • version (String)

    The version of the client



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.

Parameters:

  • client_set (String) (defaults to: '')

    a set of the clients to be got

See Also:



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.

Parameters:

  • computer_set (String) (defaults to: '')

    a set of the computers to be got.

See Also:



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.

Parameters:

  • account (PrintNode::Account)

    Account object for new user.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :ApiKeys (Array[String])

    Array of apikey descriptions to be created for this account.

  • :Tags (Hash)

    tag_name => tag_value hash of tags to be added for this user.

See Also:



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/printnode/client.rb', line 190

def (, options = {})
  hash = {}
  hash['Account'] = .to_hash
  if options
    options.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.

Parameters:

  • description (String)

    Description of the API-Key to be made.



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.

Parameters:

  • printjob (PrintNode::PrintJob)

    printjob object to be submitted.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :options (Hash)

    a hash of any of the options available on the API docs.

  • :expireAfter (int)

    Number of seconds until printjob expires.

  • :qty (int)

    how many times this printjob will be sent to the server.

  • :authentication (Hash)

    A hash of an authentication object found on the API docs.

See Also:



424
425
426
427
428
429
430
431
432
# File 'lib/printnode/client.rb', line 424

def create_printjob(printjob, options = {})
  hash = printjob.to_hash
  if options
    options.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.

Parameters:

  • end_point_url (String)

    To be appended onto api_url to be used in 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.

Returns:

  • (Boolean)


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.

Parameters:

  • description (String)

    Description of the API-Key to be deleted.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Parameters:

  • end_point_url (String)

    To be appended onto api_url to be used in 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.

Parameters:

  • response (Net::HTTPResponse)

    A response from any of the request methods.



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.message
  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.

Parameters:

  • os (String) (defaults to: 'windows')

    the OS of the client to be found.

See Also:



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.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :firstname (String)

    new Firstname of user.

  • :lastname (String)

    new Last+‘]’)ions [String] :password new Password of user.

  • :email (String)

    new Email of user.

  • :creatorRef (String)

    new creator reference of user.

See Also:



212
213
214
215
216
# File 'lib/printnode/client.rb', line 212

def (options = {})
  hash = options.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.

Parameters:

  • client_set (String)

    a set of have their settings changed

  • enabled (Boolean)

    whether we want to enable (true) or disable (false) the clients.



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.

Parameters:

  • array (Array)

    the array we want to parse.



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.

Parameters:

  • hash (Hash)

    the hash we want to parse.



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.

Parameters:

  • end_point_url (String)

    To be appended onto api_url to be used in the request.

  • data (defaults to: nil)

    Data object to be encoded into JSON. If not used, nothing is put in the body 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.

Parameters:

  • end_point_url (String)

    To be appended onto api_url to be used in the request.

  • data (defaults to: nil)

    Data object to be encoded into JSON. If not used, nothing is put in the body 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.

Parameters:

  • set_a (String) (defaults to: '')

    if set_b used: set of computers relative to printers set in set_b.

  • set_b (String) (defaults to: nil)

    set of printers.

See Also:



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.

Parameters:

  • set_a (String) (defaults to: '')

    if set_b used: set of printers relative to printjobs set in set_b.

  • set_b (String) (defaults to: nil)

    set of printjobs.

See Also:



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.

Parameters:

  • tag_name (String)

    the name of the tag to be created.

  • tag_value (String)

    the name of the tag value to be created.



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.

Parameters:

  • request (Net::HTTPGenericRequest)

    request to be done.



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.

Parameters:

  • printjob_set (String) (defaults to: '')

    set of printjobs that we will get states for.

See Also:



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.

Parameters:

  • tag_name (String)

    the name of the tag to be gotten.



232
233
234
235
# File 'lib/printnode/client.rb', line 232

def tags(tag_name)
  end_point_url = '/account/tag/' + escape_with_types(tag_name)
  JSON.parse('[' + get(end_point_url).body + ']')[0]
end

#whoamiObject

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