Class: Printer

Inherits:
ApplicationRecord show all
Defined in:
app/models/printer.rb

Overview

== Schema Information

Table name: printers
Database name: primary

id :integer not null, primary key
capabilities :jsonb
computer_name :string not null
name :string not null
state :string not null
created_at :datetime not null
updated_at :datetime not null
print_node_id :integer not null
store_id :integer

Indexes

index_printers_on_print_node_id (print_node_id)

Has many collapse

Belongs to collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Class Method Details



36
37
38
39
40
# File 'app/models/printer.rb', line 36

def self.print_node_client
  auth_key = Heatwave::Configuration.fetch(:print_node_auth_key)
  auth = PrintNode::Auth.new(auth_key)
  client = PrintNode::Client.new(auth)
end

.refresh_printer_listObject



32
33
34
# File 'app/models/printer.rb', line 32

def self.refresh_printer_list
  Printers::RefreshPrinters.new.process
end

.select_optionsObject



28
29
30
# File 'app/models/printer.rb', line 28

def self.select_options
  all.order(:name).pluck(:name, :id)
end

Instance Method Details

#binsObject



42
43
44
# File 'app/models/printer.rb', line 42

def bins
  capabilities['bins']
end

#collate?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/models/printer.rb', line 50

def collate?
  capabilities['collate'].to_b
end

#color?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/models/printer.rb', line 54

def color?
  capabilities['color'].to_b
end

#duplex?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/models/printer.rb', line 58

def duplex?
  capabilities['duplex'].to_b
end

#full_name_arrayObject



62
63
64
# File 'app/models/printer.rb', line 62

def full_name_array
  [print_node_id,store&.name,computer_name, name, state].map(&:presence).compact
end

#mediasObject



46
47
48
# File 'app/models/printer.rb', line 46

def medias
  capabilities['medias']
end

see https://www.printnode.com/docs/api/curl/
title: A title to give the PrintJob. This is the name which will appear in the operating system's print queue.
content_type: Either 'pdf_uri', 'pdf_base64', 'raw_uri', 'raw_base64'
content: A uri accessible by the client when contentType is 'pdf_uri'.
or A base64 encoded representation of the pdf when contentType is 'pdf_base64'.
source: A text description of how the printjob was created or where the printjob originated.
options: An object describing various options which can be set for this PrintJob. See options. Printing options have no effect when raw printing.
https://github.com/PrintNode/PrintNode-Ruby/blob/master/examples/creating_a_printjob.rb



75
76
77
78
79
80
# File 'app/models/printer.rb', line 75

def print(content, content_type: :pdf_uri, name: nil, source: nil, payload: {})
  #raise "Invalid pdf_uri" if content_type != :pdf_uri && (URI.parse(content) rescue nil).nil?
  source ||= "heatwave"
  print_job = PrintNode::PrintJob.new(print_node_id, name, content_type.to_s, content, source)
  self.class.print_node_client.create_printjob(print_job,payload)
end

Returns:

See Also:



25
# File 'app/models/printer.rb', line 25

has_many :print_profiles, dependent: :destroy


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/printer.rb', line 82

def print_upload(upload, bin: nil, quantity: 1, duplex: 'long-edge', rotate: nil, fit_to_page: nil)
  t = upload.active_download_token
  pdf_uri = "https://#{CRM_HOSTNAME}/en-US/downloads/#{t}"
  #pdf_data = upload.attachment.data
  payload = {}
  options = {}
  options['bin'] = bin if bin.present?
  options['duplex'] = duplex if duplex.present?
  options['rotate'] = rotate if rotate.present?
  options['fit_to_page'] = fit_to_page unless fit_to_page.nil?
  options['copies'] = quantity

  payload = {
    'options': options,
    'expireAfter': 120000
  }
  ext = File.extname(upload.attachment_name)
  name = "#{upload.category}_#{upload.id}#{ext}"
  print(pdf_uri, name: name, payload: payload, content_type: :pdf_uri)
end

#storeStore

Returns:

See Also:



26
# File 'app/models/printer.rb', line 26

belongs_to :store, optional: true