Class: PrintJob

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/print_job.rb

Overview

rubocop:todo Style/Documentation

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#label_templates_by_serviceObject

Returns the value of attribute label_templates_by_service.



7
8
9
# File 'app/models/print_job.rb', line 7

def label_templates_by_service
  @label_templates_by_service
end

#labelsObject

Returns the value of attribute labels.



7
8
9
# File 'app/models/print_job.rb', line 7

def labels
  @labels
end

#labels_sprintObject

Returns the value of attribute labels_sprint.



7
8
9
# File 'app/models/print_job.rb', line 7

def labels_sprint
  @labels_sprint
end

#number_of_copiesObject

Returns the value of attribute number_of_copies.



6
7
8
# File 'app/models/print_job.rb', line 6

def number_of_copies
  @number_of_copies
end

#printerObject

Returns the value of attribute printer.



7
8
9
# File 'app/models/print_job.rb', line 7

def printer
  @printer
end

#printer_nameObject

Returns the value of attribute printer_name.



7
8
9
# File 'app/models/print_job.rb', line 7

def printer_name
  @printer_name
end

Instance Method Details

#executeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/print_job.rb', line 12

def execute
  return false unless valid?

  case printer.print_service
  when 'PMB'
    print_to_pmb
  when 'SPrint'
    print_to_sprint
  else
    errors.add(:base, "Print service #{printer.print_service} not recognised.")
    false
  end
end


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/print_job.rb', line 26

def print_to_pmb
  job =
    PMB::PrintJob.new(
      printer_name: printer_name,
      label_template_id: pmb_label_template_id,
      labels: {
        body: (labels * number_of_copies)
      }
    )
  if job.save
    true
  else
    errors.add(:print_server, job.errors.full_messages.join(' - '))
    false
  end
rescue JsonApiClient::Errors::ConnectionError
  errors.add(:pmb, 'PrintMyBarcode service is down')
  false
end


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/print_job.rb', line 46

def print_to_sprint
  # labels structure is like this:
  # "labels"=>[
  #   {
  #     "main_label"=>{
  #       "top_left"=>"29-OCT-2020",
  #       "bottom_left"=>"DN9000214K",
  #       "top_right"=>"DN9000211H",
  #       "bottom_right"=>"Duplex-Seq LDS AL Lib",
  #       "barcode"=>"DN9000214K"
  #     },
  #     "extra_label"=>{
  #       "top_left"=>"29-OCT-2020",
  #       "bottom_left"=>"DN9000214K",
  #       "top_right"=>"DN9000211H",
  #       "bottom_right"=>"Duplex-Seq LDS AL Lib extra",
  #       "barcode"=>"DN9000214K"
  #     }
  #   },
  #   {
  #     "main_label"=>{
  #       "top_left"=>"29-OCT-2020",
  #       "bottom_left"=>"DN9000214K",
  #       "top_right"=>"DN9000211H",
  #       "bottom_right"=>"Duplex-Seq LDS Lig",
  #       "barcode"=>"DN9000214K-LIG"
  #     }
  #   },
  # ]

  # labels_sprint:
  # {
  #   "sprint"=> {
  #    "right_text"=>"DN9000003B",
  #    "left_text"=>"DN9000003B",
  #    "barcode"=>"DN9000003B",
  #    "extra_right_text"=>"DN9000003B  LTHR-384 RT",
  #    "extra_left_text"=>"10-NOV-2020"
  #   }
  # }

  label_template = get_label_template_by_service('SPrint')

  label_array = labels_sprint.values

  # label_array:
  # [{
  #   "right_text"=>"DN9000003B",
  #   "left_text"=>"DN9000003B",
  #   "barcode"=>"DN9000003B",
  #   "extra_right_text"=>"DN9000003B  LTHR-384 RT",
  #   "extra_left_text"=>"10-NOV-2020"
  # }]

  merge_fields_list = label_array * number_of_copies

  # assumes all labels use the same label template
  SPrintClient.send_print_request(printer_name, label_template, merge_fields_list)

  # TODO: DPL-865 [Limber] Handle sprint client response
  #
  # This print_to_sprint call fails silently if there is an error. Instead
  # of returning success, the result of the send_print_request call should
  # be used to check the response status and response body. Examples:
  #
  # The following response body is returned in a 200 response.
  # {"errors":[{"message":"Variable 'printRequest' has an invalid value: Expected
  #   type 'Int' but was
  #   'Double'.","locations":[{"line":1,"column":16}],"extensions":{"classification":"
  #   ValidationError"}}]}
  #
  # The following response body is returned in a 500 response.
  # {"timestamp": "2023-08-02T14:46:30.160+00:00","status": 500,"error": "Internal
  #   Server Error","path": "/graphql"}
  #
  # When sprint cannot log in to printer, the details are available in response body.
  #
  # A successful response has a job id in response body.
  #
  # Use errors.add to show proper feedback in the view.

  true
end