zebra-zpl 1.0.5 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +62 -0
  3. data/CONTRIBUTING.md +49 -0
  4. data/Gemfile +6 -0
  5. data/README.md +259 -76
  6. data/docs/example.rb +249 -0
  7. data/docs/images/barcode.png +0 -0
  8. data/docs/images/datamatrix.png +0 -0
  9. data/docs/images/earth.jpg +0 -0
  10. data/docs/images/graphics.png +0 -0
  11. data/docs/images/image.png +0 -0
  12. data/docs/images/image_inverted.png +0 -0
  13. data/docs/images/images.png +0 -0
  14. data/docs/images/justification.png +0 -0
  15. data/docs/images/qrcode.png +0 -0
  16. data/docs/images/rotation.png +0 -0
  17. data/docs/images/text.png +0 -0
  18. data/lib/zebra/print_job.rb +7 -13
  19. data/lib/zebra/zpl.rb +20 -13
  20. data/lib/zebra/zpl/barcode.rb +29 -12
  21. data/lib/zebra/zpl/barcode_type.rb +4 -1
  22. data/lib/zebra/zpl/box.rb +16 -4
  23. data/lib/zebra/zpl/comment.rb +15 -0
  24. data/lib/zebra/zpl/datamatrix.rb +76 -0
  25. data/lib/zebra/zpl/graphic.rb +91 -0
  26. data/lib/zebra/zpl/image.rb +91 -0
  27. data/lib/zebra/zpl/label.rb +2 -13
  28. data/lib/zebra/zpl/pdf417.rb +50 -0
  29. data/lib/zebra/zpl/qrcode.rb +2 -2
  30. data/lib/zebra/zpl/text.rb +44 -20
  31. data/lib/zebra/zpl/version.rb +1 -1
  32. data/spec/fixtures/default.jpg +0 -0
  33. data/spec/spec_helper.rb +6 -2
  34. data/spec/zebra/print_job_spec.rb +13 -18
  35. data/spec/zebra/zpl/barcode_spec.rb +70 -63
  36. data/spec/zebra/zpl/box_spec.rb +27 -31
  37. data/spec/zebra/zpl/character_set_spec.rb +7 -7
  38. data/spec/zebra/zpl/comment_spec.rb +18 -0
  39. data/spec/zebra/zpl/datamatrix_spec.rb +124 -0
  40. data/spec/zebra/zpl/graphics_spec.rb +227 -0
  41. data/spec/zebra/zpl/image_spec.rb +113 -0
  42. data/spec/zebra/zpl/label_spec.rb +40 -52
  43. data/spec/zebra/zpl/pdf417_spec.rb +108 -0
  44. data/spec/zebra/zpl/qrcode_spec.rb +92 -92
  45. data/spec/zebra/zpl/text_spec.rb +57 -55
  46. data/zebra-zpl.gemspec +14 -15
  47. metadata +71 -22
@@ -3,61 +3,49 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Zebra::Zpl::Label do
6
- subject(:label) { described_class.new :print_speed => 2 }
6
+ subject(:label) { described_class.new print_speed: 2 }
7
7
 
8
8
  describe "#new" do
9
9
  it "sets the label width" do
10
- label = described_class.new :width => 300
11
- label.width.should == 300
10
+ label = described_class.new width: 300
11
+ expect(label.width).to eq 300
12
12
  end
13
13
 
14
- it "sets the label length/gap" do
15
- label = described_class.new :length_and_gap => [400, 24]
16
- label.length.should == 400
17
- label.gap.should == 24
14
+ it "sets the label length" do
15
+ label = described_class.new length: 400
16
+ expect(label.length).to eq 400
18
17
  end
19
18
 
20
19
  it "sets the printing speed" do
21
- label = described_class.new :print_speed => 2
22
- label.print_speed.should == 2
20
+ label = described_class.new print_speed: 2
21
+ expect(label.print_speed).to eq 2
23
22
  end
24
23
 
25
24
  it "sets the number of copies" do
26
- label = described_class.new :copies => 4
27
- label.copies.should == 4
25
+ label = described_class.new copies: 4
26
+ expect(label.copies).to eq 4
28
27
  end
29
28
 
30
29
  it "the number of copies defaults to 1" do
31
30
  label = described_class.new
32
- label.copies.should == 1
31
+ expect(label.copies).to eq 1
33
32
  end
34
33
 
35
34
  it "validates the printing speed" do
36
35
  [-1, 8, "a"].each do |s|
37
36
  expect {
38
- described_class.new :print_speed => s
37
+ described_class.new print_speed: s
39
38
  }.to raise_error(Zebra::Zpl::Label::InvalidPrintSpeedError)
40
39
  end
41
40
  end
42
-
43
- it "sets the print density" do
44
- label = described_class.new :print_density => 10
45
- label.print_density.should == 10
46
- end
47
-
48
- it "validates the print density" do
49
- [-1, 16, "a"].each do |d|
50
- expect {
51
- described_class.new :print_density => d
52
- }.to raise_error(Zebra::Zpl::Label::InvalidPrintDensityError)
53
- end
54
- end
55
41
  end
56
42
 
57
43
  describe "#<<" do
58
44
  it "adds an item to the list of label elements" do
59
45
  expect {
60
- label << stub
46
+ obj = {}
47
+ allow(obj).to receive(:to_zpl) { 'foobar' }
48
+ label << obj
61
49
  }.to change { label.elements.count }.by 1
62
50
  end
63
51
  end
@@ -66,29 +54,26 @@ describe Zebra::Zpl::Label do
66
54
  let(:io) { "" }
67
55
 
68
56
  it "dumps its contents to the received IO" do
69
- label << stub(:to_zpl => "foobar")
70
- label << stub(:to_zpl => "blabla")
57
+ obj1, obj2 = {}, {}
58
+ allow(obj1).to receive(:to_zpl) { 'foobar' }
59
+ allow(obj2).to receive(:to_zpl) { 'blabla' }
60
+ label << obj1
61
+ label << obj2
71
62
  label.width = 100
72
- label.length_and_gap = [200, 24]
63
+ label.length = 200
73
64
  label.print_speed = 3
74
- label.print_density = 10
75
65
  label.dump_contents(io)
76
- io.should == "O\nQ200,24\nq100\nS3\nD10\n\nN\nfoobar\nblabla\nP1\n"
66
+ expect(io).to eq '^XA^LL200^LH0,0^LS10^PW100^PR3foobarblabla^PQ1^XZ'
77
67
  end
78
68
 
79
69
  it "does not try to set the label width when it's not informed (falls back to autosense)" do
80
70
  label.dump_contents(io)
81
- io.should_not =~ /q/
71
+ expect(io).to_not match /^LL/
82
72
  end
83
73
 
84
- it "does not try to set the length/gap when they were not informed (falls back to autosense)" do
74
+ it "does not try to set the length when it is not informed (falls back to autosense)" do
85
75
  label.dump_contents(io)
86
- io.should_not =~ /Q/
87
- end
88
-
89
- it "does not try to set the print density when it's not informed (falls back to the default value)" do
90
- label.dump_contents(io)
91
- io.should_not =~ /D/
76
+ expect(io).to_not match /^PW/
92
77
  end
93
78
 
94
79
  it "raises an error if the print speed was not informed" do
@@ -100,39 +85,42 @@ describe Zebra::Zpl::Label do
100
85
  end
101
86
 
102
87
  describe "#persist" do
103
- let(:tempfile) { stub.as_null_object }
104
- let(:label) { described_class.new :print_speed => 2 }
88
+
89
+ let(:tempfile) { StringIO.new }
90
+ let(:label) { described_class.new print_speed: 2 }
105
91
 
106
92
  before do
107
- Tempfile.stub :new => tempfile
108
- label << stub(:to_zpl => "foobar")
93
+ allow(Tempfile).to receive(:new) { tempfile }
94
+ obj = {}
95
+ allow(obj).to receive(:to_zpl) { 'foobar' }
96
+ label << obj
109
97
  end
110
98
 
111
99
  it "creates a tempfile" do
112
- Tempfile.should_receive(:new).with("zebra_label").and_return(tempfile)
100
+ expect(Tempfile).to receive(:new).with('zebra_label').and_return(tempfile)
113
101
  label.persist
114
102
  end
115
103
 
116
104
  it "returns the tempfile" do
117
- label.persist.should == tempfile
105
+ expect(label.persist).to eq tempfile
118
106
  end
119
107
 
120
108
  it "sets the `tempfile` attribute" do
121
109
  label.persist
122
- label.tempfile.should == tempfile
110
+ expect(label.tempfile).to eq tempfile
123
111
  end
124
112
  end
125
113
 
126
114
  describe "#persisted?" do
127
115
  it "returns false if the `tempfile` attribute is nil" do
128
- label = described_class.new :print_speed => 2
129
- label.should_not be_persisted
116
+ label = described_class.new print_speed: 2
117
+ expect(label).to_not be_persisted
130
118
  end
131
119
 
132
120
  it "returns true if the `tempfile` attribute is not nil" do
133
- label = described_class.new :print_speed => 2
134
- label.instance_variable_set(:@tempfile, stub)
135
- label.should be_persisted
121
+ label = described_class.new print_speed: 2
122
+ label.instance_variable_set(:@tempfile, Tempfile.new('zebra_label'))
123
+ expect(label).to be_persisted
136
124
  end
137
125
  end
138
126
  end
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+
3
+ describe Zebra::Zpl::PDF417 do
4
+ it "can be initialized with row height" do
5
+ pdf417 = described_class.new :row_height => 3
6
+ expect(pdf417.row_height).to eq 3
7
+ end
8
+
9
+ it "can be initialized with the security level" do
10
+ pdf417 = described_class.new :security_level => 1
11
+ expect(pdf417.security_level).to eq 1
12
+ end
13
+
14
+ it "can be initialized with truncate" do
15
+ pdf417 = described_class.new :truncate => "Y"
16
+ expect(pdf417.truncate).to eq "Y"
17
+ end
18
+
19
+ it "can be initialized with column number" do
20
+ pdf417 = described_class.new :column_number => 5
21
+ expect(pdf417.column_number).to eq 5
22
+ end
23
+
24
+ it "can be initialized with row number" do
25
+ pdf417 = described_class.new :row_number => 10
26
+ expect(pdf417.row_number).to eq 10
27
+ end
28
+
29
+ describe "#column_number" do
30
+ it "raises an error if the column number is not within the range 1-30" do
31
+ expect{described_class.new :column_number=>50}.to raise_error(Zebra::Zpl::PDF417::InvalidRowColumnNumberError)
32
+ end
33
+ end
34
+
35
+ describe "#row_number" do
36
+ it "raises an error if the row number is not within the range 3-90" do
37
+ expect{described_class.new :row_number=>95}.to raise_error(Zebra::Zpl::PDF417::InvalidRowColumnNumberError)
38
+ end
39
+ end
40
+
41
+ describe "#to_zpl" do
42
+ let(:valid_attributes) { {
43
+ :data => ("Do away with it!"),
44
+ :position => [50, 50],
45
+ :row_height => 5,
46
+ :row_number => 30,
47
+ :column_number => 15,
48
+ :rotation => "N",
49
+ :security_level => 5,
50
+ :truncate => "N"
51
+ }}
52
+ let(:pdf417) { described_class.new valid_attributes }
53
+ let(:tokens) { pdf417.to_zpl.split(",") }
54
+
55
+ it "raises an error if the X position is not given" do
56
+ pdf417 = described_class.new :position => [nil, 50], :data => "foobar"
57
+ expect {
58
+ pdf417.to_zpl
59
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the X value is not given")
60
+ end
61
+
62
+ it "raises an error if the Y position is not given" do
63
+ pdf417 = described_class.new :position => [50, nil], :data => "foobar"
64
+ expect {
65
+ pdf417.to_zpl
66
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
67
+ end
68
+
69
+ it "raises an error if the data to be printed was not informed" do
70
+ pdf417.data = nil
71
+ expect {
72
+ pdf417.to_zpl
73
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the data to be printed is not given")
74
+ end
75
+
76
+ it "contains the X position" do
77
+ expect(tokens[0].match(/FO(\d+)/)[1]).to eq "50"
78
+ end
79
+
80
+ it "contains the Y position" do
81
+ expect(tokens[1].match(/(\d+)\^/)[1]).to eq "50"
82
+ end
83
+
84
+ it "contains the row height" do
85
+ expect(tokens[4]).to eq "5"
86
+ end
87
+
88
+ it "contains the security level" do
89
+ expect(tokens[5]).to eq "5"
90
+ end
91
+
92
+ it "contains the column number" do
93
+ expect(tokens[6]).to eq "15"
94
+ end
95
+
96
+ it "contains the row number" do
97
+ expect(tokens[7]).to eq "30"
98
+ end
99
+
100
+ it "contains the trucate option" do
101
+ expect(tokens[8].match(/(\w) \^/)[1]).to eq "N"
102
+ end
103
+
104
+ it "contains the data to be printed in the pdf417 barcode" do
105
+ expect(tokens[8].include?("Do away with it!")).to eq true
106
+ end
107
+ end
108
+ end
@@ -1,101 +1,101 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Zebra::Zpl::Qrcode do
4
- it "can be initialized with the scale factor" do
5
- qrcode = described_class.new :scale_factor => 3
6
- expect(qrcode.scale_factor).to eq 3
7
- end
8
-
9
- it "can be initialized with the error correction level" do
10
- qrcode = described_class.new :correction_level => "M"
11
- expect(qrcode.correction_level).to eq "M"
4
+ it "can be initialized with the scale factor" do
5
+ qrcode = described_class.new scale_factor: 3
6
+ expect(qrcode.scale_factor).to eq 3
7
+ end
8
+
9
+ it "can be initialized with the error correction level" do
10
+ qrcode = described_class.new correction_level: "M"
11
+ expect(qrcode.correction_level).to eq "M"
12
+ end
13
+
14
+ describe "#scale_factor" do
15
+ it "raises an error if the scale factor is not within the range 1-99" do
16
+ expect { described_class.new scale_factor: 100 }.to raise_error(Zebra::Zpl::Qrcode::InvalidScaleFactorError)
17
+ end
18
+ end
19
+
20
+ describe "#correction_level" do
21
+ it "raises an error if the error correction_level not in [LMQH]" do
22
+ expect { described_class.new correction_level: "A" }.to raise_error(Zebra::Zpl::Qrcode::InvalidCorrectionLevelError)
23
+ end
24
+ end
25
+
26
+ describe "#to_zpl" do
27
+ let(:valid_attributes) { {
28
+ position: [50, 50],
29
+ scale_factor: 3,
30
+ correction_level: "M",
31
+ data: "foobar"
32
+ }}
33
+ let(:qrcode) { described_class.new valid_attributes }
34
+ let(:tokens) { qrcode.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } }
35
+
36
+ it "raises an error if the X position is not given" do
37
+ qrcode = described_class.new position: [nil, 50], data: "foobar"
38
+ expect {
39
+ qrcode.to_zpl
40
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the X value is not given")
41
+ end
42
+
43
+ it "raises an error if the Y position is not given" do
44
+ qrcode = described_class.new position: [50, nil], data: "foobar"
45
+ expect {
46
+ qrcode.to_zpl
47
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
12
48
  end
13
49
 
14
- describe "#scale_factor" do
15
- it "raises an error if the scale factor is not within the range 1-99" do
16
- expect{described_class.new :scale_factor=>100}.to raise_error(Zebra::Zpl::Qrcode::InvalidScaleFactorError)
17
- end
50
+ it "raises an error if the data to be printed was not informed" do
51
+ qrcode.data = nil
52
+ expect {
53
+ qrcode.to_zpl
54
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the data to be printed is not given")
18
55
  end
19
56
 
20
- describe "#correction_level" do
21
- it "raises an error if the error correction_level not in [LMQH]" do
22
- expect{described_class.new :correction_level=>"A"}.to raise_error(Zebra::Zpl::Qrcode::InvalidCorrectionLevelError)
23
- end
24
- end
25
-
26
- describe "#to_zpl" do
27
- let(:valid_attributes) { {
28
- :position => [50, 50],
29
- :scale_factor => 3,
30
- :correction_level => "M",
31
- :data => "foobar"
32
- }}
33
- let(:qrcode) { described_class.new valid_attributes }
34
- let(:tokens) { qrcode.to_zpl.split(",") }
35
-
36
- it "raises an error if the X position is not given" do
37
- qrcode = described_class.new :position => [nil, 50], :data => "foobar"
38
- expect {
39
- qrcode.to_zpl
40
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the X value is not given")
41
- end
42
-
43
- it "raises an error if the Y position is not given" do
44
- qrcode = described_class.new :position => [50, nil], :data => "foobar"
45
- expect {
46
- qrcode.to_zpl
47
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
48
- end
49
-
50
- it "raises an error if the data to be printed was not informed" do
51
- qrcode.data = nil
52
- expect {
53
- qrcode.to_zpl
54
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the data to be printed is not given")
55
- end
56
-
57
- it "raises an error if the scale factor is not given" do
58
- valid_attributes.delete :scale_factor
59
-
60
- expect {
61
- qrcode.to_zpl
62
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the scale factor to be used is not given")
63
- end
64
-
65
- it "raises an error if the correction level is not given" do
66
- valid_attributes.delete :correction_level
67
-
68
- expect {
69
- qrcode.to_zpl
70
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the error correction level to be used is not given")
71
- end
72
-
73
- it "begins with the command 'b'" do
74
- qrcode.to_zpl.should =~ /\Ab/
75
- end
76
-
77
- it "contains the X position" do
78
- tokens[0].match(/b(\d+)/)[1].should eq "50"
79
- end
80
-
81
- it "contains the Y position" do
82
- tokens[1].should eq "50"
83
- end
84
-
85
- it "contains QR code type" do
86
- tokens[2].should eq "Q"
87
- end
88
-
89
- it "contains the scale factor" do
90
- tokens[3].should eq "s3"
91
- end
92
-
93
- it "contains the error correction level" do
94
- tokens[4].should eq "eM"
95
- end
96
-
97
- it "contains the data to be printed in the qrcode" do
98
- tokens[5].should eq "\"foobar\""
99
- end
57
+ it "raises an error if the scale factor is not given" do
58
+ valid_attributes.delete :scale_factor
59
+
60
+ expect {
61
+ qrcode.to_zpl
62
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the scale factor to be used is not given")
63
+ end
64
+
65
+ it "raises an error if the correction level is not given" do
66
+ valid_attributes.delete :correction_level
67
+
68
+ expect {
69
+ qrcode.to_zpl
70
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the error correction level to be used is not given")
71
+ end
72
+
73
+ it "contains the barcode command '^B'" do
74
+ expect(qrcode.to_zpl).to match /\^B/
75
+ end
76
+
77
+ it "contains the X position" do
78
+ expect(tokens[2]).to eq "50"
79
+ end
80
+
81
+ it "contains the Y position" do
82
+ expect(tokens[3]).to eq "50"
83
+ end
84
+
85
+ it "contains QR code type" do
86
+ expect(tokens[6]).to eq "^BQN"
87
+ end
88
+
89
+ it "contains the scale factor" do
90
+ expect(tokens[8]).to eq "3"
91
+ end
92
+
93
+ it "contains the error correction level" do
94
+ expect(tokens[9]).to eq "3"
95
+ end
96
+
97
+ it "contains the data to be printed in the qrcode" do
98
+ expect(tokens[11]).to eq "foobar"
99
+ end
100
100
  end
101
101
  end