-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from sanger/GPL-813-4
Gpl 813 4
- Loading branch information
Showing
18 changed files
with
562 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,94 @@ | ||
# frozen_string_literal: true | ||
|
||
# PrintJobWrapper | ||
|
||
# What did I do: | ||
# added validation for required attributes as well as print job | ||
# added validation for print job | ||
# added print job creation in single method | ||
# added print job body creation method | ||
# made copies optional and create them if they are not passed | ||
# created printer and label template as attribute readers | ||
class PrintJobWrapper | ||
include ActiveModel::Model | ||
|
||
attr_reader :printer_name, :label_template_name | ||
attr_accessor :printer_name, :label_template_name, :labels | ||
|
||
validates :label_template, :printer, :labels, presence: true | ||
|
||
validate :check_print_job, :check_label_names | ||
|
||
def print | ||
return false unless valid? | ||
|
||
print_job.execute | ||
end | ||
|
||
def print_job | ||
@print_job ||= case printer.try(:printer_type) | ||
when 'toshiba' | ||
LabelPrinter::PrintJob.build_from_v2(print_job_body.except( | ||
:label_template_name, :copies | ||
)) | ||
when 'squix' | ||
Squix::PrintJob.new(print_job_body.except(:label_template_id)) | ||
end | ||
end | ||
|
||
def print_job_body | ||
@print_job_body ||= { | ||
printer_name: printer_name, | ||
label_template_id: label_template.try(:id), | ||
label_template_name: label_template.try(:name), | ||
labels: labels, | ||
copies: copies | ||
} | ||
end | ||
|
||
def printer | ||
Printer.find_by(name: printer_name) | ||
end | ||
|
||
def label_template | ||
LabelTemplate.find_by(name: label_template_name) | ||
end | ||
|
||
validate :check_attributes | ||
def copies=(copies) | ||
@copies = copies.try(:to_i) | ||
end | ||
|
||
def initialize(params = {}) | ||
@printer_name = params['printer_name'] | ||
@label_template_name = params['label_template_name'] | ||
def copies | ||
@copies ||= 1 | ||
end | ||
|
||
def check_attributes | ||
errors.add(:printer_name, 'does not exist') if printer_name.nil? | ||
errors.add(:label_template_name, 'does not exist') if label_template_name.nil? | ||
private | ||
|
||
# TODO: we may well be double checking everything | ||
def check_print_job | ||
return if print_job.blank? | ||
return if print_job.valid? | ||
|
||
print_job.errors.each do |k, v| | ||
errors.add(k, v) | ||
end | ||
end | ||
|
||
# There is a possibility that some of the label names will not match the | ||
# label names in the label template this would cause an encode error | ||
# which is hard to debug which we found out to our cost | ||
# this will tell you to check! | ||
# TODO: fix rubocop | ||
# rubocop:disable Metrics/AbcSize | ||
def check_label_names | ||
return if labels.nil? || label_template.nil? | ||
|
||
expected_label_names = label_template.labels.pluck(:name) | ||
received_label_names = labels.map { |l| l['label_name'] }.uniq | ||
|
||
# each item in received_label_names has to exist in expected_label_names | ||
return if expected_label_names & received_label_names == received_label_names | ||
|
||
errors.add(:label_name, 'does not match label template label names') | ||
end | ||
# rubocop:enable Metrics/AbcSize | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module Squix | ||
# labels: | ||
# [{ | ||
# "right_text"=>"DN9000003B", | ||
# "left_text"=>"DN9000003B", | ||
# "barcode"=>"DN9000003B", | ||
# "label_name"=>"main_label" | ||
# }, | ||
# { | ||
# "extra_right_text"=>"DN9000003B LTHR-384 RT", | ||
# "extra_left_text"=>"10-NOV-2020" | ||
# "label_name"=>"extra_label" | ||
# }] | ||
|
||
# Squix::PrintJob | ||
class PrintJob | ||
include ActiveModel::Model | ||
|
||
attr_accessor :printer_name, :label_template_name, :labels, :copies | ||
|
||
validates :printer_name, :label_template_name, :labels, :copies, presence: true | ||
|
||
def execute | ||
response = SPrintClient.send_print_request( | ||
printer_name, | ||
label_template_name, | ||
merge_fields_list | ||
) | ||
|
||
# Response contains error message, if required | ||
return false unless response.code == '200' | ||
|
||
true | ||
end | ||
|
||
def merge_fields_list | ||
converted_labels * copies | ||
end | ||
|
||
def converted_labels | ||
return if labels.nil? | ||
|
||
labels.collect { |l| l.except(:label_name) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'sprint_client' | ||
SPrintClient.sprint_uri = Rails.configuration.sprint_uri |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[ | ||
{ | ||
"barcodeFields": [ | ||
{ | ||
"x": 30, | ||
"y": 1, | ||
"cellWidth": 0.2, | ||
"barcodeType": "code128", | ||
"value": "<%= merge_fields[:barcode] %>", | ||
"height": 5 | ||
} | ||
], | ||
"textFields": [ | ||
{ | ||
"x": 3, | ||
"y": 3, | ||
"value": "<%= merge_fields[:location] %>", | ||
"font": "proportional", | ||
"fontSize": 1.7 | ||
}, | ||
{ | ||
"x": 3, | ||
"y": 6, | ||
"value": "<%= merge_fields[:parent_location] %>", | ||
"font": "proportional", | ||
"fontSize": 1.7 | ||
} | ||
] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :squix_print_job, class: Squix::PrintJob do | ||
transient do | ||
label_template { create(:label_template) } | ||
end | ||
|
||
printer_name { create(:printer).name } | ||
label_template_name { label_template.name } | ||
labels { [{ 'label' => { 'test_attr' => 'test', 'barcode' => '12345' } }] } | ||
copies { 1 } | ||
|
||
initialize_with { new(printer_name: printer_name, label_template_name: label_template_name, labels: labels, copies: copies)} | ||
end | ||
end |
Oops, something went wrong.