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
@@ -1,5 +1,5 @@
1
1
  module Zebra
2
2
  module Zpl
3
- VERSION = "1.0.5"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
Binary file
data/spec/spec_helper.rb CHANGED
@@ -6,9 +6,13 @@
6
6
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
7
 
8
8
  require "zebra/zpl"
9
+ require 'byebug'
10
+ require 'pry'
11
+ require 'pry-nav'
12
+
13
+ ENV['RUBY_ENV'] = 'test'
9
14
 
10
15
  RSpec.configure do |config|
11
- config.treat_symbols_as_metadata_keys_with_true_values = true
12
16
  config.run_all_when_everything_filtered = true
13
17
  config.filter_run :focus
14
18
 
@@ -16,5 +20,5 @@ RSpec.configure do |config|
16
20
  # order dependency and want to debug it, you can fix the order by providing
17
21
  # the seed, which is printed after each run.
18
22
  # --seed 1234
19
- config.order = 'random'
23
+ # config.order = 'random'
20
24
  end
@@ -1,36 +1,31 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Zebra::PrintJob do
4
- before do
5
- Cups.stub(:show_destinations).and_return(["Zebra", "Foobar"])
6
- end
7
-
8
4
  it "receives the name of a printer" do
9
- described_class.new("Zebra").printer.should == "Zebra"
10
- end
11
-
12
- it "raises an error if the printer does not exists" do
13
- expect {
14
- described_class.new("Wrong")
15
- }.to raise_error(Zebra::PrintJob::UnknownPrinter)
5
+ expect(described_class.new("Zebra").printer).to eq 'Zebra'
16
6
  end
17
7
 
18
8
  describe "#print" do
19
- let(:label) { stub :persist => tempfile }
20
- let(:cups_job) { stub :print => true }
21
- let(:tempfile) { stub(:path => "/foo/bar").as_null_object }
9
+ let(:label) { Zebra::Zpl::Label.new(print_speed: 2) }
10
+ let(:zpl_string) { '^XA^FO50,50^FDHello, Zebra^FS^XZ' }
11
+ let(:ip) { '127.0.0.1' }
22
12
 
23
13
  subject(:print_job) { described_class.new "Zebra" }
24
14
 
25
- before { print_job.stub(:` => true) }
15
+ before { allow(print_job).to receive(:`) { true } }
26
16
 
27
17
  it "creates a cups print job with the correct arguments" do
28
- print_job.print label
18
+ print_job.print label, ip
29
19
  end
30
20
 
31
21
  it "prints the label" do
32
- print_job.should_receive(:`).with("lpr -P Zebra -o raw /foo/bar")
33
- print_job.print label
22
+ expect(print_job).to receive(:system).with(/r?lpr? -(h|H) 127.0.0.1 -(d|P) Zebra.*/).at_least(:once)
23
+ print_job.print label, ip
24
+ end
25
+
26
+ it "can print from a ZPL string" do
27
+ expect(print_job).to receive(:system).with(/r?lpr? -(h|H) 127.0.0.1 -(d|P) Zebra.*/).at_least(:once)
28
+ print_job.print zpl_string, ip
34
29
  end
35
30
  end
36
31
  end
@@ -2,48 +2,58 @@ require 'spec_helper'
2
2
 
3
3
  describe Zebra::Zpl::Barcode do
4
4
  it "can be initialized with the position of the text to be printed" do
5
- barcode = described_class.new :position => [20, 40]
6
- barcode.position.should == [20,40]
7
- barcode.x.should == 20
8
- barcode.y.should == 40
5
+ barcode = described_class.new position: [20, 40]
6
+ expect(barcode.position).to eq [20,40]
7
+ expect(barcode.x).to eq 20
8
+ expect(barcode.y).to eq 40
9
9
  end
10
10
 
11
11
  it "can be initialized with the barcode rotation" do
12
12
  rotation = Zebra::Zpl::Rotation::DEGREES_90
13
- barcode = described_class.new :rotation => rotation
14
- barcode.rotation.should == rotation
13
+ barcode = described_class.new rotation: rotation
14
+ expect(barcode.rotation).to eq rotation
15
15
  end
16
16
 
17
17
  it "can be initialized with the barcode rotation" do
18
18
  rotation = Zebra::Zpl::Rotation::DEGREES_90
19
- barcode = described_class.new :rotation => rotation
20
- barcode.rotation.should == rotation
19
+ barcode = described_class.new rotation: rotation
20
+ expect(barcode.rotation).to eq rotation
21
21
  end
22
22
 
23
23
  it "can be initialized with the barcode type" do
24
- type = Zebra::Zpl::BarcodeType::CODE_128_C
25
- barcode = described_class.new :type => type
26
- barcode.type.should == type
24
+ type = Zebra::Zpl::BarcodeType::CODE_128_AUTO
25
+ barcode = described_class.new type: type
26
+ expect(barcode.type).to eq type
27
27
  end
28
28
 
29
- it "can be initialized with the narrow bar width" do
30
- barcode = described_class.new :narrow_bar_width => 3
31
- barcode.narrow_bar_width.should == 3
29
+ it "can be initialized with the barcode width" do
30
+ barcode = described_class.new width: 5
31
+ expect(barcode.width).to eq 5
32
32
  end
33
33
 
34
- it "can be initialized with the wide bar width" do
35
- barcode = described_class.new :wide_bar_width => 10
36
- barcode.wide_bar_width.should == 10
34
+ it "can be initialized with the barcode height" do
35
+ barcode = described_class.new height: 20
36
+ expect(barcode.height).to eq 20
37
37
  end
38
38
 
39
- it "can be initialized with the barcode height" do
40
- barcode = described_class.new :height => 20
41
- barcode.height.should == 20
39
+ it "can be initialized with the ratio" do
40
+ barcode = described_class.new ratio: 3.0
41
+ expect(barcode.ratio).to eq 3.0
42
+ end
43
+
44
+ it "can be initialized with the narrow bar width" do
45
+ barcode = described_class.new narrow_bar_width: 3
46
+ expect(barcode.narrow_bar_width).to eq 3
47
+ end
48
+
49
+ it "can be initialized with the wide bar width" do
50
+ barcode = described_class.new wide_bar_width: 10
51
+ expect(barcode.wide_bar_width).to eq 10
42
52
  end
43
53
 
44
54
  it "can be initialized informing if the human readable code should be printed" do
45
- barcode = described_class.new :print_human_readable_code => true
46
- barcode.print_human_readable_code.should == true
55
+ barcode = described_class.new print_human_readable_code: true
56
+ expect(barcode.print_human_readable_code).to eq true
47
57
  end
48
58
 
49
59
  describe "#rotation=" do
@@ -65,7 +75,7 @@ describe Zebra::Zpl::Barcode do
65
75
  describe "#narrow_bar_width=" do
66
76
  it "raises an error if the type is Code 128 and the width is invalid" do
67
77
  expect {
68
- described_class.new :type => Zebra::Zpl::BarcodeType::CODE_128_AUTO, :narrow_bar_width => 20
78
+ described_class.new type: Zebra::Zpl::BarcodeType::CODE_128_AUTO, narrow_bar_width: 20
69
79
  }.to raise_error(Zebra::Zpl::Barcode::InvalidNarrowBarWidthError)
70
80
  end
71
81
  end
@@ -73,45 +83,46 @@ describe Zebra::Zpl::Barcode do
73
83
  describe "#wide_bar_width=" do
74
84
  it "raises an error if the type is Code 128 and the width is invalid" do
75
85
  expect {
76
- described_class.new :type => Zebra::Zpl::BarcodeType::CODE_128_AUTO, :wide_bar_width => 40
86
+ described_class.new type: Zebra::Zpl::BarcodeType::CODE_128_AUTO, wide_bar_width: 40
77
87
  }.to raise_error(Zebra::Zpl::Barcode::InvalidWideBarWidthError)
78
88
  end
79
89
  end
80
90
 
81
91
  describe "#print_human_readable_code" do
82
92
  it "defaults to false" do
83
- described_class.new.print_human_readable_code.should == false
93
+ expect(described_class.new.print_human_readable_code).to eq false
84
94
  end
85
95
  end
86
96
 
87
97
  describe "#to_zpl" do
88
98
  let(:valid_attributes) { {
89
- :position => [100, 150],
90
- :type => Zebra::Zpl::BarcodeType::CODE_128_AUTO,
91
- :height => 20,
92
- :narrow_bar_width => 4,
93
- :wide_bar_width => 6,
94
- :data => "foobar"
99
+ position: [100, 150],
100
+ type: Zebra::Zpl::BarcodeType::CODE_128_AUTO,
101
+ height: 20,
102
+ width: 5,
103
+ narrow_bar_width: 3,
104
+ wide_bar_width: 6,
105
+ data: "foobar"
95
106
  } }
96
107
  let(:barcode) { described_class.new valid_attributes }
97
- let(:tokens) { barcode.to_zpl.split(",") }
108
+ let(:tokens) { barcode.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } }
98
109
 
99
110
  it "raises an error if the X position was not informed" do
100
- barcode = described_class.new :position => [nil, 100], :data => "foobar"
111
+ barcode = described_class.new position: [nil, 100], data: "foobar"
101
112
  expect {
102
113
  barcode.to_zpl
103
114
  }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the X value is not given")
104
115
  end
105
116
 
106
117
  it "raises an error if the Y position was not informed" do
107
- barcode = described_class.new :position => [100, nil]
118
+ barcode = described_class.new position: [100, nil]
108
119
  expect {
109
120
  barcode.to_zpl
110
121
  }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
111
122
  end
112
123
 
113
124
  it "raises an error if the barcode type is not informed" do
114
- barcode = described_class.new :position => [100, 100], :data => "foobar"
125
+ barcode = described_class.new position: [100, 100], data: "foobar"
115
126
  expect {
116
127
  barcode.to_zpl
117
128
  }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the barcode type to be used is not given")
@@ -131,66 +142,62 @@ describe Zebra::Zpl::Barcode do
131
142
  }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the height to be used is not given")
132
143
  end
133
144
 
134
- it "raises an error if the narrow bar width is not given" do
145
+ it "raises an error if the neither of with, narrow_bar_width, or wide_bar_width are not given" do
146
+ valid_attributes.delete :width
135
147
  valid_attributes.delete :narrow_bar_width
136
-
137
- expect {
138
- barcode.to_zpl
139
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the narrow bar width to be used is not given")
140
- end
141
-
142
- it "raises an error if the wide bar width is not given" do
143
148
  valid_attributes.delete :wide_bar_width
144
-
145
149
  expect {
146
150
  barcode.to_zpl
147
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the wide bar width to be used is not given")
151
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the width to be used is not given")
148
152
  end
149
153
 
150
154
  it "begins with the command 'B'" do
151
- barcode.to_zpl.should =~ /\AB/
155
+ expect(tokens[4]).to eq '^BY'
152
156
  end
153
157
 
154
158
  it "contains the X position" do
155
- tokens[0].match(/B(\d+)/)[1].should == "100"
159
+ expect(tokens[2]).to eq '100'
156
160
  end
157
161
 
158
162
  it "contains the Y position" do
159
- tokens[1].should == "150"
163
+ expect(tokens[3]).to eq '150'
160
164
  end
161
165
 
162
- it "contains the barcode rotation" do
163
- tokens[2].should == Zebra::Zpl::Rotation::NO_ROTATION.to_s
166
+ it "contains the width" do
167
+ expect(tokens[5]).to eq '5'
164
168
  end
165
169
 
166
- it "contains the barcode type" do
167
- tokens[3].should == Zebra::Zpl::BarcodeType::CODE_128_AUTO
170
+ it "contains the ratio" do
171
+ expect(tokens[6]).to eq '2.0'
168
172
  end
169
173
 
170
- it "contains the barcode narrow bar width" do
171
- tokens[4].should == "4"
174
+ it "contains the barcode height" do
175
+ expect(tokens[7]).to eq '20'
172
176
  end
173
177
 
174
- it "contains the barcode wide bar width" do
175
- tokens[5].should == "6"
178
+ it "contains the barcode type" do
179
+ expect(tokens[8]).to start_with "^B#{Zebra::Zpl::BarcodeType::CODE_128_AUTO}"
176
180
  end
177
181
 
178
- it "contains the barcode height" do
179
- tokens[6].should == "20"
182
+ it "contains the barcode rotation" do
183
+ expect(tokens[0]).to start_with '^FW'
184
+ expect(tokens[0]).to end_with Zebra::Zpl::Rotation::NO_ROTATION.to_s
185
+ expect(tokens[8]).to start_with '^B'
186
+ expect(tokens[8]).to end_with Zebra::Zpl::Rotation::NO_ROTATION.to_s
180
187
  end
181
188
 
182
189
  it "contains the correct indication when the human readable code should be printed" do
183
- valid_attributes.merge! :print_human_readable_code => true
184
- tokens[7].should == "B"
190
+ valid_attributes.merge! print_human_readable_code: true
191
+ expect(tokens[9]).to eq 'Y'
185
192
  end
186
193
 
187
194
  it "contains the correct indication when the human readable code should not be printed" do
188
- valid_attributes.merge! :print_human_readable_code => false
189
- tokens[7].should == "N"
195
+ valid_attributes.merge! print_human_readable_code: false
196
+ expect(tokens[9]).to eq 'N'
190
197
  end
191
198
 
192
199
  it "contains the data to be printed in the barcode" do
193
- tokens[8].should == "\"foobar\""
200
+ expect(tokens[11]).to eq 'foobar'
194
201
  end
195
202
  end
196
203
  end
@@ -3,69 +3,65 @@ require 'spec_helper'
3
3
 
4
4
  describe Zebra::Zpl::Box do
5
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
6
+ box = described_class.new position: [20, 40]
7
+ expect(box.position).to eq [20, 40]
8
+ expect(box.x).to eq 20
9
+ expect(box.y).to eq 40
17
10
  end
18
11
 
19
12
  it "can be initialized with the line thckness " do
20
- box = described_class.new :line_thickness => 3
21
- box.line_thickness.should == 3
13
+ box = described_class.new line_thickness: 3
14
+ expect(box.line_thickness).to eq 3
22
15
  end
23
16
 
24
17
  describe "#to_zpl" do
25
- subject(:box) { described_class.new attributes }
26
- let(:attributes) { { :position => [20,40], :end_position => [60, 100], :line_thickness => 3 } }
18
+ subject(:box) { described_class.new attributes }
19
+ let(:attributes) {{
20
+ position: [20,40],
21
+ line_thickness: 3,
22
+ box_width: 5,
23
+ box_height: 4,
24
+ color: 'B',
25
+ rounding_degree: 6
26
+ }}
27
27
 
28
28
  it "raises an error if the X position was not informed" do
29
- box = described_class.new attributes.merge :position => [nil, 40]
29
+ box = described_class.new attributes.merge position: [nil, 40]
30
30
  expect {
31
31
  box.to_zpl
32
32
  }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the X value is not given")
33
33
  end
34
34
 
35
35
  it "raises an error if the Y position was not informed" do
36
- box = described_class.new attributes.merge :position => [20, nil]
36
+ box = described_class.new attributes.merge position: [20, nil]
37
37
  expect {
38
38
  box.to_zpl
39
39
  }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
40
40
  end
41
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]
42
+ it "raises an error if the line thickness was not informed" do
43
+ box = described_class.new attributes.merge line_thickness: nil
44
44
  expect {
45
45
  box.to_zpl
46
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the horizontal end position (X) is not given")
46
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the line thickness is not given")
47
47
  end
48
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]
49
+ it "raises an error if the box width was not informed" do
50
+ box = described_class.new attributes.merge box_width: nil
51
51
  expect {
52
52
  box.to_zpl
53
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the vertical end position (Y) is not given")
53
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the box_width is not given")
54
54
  end
55
55
 
56
- it "raises an error if the line thickness was not informed" do
57
- box = described_class.new attributes.merge :line_thickness => nil
56
+ it "raises an error if the box height was not informed" do
57
+ box = described_class.new attributes.merge box_height: nil
58
58
  expect {
59
59
  box.to_zpl
60
- }.to raise_error(Zebra::Zpl::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_zpl.should =~ /\AX/
60
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the box_height is not given")
65
61
  end
66
62
 
67
63
  it "contains the attributes in correct order" do
68
- box.to_zpl.should == "X20,40,3,60,100"
64
+ expect(box.to_zpl).to eq '^FO20,40^GB5,4,3,B,6^FS'
69
65
  end
70
66
  end
71
67
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Zebra::Zpl::CharacterSet do
4
4
  it "can be initialized with the number of data bits" do
5
5
  character_set = described_class.new :number_of_data_bits => 8
6
- character_set.number_of_data_bits.should == 8
6
+ expect(character_set.number_of_data_bits).to eq 8
7
7
  end
8
8
 
9
9
  it "raises an error when receiving an invalid number of data bits" do
@@ -14,7 +14,7 @@ describe Zebra::Zpl::CharacterSet do
14
14
 
15
15
  it "can be initialized with the language" do
16
16
  character_set = described_class.new :language => Zebra::Zpl::Language::PORTUGUESE
17
- character_set.language.should == Zebra::Zpl::Language::PORTUGUESE
17
+ expect(character_set.language).to eq Zebra::Zpl::Language::PORTUGUESE
18
18
  end
19
19
 
20
20
  it "raises an error with an invalid language" do
@@ -25,7 +25,7 @@ describe Zebra::Zpl::CharacterSet do
25
25
 
26
26
  it "can be initialized with the country code" do
27
27
  character_set = described_class.new :country_code => Zebra::Zpl::CountryCode::LATIN_AMERICA
28
- character_set.country_code.should == Zebra::Zpl::CountryCode::LATIN_AMERICA
28
+ expect(character_set.country_code).to eq Zebra::Zpl::CountryCode::LATIN_AMERICA
29
29
  end
30
30
 
31
31
  it "raises an error with an invalid country code" do
@@ -82,19 +82,19 @@ describe Zebra::Zpl::CharacterSet do
82
82
  end
83
83
 
84
84
  it "begins with the command 'I'" do
85
- character_set.to_zpl.should =~ /\AI/
85
+ expect(character_set.to_zpl).to match /\AI/
86
86
  end
87
87
 
88
88
  it "contains the number of data bits" do
89
- tokens[0].match(/I(\d)/)[1].should == "8"
89
+ expect(tokens[0].match(/I(\d)/)[1]).to eq "8"
90
90
  end
91
91
 
92
92
  it "contains the language" do
93
- tokens[1].should == Zebra::Zpl::Language::PORTUGUESE
93
+ expect(tokens[1]).to eq Zebra::Zpl::Language::PORTUGUESE
94
94
  end
95
95
 
96
96
  it "contains the country code" do
97
- tokens[2].should == Zebra::Zpl::CountryCode::LATIN_AMERICA
97
+ expect(tokens[2]).to eq Zebra::Zpl::CountryCode::LATIN_AMERICA
98
98
  end
99
99
  end
100
100
  end