zebra-epl 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/zebra/epl.rb +2 -0
- data/lib/zebra/epl/box.rb +40 -0
- data/lib/zebra/epl/label.rb +9 -1
- data/lib/zebra/epl/printable.rb +5 -1
- data/lib/zebra/epl/version.rb +1 -1
- data/lib/zebra/print_job.rb +8 -1
- data/spec/zebra/epl/box_spec.rb +71 -0
- data/spec/zebra/epl/label_spec.rb +26 -2
- data/spec/zebra/print_job_spec.rb +10 -4
- metadata +7 -4
data/lib/zebra/epl.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
require "cups"
|
2
|
+
require "tempfile"
|
2
3
|
require "zebra/epl/version"
|
3
4
|
require "zebra/epl/rotation"
|
4
5
|
require "zebra/epl/multipliers"
|
5
6
|
require "zebra/epl/print_mode"
|
6
7
|
require "zebra/epl/font"
|
8
|
+
require "zebra/epl/box"
|
7
9
|
require "zebra/epl/label"
|
8
10
|
require "zebra/epl/text"
|
9
11
|
require "zebra/epl/barcode"
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "zebra/epl/printable"
|
2
|
+
|
3
|
+
module Zebra
|
4
|
+
module Epl
|
5
|
+
class Box
|
6
|
+
include Printable
|
7
|
+
|
8
|
+
class InvalidLineThickness < StandardError; end
|
9
|
+
|
10
|
+
attr_reader :line_thickness, :end_position, :end_x, :end_y
|
11
|
+
|
12
|
+
def line_thickness=(thickness)
|
13
|
+
raise InvalidLineThickness unless thickness.nil? || thickness.to_i.to_s == thickness.to_s
|
14
|
+
@line_thickness = thickness
|
15
|
+
end
|
16
|
+
|
17
|
+
def end_position=(coords)
|
18
|
+
@end_position, @end_x, @end_y = coords, coords[0], coords[1]
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_epl
|
22
|
+
check_attributes
|
23
|
+
["X#{x}", y, line_thickness, end_x, end_y].join(",")
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def has_data?
|
29
|
+
false
|
30
|
+
end
|
31
|
+
|
32
|
+
def check_attributes
|
33
|
+
super
|
34
|
+
raise MissingAttributeError.new("the line thickness is not given") unless line_thickness
|
35
|
+
raise MissingAttributeError.new("the horizontal end position (X) is not given") unless end_x
|
36
|
+
raise MissingAttributeError.new("the vertical end position (Y) is not given") unless end_y
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/zebra/epl/label.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
|
2
3
|
module Zebra
|
3
4
|
module Epl
|
4
5
|
class Label
|
@@ -52,12 +53,19 @@ module Zebra
|
|
52
53
|
io << "N\n"
|
53
54
|
|
54
55
|
elements.each do |element|
|
55
|
-
io << element.
|
56
|
+
io << element.to_epl << "\n"
|
56
57
|
end
|
57
58
|
|
58
59
|
io << "P0\n"
|
59
60
|
end
|
60
61
|
|
62
|
+
def persist
|
63
|
+
tempfile = Tempfile.new "zebra_label"
|
64
|
+
dump_contents tempfile
|
65
|
+
tempfile.rewind
|
66
|
+
tempfile
|
67
|
+
end
|
68
|
+
|
61
69
|
private
|
62
70
|
|
63
71
|
def check_required_configurations
|
data/lib/zebra/epl/printable.rb
CHANGED
@@ -29,10 +29,14 @@ module Zebra
|
|
29
29
|
|
30
30
|
private
|
31
31
|
|
32
|
+
def has_data?
|
33
|
+
true
|
34
|
+
end
|
35
|
+
|
32
36
|
def check_attributes
|
33
37
|
raise MissingAttributeError.new("the X value is not given") unless @x
|
34
38
|
raise MissingAttributeError.new("the Y value is not given") unless @y
|
35
|
-
raise MissingAttributeError.new("the data to be printed is not given") unless @data
|
39
|
+
raise MissingAttributeError.new("the data to be printed is not given") unless @data || !has_data?
|
36
40
|
end
|
37
41
|
end
|
38
42
|
end
|
data/lib/zebra/epl/version.rb
CHANGED
data/lib/zebra/print_job.rb
CHANGED
@@ -15,7 +15,14 @@ module Zebra
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def print(label)
|
18
|
-
|
18
|
+
tempfile = label.persist
|
19
|
+
|
20
|
+
begin
|
21
|
+
`lpr -P #{@printer} -o raw #{tempfile.path}`
|
22
|
+
ensure
|
23
|
+
tempfile.close
|
24
|
+
tempfile.unlink
|
25
|
+
end
|
19
26
|
end
|
20
27
|
|
21
28
|
private
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Zebra::Epl::Box do
|
5
|
+
it "can be initialized with initial position" do
|
6
|
+
box = described_class.new :position => [20, 40]
|
7
|
+
box.position.should == [20, 40]
|
8
|
+
box.x.should == 20
|
9
|
+
box.y.should == 40
|
10
|
+
end
|
11
|
+
|
12
|
+
it "can be initialized with the end position" do
|
13
|
+
box = described_class.new :end_position => [20, 40]
|
14
|
+
box.end_position.should == [20, 40]
|
15
|
+
box.end_x.should == 20
|
16
|
+
box.end_y.should == 40
|
17
|
+
end
|
18
|
+
|
19
|
+
it "can be initialized with the line thckness " do
|
20
|
+
box = described_class.new :line_thickness => 3
|
21
|
+
box.line_thickness.should == 3
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#to_epl" do
|
25
|
+
subject(:box) { described_class.new attributes }
|
26
|
+
let(:attributes) { { :position => [20,40], :end_position => [60, 100], :line_thickness => 3 } }
|
27
|
+
|
28
|
+
it "raises an error if the X position was not informed" do
|
29
|
+
box = described_class.new attributes.merge :position => [nil, 40]
|
30
|
+
expect {
|
31
|
+
box.to_epl
|
32
|
+
}.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the X value is not given")
|
33
|
+
end
|
34
|
+
|
35
|
+
it "raises an error if the Y position was not informed" do
|
36
|
+
box = described_class.new attributes.merge :position => [20, nil]
|
37
|
+
expect {
|
38
|
+
box.to_epl
|
39
|
+
}.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
|
40
|
+
end
|
41
|
+
|
42
|
+
it "raises an error if the end X position was not informed" do
|
43
|
+
box = described_class.new attributes.merge :end_position => [nil, 40]
|
44
|
+
expect {
|
45
|
+
box.to_epl
|
46
|
+
}.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the horizontal end position (X) is not given")
|
47
|
+
end
|
48
|
+
|
49
|
+
it "raises an error if the end Y position was not informed" do
|
50
|
+
box = described_class.new attributes.merge :end_position => [20, nil]
|
51
|
+
expect {
|
52
|
+
box.to_epl
|
53
|
+
}.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the vertical end position (Y) is not given")
|
54
|
+
end
|
55
|
+
|
56
|
+
it "raises an error if the line thickness was not informed" do
|
57
|
+
box = described_class.new attributes.merge :line_thickness => nil
|
58
|
+
expect {
|
59
|
+
box.to_epl
|
60
|
+
}.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the line thickness is not given")
|
61
|
+
end
|
62
|
+
|
63
|
+
it "begins with the 'X' command" do
|
64
|
+
box.to_epl.should =~ /\AX/
|
65
|
+
end
|
66
|
+
|
67
|
+
it "contains the attributes in correct order" do
|
68
|
+
box.to_epl.should == "X20,40,3,60,100"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -56,8 +56,8 @@ describe Zebra::Epl::Label do
|
|
56
56
|
let(:io) { "" }
|
57
57
|
|
58
58
|
it "dumps its contents to the received IO" do
|
59
|
-
label << stub(:
|
60
|
-
label << stub(:
|
59
|
+
label << stub(:to_epl => "foobar")
|
60
|
+
label << stub(:to_epl => "blabla")
|
61
61
|
label.width = 100
|
62
62
|
label.length_and_gap = [200, 24]
|
63
63
|
label.print_speed = 3
|
@@ -88,4 +88,28 @@ describe Zebra::Epl::Label do
|
|
88
88
|
}.to raise_error(Zebra::Epl::Label::PrintSpeedNotInformedError)
|
89
89
|
end
|
90
90
|
end
|
91
|
+
|
92
|
+
describe "#persist" do
|
93
|
+
let(:tempfile) { stub.as_null_object }
|
94
|
+
let(:label) { described_class.new :print_speed => 2 }
|
95
|
+
|
96
|
+
before do
|
97
|
+
Tempfile.stub :new => tempfile
|
98
|
+
label << stub(:to_epl => "foobar")
|
99
|
+
end
|
100
|
+
|
101
|
+
it "creates a tempfile" do
|
102
|
+
Tempfile.should_receive(:new).with("zebra_label").and_return(tempfile)
|
103
|
+
label.persist
|
104
|
+
end
|
105
|
+
|
106
|
+
it "returns the tempfile" do
|
107
|
+
label.persist.should == tempfile
|
108
|
+
end
|
109
|
+
|
110
|
+
it "dumps its contents to the tempfile" do
|
111
|
+
tempfile.should_receive(:rewind)
|
112
|
+
label.persist
|
113
|
+
end
|
114
|
+
end
|
91
115
|
end
|
@@ -16,20 +16,26 @@ describe Zebra::PrintJob do
|
|
16
16
|
end
|
17
17
|
|
18
18
|
describe "#print" do
|
19
|
-
let(:label) { stub :
|
19
|
+
let(:label) { stub :persist => tempfile }
|
20
20
|
let(:cups_job) { stub :print => true }
|
21
|
+
let(:tempfile) { stub(:path => "/foo/bar").as_null_object }
|
21
22
|
|
22
23
|
subject(:print_job) { described_class.new "Zebra" }
|
23
24
|
|
24
|
-
before {
|
25
|
+
before { print_job.stub(:` => true) }
|
25
26
|
|
26
27
|
it "creates a cups print job with the correct arguments" do
|
27
|
-
Cups::PrintJob.should_receive(:new).with("/foo/bar", "Zebra").and_return(cups_job)
|
28
28
|
print_job.print label
|
29
29
|
end
|
30
30
|
|
31
31
|
it "prints the label" do
|
32
|
-
|
32
|
+
print_job.should_receive(:`).with("lpr -P Zebra -o raw /foo/bar")
|
33
|
+
print_job.print label
|
34
|
+
end
|
35
|
+
|
36
|
+
it "unlinks the file" do
|
37
|
+
tempfile.should_receive(:close)
|
38
|
+
tempfile.should_receive(:unlink)
|
33
39
|
print_job.print label
|
34
40
|
end
|
35
41
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zebra-epl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-11-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: cups
|
@@ -124,6 +124,7 @@ files:
|
|
124
124
|
- lib/zebra/epl.rb
|
125
125
|
- lib/zebra/epl/barcode.rb
|
126
126
|
- lib/zebra/epl/barcode_type.rb
|
127
|
+
- lib/zebra/epl/box.rb
|
127
128
|
- lib/zebra/epl/font.rb
|
128
129
|
- lib/zebra/epl/label.rb
|
129
130
|
- lib/zebra/epl/multipliers.rb
|
@@ -135,6 +136,7 @@ files:
|
|
135
136
|
- lib/zebra/print_job.rb
|
136
137
|
- spec/spec_helper.rb
|
137
138
|
- spec/zebra/epl/barcode_spec.rb
|
139
|
+
- spec/zebra/epl/box_spec.rb
|
138
140
|
- spec/zebra/epl/epl_spec.rb
|
139
141
|
- spec/zebra/epl/label_spec.rb
|
140
142
|
- spec/zebra/epl/text_spec.rb
|
@@ -155,7 +157,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
155
157
|
segments:
|
156
158
|
- 0
|
157
159
|
version: '0'
|
158
|
-
hash:
|
160
|
+
hash: 4481700405794095244
|
159
161
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
160
162
|
none: false
|
161
163
|
requirements:
|
@@ -164,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
164
166
|
segments:
|
165
167
|
- 0
|
166
168
|
version: '0'
|
167
|
-
hash:
|
169
|
+
hash: 4481700405794095244
|
168
170
|
requirements: []
|
169
171
|
rubyforge_project:
|
170
172
|
rubygems_version: 1.8.24
|
@@ -175,6 +177,7 @@ summary: Simple DSL to create labels and send them to a Zebra printer using Ruby
|
|
175
177
|
test_files:
|
176
178
|
- spec/spec_helper.rb
|
177
179
|
- spec/zebra/epl/barcode_spec.rb
|
180
|
+
- spec/zebra/epl/box_spec.rb
|
178
181
|
- spec/zebra/epl/epl_spec.rb
|
179
182
|
- spec/zebra/epl/label_spec.rb
|
180
183
|
- spec/zebra/epl/text_spec.rb
|