Class: PrintNode::Client

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

Overview

Handles all requests and API access.

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.

See Also:



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

#headersObject (readonly)

Returns the value of attribute headers.



26
27
28
# File 'lib/printnode/client.rb', line 26

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

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:



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/printnode/client.rb', line 198

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.



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.

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:



440
441
442
443
444
445
446
447
448
# File 'lib/printnode/client.rb', line 440

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.



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.



21
22
23
24
# File 'lib/printnode/client.rb', line 21

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



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.

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:



220
221
222
223
224
# File 'lib/printnode/client.rb', line 220

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.



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.

See Also:



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



185
186
187
# File 'lib/printnode/client.rb', line 185

def whoami
  OpenStruct.new JSON.parse(get('/whoami/').body)
end