Skip to content

Commit

Permalink
Merge pull request #68 from sanger/GPL-813-4
Browse files Browse the repository at this point in the history
Gpl 813 4
  • Loading branch information
stevieing authored Feb 4, 2021
2 parents c28e32f + 2f5923d commit 1286f62
Show file tree
Hide file tree
Showing 18 changed files with 562 additions and 48 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ gem 'rails-perftest'

gem 'rack-cors', require: 'rack/cors'

gem 'sprint_client'

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
group :development do
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ GEM
ruby_dep (1.5.0)
spring (2.0.2)
activesupport (>= 4.2)
sprint_client (0.0.10)
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
Expand Down Expand Up @@ -227,6 +228,7 @@ DEPENDENCIES
rubocop
ruby-prof (~> 0.15.9)
spring
sprint_client
sqlite3

BUNDLED WITH
Expand Down
28 changes: 26 additions & 2 deletions app/controllers/v2/print_jobs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module V2
class PrintJobsController < ApplicationController
def create
print_job_wrapper = PrintJobWrapper.new(print_job_params)
if print_job_wrapper.valid?

if print_job_wrapper.print
render json: { message: 'labels successfully printed' }
else
render_error print_job_wrapper
Expand All @@ -14,8 +15,31 @@ def create

private

# TODO: get this working in one go.
# def print_job_params
# params.require(:print_job).require(:printer_name, :label_template_name, :copies)
# .tap do |allow_listed|
# allow_listed[:labels] = []
# params[:print_job][:labels].each do |label|
# allow_listed[:labels] << label.permit!.to_h
# end
# end
# end

def print_job_params
params.require(:print_job).permit(:printer_name, :label_template_name)
p1 = params.permit(
print_job: %i[
printer_name
label_template_name
copies
]
)[:print_job].to_h

p1.merge(labels: labels_params)
end

def labels_params
params.require(:print_job)[:labels].map { |label| label.permit!.to_h }
end
end
end
45 changes: 45 additions & 0 deletions app/label_printer/label_printer/print_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,50 @@ def self.build(attributes)
LabelPrinter::PrintJob::Base.new(attributes)
end
end

# If the data is coming from v2 then we need to convert the labels
# into a compatible format
def self.build_from_v2(attributes)
build(attributes.merge(labels: convert_labels(attributes[:labels])))
end

# Example labels from v2:
# 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"
# }]
# and v1 equivalent
# {
# "body" => [{
# "main_label" => {
# "right_text"=>"DN9000003B",
# "left_text"=>"DN9000003B",
# "barcode"=>"DN9000003B",
#
# }},
# { "extra_label" => {
# "extra_right_text"=>"DN9000003B LTHR-384 RT",
# "extra_left_text"=>"10-NOV-2020"
# }}]
# }
def self.convert_labels(labels)
return if labels.nil?

labels_with_location = labels.map do |label|
label_name = label[:label_name]

# we need to remove the label_name otherwise it will cause a failure
{ label_name => label.except(:label_name) }
end
{ body: labels_with_location }
end
end
end
90 changes: 82 additions & 8 deletions app/models/print_job_wrapper.rb
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
48 changes: 48 additions & 0 deletions app/models/squix/print_job.rb
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
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@
resource '*', headers: :any, methods: [:get, :post, :options]
end
end

config.sprint_uri = 'http://sprint.psd.sanger.ac.uk/graphql'
end
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@
sender_address: %("Projects Exception Notifier" <#{Rails.configuration.mailer['smtp']['sender']}>),
exception_recipients: %w(#{Rails.configuration.mailer['smtp']['recipient']})
}

config.sprint_uri = 'http://example_sprint.com/graphql'
end
4 changes: 4 additions & 0 deletions config/initializers/sprint_server_setup.rb
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
30 changes: 30 additions & 0 deletions config/sprint/label_templates/labwhere_1d
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
}
]
}
]
8 changes: 8 additions & 0 deletions spec/factories/label_templates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@
FactoryBot.create(:label_with_drawings, name: 'footer')].flatten
end
end

factory :label_template_simple, class: LabelTemplate do
sequence(:name) { |n| "label_template_#{n}" }
label_type
labels do
[FactoryBot.create(:label_with_drawings)]
end
end
end
7 changes: 7 additions & 0 deletions spec/factories/printers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@
factory :printer do
sequence(:name) { |n| "Printer #{n}" }
printer_type { :toshiba }

# maybe not necessary but more descriptive
factory :toshiba_printer

factory :squix_printer do
printer_type { :squix }
end
end
end
16 changes: 16 additions & 0 deletions spec/factories/squix/print_jobs.rb
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
Loading

0 comments on commit 1286f62

Please sign in to comment.