Skip to content

Commit

Permalink
Merge pull request #71 from jbeast/pmb-client-integration
Browse files Browse the repository at this point in the history
Integrated with PMB Client gem
  • Loading branch information
jbeast committed May 26, 2016
2 parents 583b832 + e98bc21 commit de1067d
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 192 deletions.
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.3'
# Use sqlite3 as the database for Active Record
Expand Down Expand Up @@ -41,7 +40,7 @@ gem 'kaminari'

gem 'backbone-on-rails'

gem 'rest-client'
gem 'pmb-client', '0.1.0', :github => 'sanger/pmb-client'

gem 'exception_notification'

Expand Down
34 changes: 20 additions & 14 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
GIT
remote: git://github.com/sanger/pmb-client.git
revision: 016f151a3bde84448ed64e528259c3511282dcdb
specs:
pmb-client (0.1.0)
json_api_client (~> 1.1)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -78,8 +85,6 @@ GEM
database_cleaner (1.4.1)
debug_inspector (0.0.2)
diff-lcs (1.2.5)
domain_name (0.5.25)
unf (>= 0.0.5, < 1.0.0)
eco (1.0.0)
coffee-script
eco-source
Expand All @@ -96,13 +101,15 @@ GEM
factory_girl_rails (4.5.0)
factory_girl (~> 4.5.0)
railties (>= 3.0.0)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
faraday_middleware (0.10.0)
faraday (>= 0.7.4, < 0.10)
ffi (1.9.8)
font-awesome-rails (4.5.0.0)
railties (>= 3.2, < 5.0)
globalid (0.3.6)
activesupport (>= 4.1.0)
http-cookie (1.0.2)
domain_name (~> 0.5)
i18n (0.7.0)
jbuilder (2.3.1)
activesupport (>= 3.0.0, < 5)
Expand All @@ -119,6 +126,12 @@ GEM
json (1.8.3)
json-schema (2.6.0)
addressable (~> 2.3.8)
json_api_client (1.1.1)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
addressable (~> 2.2)
faraday (~> 0.9)
faraday_middleware (~> 0.9)
kaminari (0.16.3)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
Expand All @@ -132,9 +145,9 @@ GEM
mini_portile (0.6.2)
minitest (5.8.3)
multi_json (1.11.2)
multipart-post (2.0.0)
mysql2 (0.3.20)
net-ldap (0.12.0)
netrc (0.11.0)
nokogiri (1.6.6.3)
mini_portile (~> 0.6.0)
phantomjs (1.9.8.0)
Expand Down Expand Up @@ -183,10 +196,6 @@ GEM
uri_template (~> 0.7)
rdoc (4.2.0)
ref (2.0.0)
rest-client (1.8.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)
rouge (1.10.1)
rspec-core (3.2.3)
rspec-support (~> 3.2.0)
Expand Down Expand Up @@ -250,9 +259,6 @@ GEM
uglifier (2.7.1)
execjs (>= 0.3.0)
json (>= 1.8.0)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.1)
uri_template (0.7.0)
web-console (2.2.1)
activemodel (>= 4.0)
Expand Down Expand Up @@ -290,12 +296,12 @@ DEPENDENCIES
mysql2 (= 0.3.20)
net-ldap
phantomjs
pmb-client (= 0.1.0)!
poltergeist
puma
rails (= 4.2.3)
rake
raml_ruby (~> 0.1.1)
rest-client
rspec-rails (~> 3.1)
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
Expand All @@ -310,4 +316,4 @@ DEPENDENCIES
with_model

BUNDLED WITH
1.11.2
1.12.1
4 changes: 0 additions & 4 deletions app/models/consumable_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ class ConsumableType < ActiveRecord::Base
"LN2": 5
}

def simple_storage_condition
(storage_condition or "").gsub('°', '')
end

def latest_batch
batches.last
end
Expand Down
17 changes: 0 additions & 17 deletions app/serializers/consumable_label_serializer.rb

This file was deleted.

25 changes: 0 additions & 25 deletions app/serializers/print_job_serializer.rb

This file was deleted.

2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@

# config.stub_ldap = true

# Print My Barcode api base
config.x.pmb_api_base = 'http://pmb.dev/api/v1'
end
3 changes: 3 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,7 @@

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

# Print My Barcode api base
config.x.pmb_api_base = 'http://pmb.prod/api/v1'
end
3 changes: 3 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@
exception_recipients: %w(#{Rails.configuration.mailer['smtp']['recipient']})
}

# Print My Barcode api base
config.x.pmb_api_base = 'http://pmb.test/api/v1'

end
1 change: 1 addition & 0 deletions config/initializers/print_my_barcode.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PMB::Base.site = Rails.configuration.x.pmb_api_base
33 changes: 33 additions & 0 deletions lib/labels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Labels

attr_accessor :consumables

def initialize(batch)
@consumables = batch.single_barcode? ? [batch.consumables.first] : batch.consumables
end

def to_h
{
body: body
}
end

private

def body
consumables.map do |consumable|
{
label_1: {
barcode_text: consumable.barcode,
reagent_name: consumable.batch.consumable_type.name,
batch_no: consumable.batch.number,
date: "Use by:#{consumable.batch.expiry_date}",
barcode: consumable.barcode,
volume: consumable.display_volume,
storage_condition: consumable.batch.consumable_type.storage_condition,
}
}
end
end

end
49 changes: 14 additions & 35 deletions lib/print_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class PrintJob
def printer_label_type_matches
printer_model = Printer.find_by(name: printer)
label_type_model = LabelType.find_by(external_id: label_template_id)

if label_type_model.nil? or printer_model.nil?
errors.add(:printer, 'does not exist') if printer_model.nil?
errors.add(:label_template_id, 'does not exist') if label_type_model.nil?
Expand All @@ -22,47 +23,25 @@ def printer_label_type_matches
end
end

def config
Rails.configuration.print_service
end

def execute!
return false unless valid?

begin
if valid?
@response = RestClient.post config["host"], to_json, content_type: "application/vnd.api+json"
response_successful?
else
return false
end
rescue RestClient::Exception => e
if e.http_code == 422
JSON.parse(e.response)['errors'].each do |type, err_array|
err_array.each do |error|
# Remove the type from the start of the error if it's there.
errors.add(type, error.sub(/^#{type} /i, ''))
end
end
end
PMB::PrintJob.execute(printer_name: printer, label_template_id: label_template_id, labels: labels.to_h)
return true
##
# PMB doesn't format errors in the way PMB::Client expects, so somewhere within the depths
# of PMB::Client it throws a NoMethodError
# Also, PMB::Client doesn't give access to the json returned from the service so we can't even
# access what went wrong to populate errors
# This needs to be modified when PMB has fixed its errors object
rescue StandardError => e
return false
end
end

def to_json
{
data: {
attributes: serializer.attributes
}
}.to_json
end

private

def serializer
PrintJobSerializer.new(self)
end

def response_successful?
@response.code == 200
def labels
Labels.new(batch)
end

end
6 changes: 3 additions & 3 deletions spec/features/batches_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
end

it 'prints labels for the batch' do
allow(RestClient).to receive(:post).and_return(OpenStruct.new(:code => 200))
allow(PMB::PrintJob).to receive(:execute).and_return(true)

visit batch_path(@batch)
click_button "Print Labels"
Expand All @@ -77,8 +77,7 @@
end

it 'tells the user if there\'s and error' do
exception = RestClient::Exception.new(OpenStruct.new(code: 500))
allow(RestClient).to receive(:post).and_raise(exception)
allow(PMB::PrintJob).to receive(:execute).and_raise(JsonApiClient::Errors::ServerError.new({}))

visit batch_path(@batch)
click_button "Print Labels"
Expand All @@ -90,6 +89,7 @@
end

it 'tells the user the error if known' do
pending 'Need PMB to be fixed first'
exception = RestClient::Exception.new(OpenStruct.new(code: 422, to_str: '{"errors":{"printer":["Printer does not exist"]}}'))
allow(RestClient).to receive(:post).and_raise(exception)

Expand Down
52 changes: 52 additions & 0 deletions spec/lib/labels_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'rails_helper'

RSpec.describe Labels, type: :model do

before(:each) { @batch = create(:batch_with_consumables) }

it 'should serialize a batch into labels' do
labels = Labels.new(@batch).to_h

expect(labels).to be_kind_of(Hash)

expect(labels[:body]).to be_kind_of(Array)
expect(labels[:body].count).to eq(3)

first_label = labels[:body].first
first_consumable = @batch.consumables.first

expect(first_label[:label_1][:barcode_text]).to eql(first_consumable.barcode)
expect(first_label[:label_1][:reagent_name]).to eql(@batch.consumable_type.name)
expect(first_label[:label_1][:batch_no]).to eql(@batch.number)
expect(first_label[:label_1][:date]).to eql("Use by:#{@batch.expiry_date.to_date}")
expect(first_label[:label_1][:barcode]).to eql(first_consumable.barcode)
expect(first_label[:label_1][:volume]).to eq("1.1mL")
expect(first_label[:label_1][:storage_condition]).to eql('LN2')
end

context 'when all barcodes are identical' do

it 'should only generate a single label' do

batch = create(:batch)
consumable = create(:consumable)
batch.consumables << (1..3).map {|n| consumable.dup}

expect(batch.consumables[0].barcode).to eq(consumable.barcode)
expect(batch.consumables[1].barcode).to eq(consumable.barcode)
expect(batch.consumables[2].barcode).to eq(consumable.barcode)

labels = Labels.new(batch).to_h

expect(labels[:body]).to be_kind_of(Array)
expect(labels[:body].count).to eq(1)

first_label = labels[:body].first
expect(first_label[:label_1][:barcode]).to eql(consumable.barcode)
expect(first_label[:label_1][:barcode_text]).to eql(consumable.barcode)

end

end

end
Loading

0 comments on commit de1067d

Please sign in to comment.