Class: LabelPrinter::PrintJob

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/label_printer/label_printer/print_job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(printer_name, label_class, options) ⇒ PrintJob

Returns a new instance of PrintJob


9
10
11
12
13
# File 'lib/label_printer/label_printer/print_job.rb', line 9

def initialize(printer_name, label_class, options)
  @printer_name = printer_name
  @label_class = label_class
  @options = options
end

Instance Attribute Details

#label_classObject (readonly)

Returns the value of attribute label_class


7
8
9
# File 'lib/label_printer/label_printer/print_job.rb', line 7

def label_class
  @label_class
end

#labelsObject (readonly)

Returns the value of attribute labels


7
8
9
# File 'lib/label_printer/label_printer/print_job.rb', line 7

def labels
  @labels
end

#optionsObject (readonly)

Returns the value of attribute options


7
8
9
# File 'lib/label_printer/label_printer/print_job.rb', line 7

def options
  @options
end

#printer_nameObject (readonly)

Returns the value of attribute printer_name


7
8
9
# File 'lib/label_printer/label_printer/print_job.rb', line 7

def printer_name
  @printer_name
end

Instance Method Details

#build_attributesObject


33
34
35
# File 'lib/label_printer/label_printer/print_job.rb', line 33

def build_attributes
  printer_name_attribute.merge(label_template_id_attribute).merge(labels_attribute)
end

#executeObject


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/label_printer/label_printer/print_job.rb', line 15

def execute
  begin
    attributes = build_attributes
    LabelPrinter::PmbClient.print(attributes)
  rescue LabelPrinter::PmbException => e
    errors.add(:printmybarcode, e)
    return false
  rescue BarcodePrinter::BarcodePrinterException => e
    errors.add(:printer, e)
    return false
  rescue SampleManifest::MultiplexedLibraryBehaviour::Core::MxLibraryTubeException => e
    errors.add(:mx_tube, e)
    return false
  end

  true
end

#find_printerObject


57
58
59
# File 'lib/label_printer/label_printer/print_job.rb', line 57

def find_printer
  BarcodePrinter.find_by(name: printer_name) or raise BarcodePrinter::BarcodePrinterException.new, "Could not find barcode printer #{printer_name.inspect}"
end

#label_template_idObject


51
52
53
54
55
# File 'lib/label_printer/label_printer/print_job.rb', line 51

def label_template_id
  printer = find_printer
  name = printer.barcode_printer_type.label_template_name
  LabelPrinter::PmbClient.get_label_template_by_name(name).fetch('data').first['id']
end

#label_template_id_attributeObject


47
48
49
# File 'lib/label_printer/label_printer/print_job.rb', line 47

def label_template_id_attribute
  { label_template_id: label_template_id }
end

#labels_attributeObject


37
38
39
40
41
# File 'lib/label_printer/label_printer/print_job.rb', line 37

def labels_attribute
  printer = find_printer
  printer_type_class = { printer_type_class: printer.barcode_printer_type.class }
  @labels = label_class.new(options.merge(printer_type_class)).to_h
end

#number_of_labelsObject


65
66
67
# File 'lib/label_printer/label_printer/print_job.rb', line 65

def number_of_labels
  labels[:labels][:body] ? labels[:labels][:body].count : 0
end

#printer_name_attributeObject


43
44
45
# File 'lib/label_printer/label_printer/print_job.rb', line 43

def printer_name_attribute
  { printer_name: printer_name }
end

#successObject


61
62
63
# File 'lib/label_printer/label_printer/print_job.rb', line 61

def success
  "Your #{number_of_labels} label(s) have been sent to printer #{printer_name}"
end