Uses lpr directly instead of relying on Cups (so we can set the raw option)
Cássio Marques
committed Nov 21, 2013
commit 5b42e5186803eabde1c19fd56b2820481a0c008f
Showing 5
changed files with
52 additions
and 16 deletions
zebra/epl.rb b/lib/zebra/epl.rb
+1
-0
| @@ | @@ -1,4 +1,5 @@ |
| require "cups" | |
| + | require "tempfile" |
| require "zebra/epl/version" | |
| require "zebra/epl/rotation" | |
| require "zebra/epl/multipliers" | |
zebra/epl/label.rb b/lib/zebra/epl/label.rb
+9
-1
| @@ | @@ -1,4 +1,5 @@ |
| # encoding: utf-8 | |
| + | |
| module Zebra | |
| module Epl | |
| class Label | |
| @@ | @@ -52,12 +53,19 @@ module Zebra |
| io << "N\n" | |
| elements.each do |element| | |
| - | io << element.to_s << "\n" |
| + | io << element.to_epl << "\n" |
| end | |
| io << "P0\n" | |
| end | |
| + | def persist |
| + | tempfile = Tempfile.new "zebra_label" |
| + | dump_contents tempfile |
| + | tempfile.rewind |
| + | tempfile |
| + | end |
| + | |
| private | |
| def check_required_configurations | |
zebra/print_job.rb b/lib/zebra/print_job.rb
+8
-2
| @@ | @@ -15,8 +15,14 @@ module Zebra |
| end | |
| def print(label) | |
| - | label.persist! unless label.persisted? |
| - | Cups::PrintJob.new(label.path, @printer).print |
| + | tempfile = label.persist |
| + | |
| + | begin |
| + | `lpr -P #{@printer} -o raw #{tempfile.path}` |
| + | ensure |
| + | tempfile.close |
| + | tempfile.unlink |
| + | end |
| end | |
| private | |
spec/zebra/epl/label_spec.rb
+26
-2
| @@ | @@ -56,8 +56,8 @@ describe Zebra::Epl::Label do |
| let(:io) { "" } | |
| it "dumps its contents to the received IO" do | |
| - | label << stub(:to_s => "foobar") |
| - | label << stub(:to_s => "blabla") |
| + | label << stub(:to_epl => "foobar") |
| + | label << stub(:to_epl => "blabla") |
| label.width = 100 | |
| label.length_and_gap = [200, 24] | |
| label.print_speed = 3 | |
| @@ | @@ -88,4 +88,28 @@ describe Zebra::Epl::Label do |
| }.to raise_error(Zebra::Epl::Label::PrintSpeedNotInformedError) | |
| end | |
| end | |
| + | |
| + | describe "#persist" do |
| + | let(:tempfile) { stub.as_null_object } |
| + | let(:label) { described_class.new :print_speed => 2 } |
| + | |
| + | before do |
| + | Tempfile.stub :new => tempfile |
| + | label << stub(:to_epl => "foobar") |
| + | end |
| + | |
| + | it "creates a tempfile" do |
| + | Tempfile.should_receive(:new).with("zebra_label").and_return(tempfile) |
| + | label.persist |
| + | end |
| + | |
| + | it "returns the tempfile" do |
| + | label.persist.should == tempfile |
| + | end |
| + | |
| + | it "dumps its contents to the tempfile" do |
| + | tempfile.should_receive(:rewind) |
| + | label.persist |
| + | end |
| + | end |
| end | |
spec/zebra/print_job_spec.rb
+8
-11
| @@ | @@ -16,30 +16,27 @@ describe Zebra::PrintJob do |
| end | |
| describe "#print" do | |
| - | let(:label) { stub :path => "/foo/bar", :persisted? => true } |
| + | let(:label) { stub :persist => tempfile } |
| let(:cups_job) { stub :print => true } | |
| + | let(:tempfile) { stub(:path => "/foo/bar").as_null_object } |
| subject(:print_job) { described_class.new "Zebra" } | |
| - | before { Cups::PrintJob.stub(:new).and_return(cups_job) } |
| + | before { print_job.stub(:` => true) } |
| it "creates a cups print job with the correct arguments" do | |
| - | Cups::PrintJob.should_receive(:new).with("/foo/bar", "Zebra").and_return(cups_job) |
| print_job.print label | |
| end | |
| it "prints the label" do | |
| - | cups_job.should_receive(:print) |
| + | print_job.should_receive(:`).with("lpr -P Zebra -o raw /foo/bar") |
| print_job.print label | |
| end | |
| - | context "when the label is not persisted" do |
| - | before { label.stub :persisted? => false } |
| - | |
| - | it "persists the label" do |
| - | label.should_receive(:persist!) |
| - | print_job.print label |
| - | end |
| + | it "unlinks the file" do |
| + | tempfile.should_receive(:close) |
| + | tempfile.should_receive(:unlink) |
| + | print_job.print label |
| end | |
| end | |
| end | |