Class: PrintNode::PrintJob

Inherits:
Object
  • Object
show all
Defined in:
lib/print_node/print_job.rb

Overview

This module provides API requests to Print Pdf files, Get printers detail, View Print Pdf file jobs.

Constant Summary collapse

ALL_PRINTERS =
'/printers'
PDF_URL =
'/printjobs'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection = nil) ⇒ PrintJob

Creates a new PrintNode::PrintJob instance.


16
17
18
# File 'lib/print_node/print_job.rb', line 16

def initialize(connection=nil)
  @connection ||= connection || PrintNode::Client.connection
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection


9
10
11
# File 'lib/print_node/print_job.rb', line 9

def connection
  @connection
end

Instance Method Details

#get_printersObject

Get all Printers


22
23
24
25
# File 'lib/print_node/print_job.rb', line 22

def get_printers
  response = @connection.get ALL_PRINTERS
  result = JSON.parse(response.body)
end

Print Pdf File

Parameters:

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

Options Hash (options):

  • :printer_id (Integer)

    Defines the printerId on which print is taken Note that this is a mandatory parameter

  • :title (String)

    title to take the file print Note that this is a mandatory parameter

  • :contentType (String)

    pass the contentType 'pdf_base64' Note that this is a mandatory parameter

  • :content (String)

    pass the content as pdf online url or file path where file is stored Note that this is a mandatory parameter

Returns:

  • id of print job


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/print_node/print_job.rb', line 43

def print_file(options={})
  begin
    pdf_content = open(options[:content]) {|io| io.read}
  rescue OpenURI::HTTPError
    return
  end
  main_content = Base64.encode64(pdf_content)
  params = {
    printerId:     options[:printer_id],
    title:         options[:title],
    contentType:   options[:contentType],
    content:       main_content,
    source:        "Web Interface"
  }
  response = @connection.post PDF_URL, params
end

#view_print_jobsObject

View All Print Jobs


62
63
64
65
# File 'lib/print_node/print_job.rb', line 62

def view_print_jobs
  response = @connection.get PDF_URL
  result = JSON.parse(response.body)
end