zebra-epl 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ tags
19
+ bin
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zebra-epl.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,11 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
10
+ end
11
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Cássio Marques
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Zebra::Epl
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'zebra-epl'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install zebra-epl
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/zebra/epl.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "cups"
2
+ require "zebra/epl/version"
3
+ require "zebra/epl/rotation"
4
+ require "zebra/epl/multipliers"
5
+ require "zebra/epl/print_mode"
6
+ require "zebra/epl/font"
7
+ require "zebra/epl/label"
8
+ require "zebra/epl/text"
9
+ require "zebra/epl/barcode"
10
+ require "zebra/epl/barcode_type"
11
+ require "zebra/print_job"
12
+
@@ -0,0 +1,51 @@
1
+ require "zebra/epl/printable"
2
+
3
+ module Zebra
4
+ module Epl
5
+ class Barcode
6
+ include Printable
7
+
8
+ class InvalidNarrowBarWidthError < StandardError; end
9
+ class InvalidWideBarWidthError < StandardError; end
10
+
11
+ attr_accessor :height
12
+ attr_reader :type, :narrow_bar_width, :wide_bar_width
13
+ attr_writer :print_human_readable_code
14
+
15
+ def type=(type)
16
+ BarcodeType.validate_barcode_type(type)
17
+ @type = type
18
+ end
19
+
20
+ def narrow_bar_width=(width)
21
+ raise InvalidNarrowBarWidthError unless (1..10).include?(width.to_i)
22
+ @narrow_bar_width = width
23
+ end
24
+
25
+ def wide_bar_width=(width)
26
+ raise InvalidWideBarWidthError unless (2..30).include?(width.to_i)
27
+ @wide_bar_width = width
28
+ end
29
+
30
+ def print_human_readable_code
31
+ @print_human_readable_code || false
32
+ end
33
+
34
+ def to_epl
35
+ check_attributes
36
+ human_readable = print_human_readable_code ? "B" : "N"
37
+ ["B#{x}", y, rotation, type, narrow_bar_width, wide_bar_width, height, human_readable, "\"#{data}\""].join(",")
38
+ end
39
+
40
+ private
41
+
42
+ def check_attributes
43
+ super
44
+ raise MissingAttributeError.new("the barcode type to be used is not given") unless @type
45
+ raise MissingAttributeError.new("the height to be used is not given") unless @height
46
+ raise MissingAttributeError.new("the narrow bar width to be used is not given") unless @narrow_bar_width
47
+ raise MissingAttributeError.new("the wide bar width to be used is not given") unless @wide_bar_width
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,24 @@
1
+ module Zebra
2
+ module Epl
3
+ module BarcodeType
4
+ class InvalidBarcodeTypeError < StandardError; end
5
+
6
+ CODE_39 = "3"
7
+ CODE_39_CHECK_DIGIT = "3C"
8
+ CODE_93 = "9"
9
+ CODE_128_AUTO = "1"
10
+ CODE_128_A = "1A"
11
+ CODE_128_B = "1B"
12
+ CODE_128_C = "1C"
13
+ CODABAR = "K"
14
+
15
+ def self.valid_barcode_type?(type)
16
+ %w(3 3C 9 1 1A 1B 1C K).include? type
17
+ end
18
+
19
+ def self.validate_barcode_type(type)
20
+ raise InvalidBarcodeTypeError unless valid_barcode_type?(type)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ module Zebra
2
+ module Epl
3
+ module Font
4
+ class InvalidFontError < StandardError; end
5
+
6
+ SIZE_1 = 1
7
+ SIZE_2 = 2
8
+ SIZE_3 = 3
9
+ SIZE_4 = 4
10
+ SIZE_5 = 5
11
+
12
+ def self.valid_font?(font)
13
+ (1..5).include?(font.to_i) || ('A'..'Z').include?(font)
14
+ end
15
+
16
+ def self.validate_font(font)
17
+ raise InvalidFontError unless valid_font?(font)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,68 @@
1
+ # encoding: utf-8
2
+ module Zebra
3
+ module Epl
4
+ class Label
5
+ class InvalidPrintSpeedError < StandardError; end
6
+ class InvalidPrintDensityError < StandardError; end
7
+ class PrintSpeedNotInformedError < StandardError; end
8
+
9
+ attr_reader :elements
10
+ attr_accessor :width, :length, :gap, :print_speed, :print_density
11
+
12
+ def initialize(options = {})
13
+ options.each_pair { |key, value| self.__send__("#{key}=", value) if self.respond_to?("#{key}=") }
14
+ @elements = []
15
+ end
16
+
17
+ def length_and_gap=(length_and_gap)
18
+ self.length = length_and_gap[0]
19
+ self.gap = length_and_gap[1]
20
+ end
21
+
22
+ def print_speed=(s)
23
+ raise InvalidPrintSpeedError unless (0..6).include?(s)
24
+ @print_speed = s
25
+ end
26
+
27
+ def print_density=(d)
28
+ raise InvalidPrintDensityError unless (0..15).include?(d)
29
+ @print_density = d
30
+ end
31
+
32
+ def <<(element)
33
+ elements << element
34
+ end
35
+
36
+ def dump_contents(io = STDOUT)
37
+ check_required_configurations
38
+ # Start options
39
+ io << "O\n"
40
+ # Q<label height in dots>,<space between labels in dots>
41
+ io << "Q#{length},#{gap}\n" if length && gap
42
+ # q<label width in dots>
43
+ io << "q#{width}\n" if width
44
+ # Print Speed (S command)
45
+ io << "S#{print_speed}\n"
46
+ # Density (D command)
47
+ io << "D#{print_density}\n" if print_density
48
+ # ZT = Printing from top of image buffer.
49
+
50
+ io << "\n"
51
+ # Start new label
52
+ io << "N\n"
53
+
54
+ elements.each do |element|
55
+ io << element.to_s << "\n"
56
+ end
57
+
58
+ io << "P0\n"
59
+ end
60
+
61
+ private
62
+
63
+ def check_required_configurations
64
+ raise PrintSpeedNotInformedError unless print_speed
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,42 @@
1
+ module Zebra
2
+ module Epl
3
+ module BaseMultiplier
4
+ class InvalidMultiplierError < StandardError; end
5
+
6
+ VALUE_1 = 1
7
+ VALUE_2 = 2
8
+ VALUE_3 = 3
9
+ VALUE_4 = 4
10
+ VALUE_5 = 5
11
+ VALUE_6 = 6
12
+ VALUE_7 = 7
13
+ VALUE_8 = 8
14
+
15
+ def self.included(base_module)
16
+ base_module.instance_eval do
17
+ def validate_multiplier(multiplier)
18
+ raise InvalidMultiplierError unless valid_multiplier?(multiplier)
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ module HorizontalMultiplier
25
+ include BaseMultiplier
26
+
27
+ def self.valid_multiplier?(multiplier)
28
+ (1..8).include? multiplier
29
+ end
30
+ end
31
+
32
+ module VerticalMultiplier
33
+ include BaseMultiplier
34
+
35
+ VALUE_9 = 9
36
+
37
+ def self.valid_multiplier?(multiplier)
38
+ (1..9).include? multiplier
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,18 @@
1
+ module Zebra
2
+ module Epl
3
+ module PrintMode
4
+ class InvalidPrintModeError < StandardError; end
5
+
6
+ NORMAL = "N"
7
+ REVERSE = "R"
8
+
9
+ def self.valid_mode?(mode)
10
+ %w(N R).include? mode
11
+ end
12
+
13
+ def self.validate_mode(mode)
14
+ raise InvalidPrintModeError unless valid_mode?(mode)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,39 @@
1
+ module Zebra
2
+ module Epl
3
+ module Printable
4
+ class MissingAttributeError < StandardError
5
+ def initialize(message)
6
+ super("Can't print if #{message}")
7
+ end
8
+ end
9
+
10
+ attr_reader :position, :x, :y
11
+ attr_accessor :data
12
+
13
+ def initialize(options = {})
14
+ options.each_pair { |attribute, value| self.__send__ "#{attribute}=", value }
15
+ end
16
+
17
+ def position=(coords)
18
+ @position, @x, @y = coords, coords[0], coords[1]
19
+ end
20
+
21
+ def rotation=(rot)
22
+ Rotation.validate_rotation rot
23
+ @rotation = rot
24
+ end
25
+
26
+ def rotation
27
+ @rotation || Rotation::NO_ROTATION
28
+ end
29
+
30
+ private
31
+
32
+ def check_attributes
33
+ raise MissingAttributeError.new("the X value is not given") unless @x
34
+ 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
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,20 @@
1
+ module Zebra
2
+ module Epl
3
+ module Rotation
4
+ class InvalidRotationError < StandardError; end
5
+
6
+ NO_ROTATION = 0
7
+ DEGREES_90 = 1
8
+ DEGREES_180 = 2
9
+ DEGREES_270 = 3
10
+
11
+ def self.valid_rotation?(rotation)
12
+ [NO_ROTATION, DEGREES_90, DEGREES_180, DEGREES_270].include? rotation
13
+ end
14
+
15
+ def self.validate_rotation(rotation)
16
+ raise InvalidRotationError unless valid_rotation?(rotation)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,59 @@
1
+ require "zebra/epl/printable"
2
+
3
+ module Zebra
4
+ module Epl
5
+ class Text
6
+ include Printable
7
+
8
+ attr_reader :font
9
+
10
+ def font=(f)
11
+ Font.validate_font f
12
+ @font = f
13
+ end
14
+
15
+ def print_mode=(mode)
16
+ PrintMode.validate_mode mode
17
+ @print_mode = mode
18
+ end
19
+
20
+ def print_mode
21
+ @print_mode || PrintMode::NORMAL
22
+ end
23
+
24
+ def h_multiplier
25
+ @h_multiplier || HorizontalMultiplier::VALUE_1
26
+ end
27
+
28
+ def v_multiplier
29
+ @v_multiplier || VerticalMultiplier::VALUE_1
30
+ end
31
+
32
+ def print_mode
33
+ @print_mode || PrintMode::NORMAL
34
+ end
35
+
36
+ def h_multiplier=(multiplier)
37
+ HorizontalMultiplier.validate_multiplier multiplier
38
+ @h_multiplier = multiplier
39
+ end
40
+
41
+ def v_multiplier=(multiplier)
42
+ VerticalMultiplier.validate_multiplier multiplier
43
+ @v_multiplier = multiplier
44
+ end
45
+
46
+ def to_epl
47
+ check_attributes
48
+ ["A#{x}", y, rotation, font, h_multiplier, v_multiplier, print_mode, "\"#{data}\""].join(",")
49
+ end
50
+
51
+ private
52
+
53
+ def check_attributes
54
+ super
55
+ raise MissingAttributeError.new("the font to be used is not given") unless @font
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,5 @@
1
+ module Zebra
2
+ module Epl
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ module Zebra
2
+ class PrintJob
3
+ class UnknownPrinter < StandardError
4
+ def initialize(printer)
5
+ super("Could not find a printer named #{printer}")
6
+ end
7
+ end
8
+
9
+ attr_reader :printer
10
+
11
+ def initialize(printer)
12
+ check_existent_printers printer
13
+
14
+ @printer = printer
15
+ end
16
+
17
+ def print(label)
18
+ Cups::PrintJob.new(label.path, @printer).print
19
+ end
20
+
21
+ private
22
+
23
+ def check_existent_printers(printer)
24
+ existent_printers = Cups.show_destinations
25
+ raise UnknownPrinter.new(printer) unless existent_printers.include?(printer)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,20 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require "zebra/epl"
9
+
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+
15
+ # Run specs in random order to surface order dependencies. If you find an
16
+ # order dependency and want to debug it, you can fix the order by providing
17
+ # the seed, which is printed after each run.
18
+ # --seed 1234
19
+ config.order = 'random'
20
+ end
@@ -0,0 +1,196 @@
1
+ require 'spec_helper'
2
+
3
+ describe Zebra::Epl::Barcode do
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
9
+ end
10
+
11
+ it "can be initialized with the barcode rotation" do
12
+ rotation = Zebra::Epl::Rotation::DEGREES_90
13
+ barcode = described_class.new :rotation => rotation
14
+ barcode.rotation.should == rotation
15
+ end
16
+
17
+ it "can be initialized with the barcode rotation" do
18
+ rotation = Zebra::Epl::Rotation::DEGREES_90
19
+ barcode = described_class.new :rotation => rotation
20
+ barcode.rotation.should == rotation
21
+ end
22
+
23
+ it "can be initialized with the barcode type" do
24
+ type = Zebra::Epl::BarcodeType::CODE_128_C
25
+ barcode = described_class.new :type => type
26
+ barcode.type.should == type
27
+ end
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
32
+ end
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
37
+ end
38
+
39
+ it "can be initialized with the barcode height" do
40
+ barcode = described_class.new :height => 20
41
+ barcode.height.should == 20
42
+ end
43
+
44
+ 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
47
+ end
48
+
49
+ describe "#rotation=" do
50
+ it "raises an error if the received rotation is invalid" do
51
+ expect {
52
+ described_class.new.rotation = 4
53
+ }.to raise_error(Zebra::Epl::Rotation::InvalidRotationError)
54
+ end
55
+ end
56
+
57
+ describe "#type=" do
58
+ it "raises an error if the received type is invalid" do
59
+ expect {
60
+ described_class.new.type = "ZZZ"
61
+ }.to raise_error(Zebra::Epl::BarcodeType::InvalidBarcodeTypeError)
62
+ end
63
+ end
64
+
65
+ describe "#narrow_bar_width=" do
66
+ it "raises an error if the type is Code 128 and the width is invalid" do
67
+ expect {
68
+ described_class.new :type => Zebra::Epl::BarcodeType::CODE_128_AUTO, :narrow_bar_width => 20
69
+ }.to raise_error(Zebra::Epl::Barcode::InvalidNarrowBarWidthError)
70
+ end
71
+ end
72
+
73
+ describe "#wide_bar_width=" do
74
+ it "raises an error if the type is Code 128 and the width is invalid" do
75
+ expect {
76
+ described_class.new :type => Zebra::Epl::BarcodeType::CODE_128_AUTO, :wide_bar_width => 40
77
+ }.to raise_error(Zebra::Epl::Barcode::InvalidWideBarWidthError)
78
+ end
79
+ end
80
+
81
+ describe "#print_human_readable_code" do
82
+ it "defaults to false" do
83
+ described_class.new.print_human_readable_code.should == false
84
+ end
85
+ end
86
+
87
+ describe "#to_epl" do
88
+ let(:valid_attributes) { {
89
+ :position => [100, 150],
90
+ :type => Zebra::Epl::BarcodeType::CODE_128_AUTO,
91
+ :height => 20,
92
+ :narrow_bar_width => 4,
93
+ :wide_bar_width => 6,
94
+ :data => "foobar"
95
+ } }
96
+ let(:barcode) { described_class.new valid_attributes }
97
+ let(:tokens) { barcode.to_epl.split(",") }
98
+
99
+ it "raises an error if the X position was not informed" do
100
+ barcode = described_class.new :position => [nil, 100], :data => "foobar"
101
+ expect {
102
+ barcode.to_epl
103
+ }.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the X value is not given")
104
+ end
105
+
106
+ it "raises an error if the Y position was not informed" do
107
+ barcode = described_class.new :position => [100, nil]
108
+ expect {
109
+ barcode.to_epl
110
+ }.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
111
+ end
112
+
113
+ it "raises an error if the barcode type is not informed" do
114
+ barcode = described_class.new :position => [100, 100], :data => "foobar"
115
+ expect {
116
+ barcode.to_epl
117
+ }.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the barcode type to be used is not given")
118
+ end
119
+
120
+ it "raises an error if the data to be printed was not informed" do
121
+ barcode.data = nil
122
+ expect {
123
+ barcode.to_epl
124
+ }.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the data to be printed is not given")
125
+ end
126
+
127
+ it "raises an error if the height to be used was not informed" do
128
+ barcode.height = nil
129
+ expect {
130
+ barcode.to_epl
131
+ }.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the height to be used is not given")
132
+ end
133
+
134
+ it "raises an error if the narrow bar width is not given" do
135
+ valid_attributes.delete :narrow_bar_width
136
+
137
+ expect {
138
+ barcode.to_epl
139
+ }.to raise_error(Zebra::Epl::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
+ valid_attributes.delete :wide_bar_width
144
+
145
+ expect {
146
+ barcode.to_epl
147
+ }.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the wide bar width to be used is not given")
148
+ end
149
+
150
+ it "begins with the command 'B'" do
151
+ barcode.to_epl.should =~ /\AB/
152
+ end
153
+
154
+ it "contains the X position" do
155
+ tokens[0].match(/B(\d+)/)[1].should == "100"
156
+ end
157
+
158
+ it "contains the Y position" do
159
+ tokens[1].should == "150"
160
+ end
161
+
162
+ it "contains the barcode rotation" do
163
+ tokens[2].should == Zebra::Epl::Rotation::NO_ROTATION.to_s
164
+ end
165
+
166
+ it "contains the barcode type" do
167
+ tokens[3].should == Zebra::Epl::BarcodeType::CODE_128_AUTO
168
+ end
169
+
170
+ it "contains the barcode narrow bar width" do
171
+ tokens[4].should == "4"
172
+ end
173
+
174
+ it "contains the barcode wide bar width" do
175
+ tokens[5].should == "6"
176
+ end
177
+
178
+ it "contains the barcode height" do
179
+ tokens[6].should == "20"
180
+ end
181
+
182
+ 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"
185
+ end
186
+
187
+ 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"
190
+ end
191
+
192
+ it "contains the data to be printed in the barcode" do
193
+ tokens[8].should == "\"foobar\""
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Zebra::Epl do
5
+ end
@@ -0,0 +1,91 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Zebra::Epl::Label do
6
+ subject(:label) { described_class.new :print_speed => 2 }
7
+
8
+ describe "#new" do
9
+ it "sets the label width" do
10
+ label = described_class.new :width => 300
11
+ label.width.should == 300
12
+ end
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
18
+ end
19
+
20
+ it "sets the printing speed" do
21
+ label = described_class.new :print_speed => 2
22
+ label.print_speed.should == 2
23
+ end
24
+
25
+ it "validates the printing speed" do
26
+ [-1, 8, "a"].each do |s|
27
+ expect {
28
+ described_class.new :print_speed => s
29
+ }.to raise_error(Zebra::Epl::Label::InvalidPrintSpeedError)
30
+ end
31
+ end
32
+
33
+ it "sets the print density" do
34
+ label = described_class.new :print_density => 10
35
+ label.print_density.should == 10
36
+ end
37
+
38
+ it "validates the print density" do
39
+ [-1, 16, "a"].each do |d|
40
+ expect {
41
+ described_class.new :print_density => d
42
+ }.to raise_error(Zebra::Epl::Label::InvalidPrintDensityError)
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "#<<" do
48
+ it "adds an item to the list of label elements" do
49
+ expect {
50
+ label << stub
51
+ }.to change { label.elements.count }.by 1
52
+ end
53
+ end
54
+
55
+ describe "#dump_contents" do
56
+ let(:io) { "" }
57
+
58
+ it "dumps its contents to the received IO" do
59
+ label << stub(:to_s => "foobar")
60
+ label << stub(:to_s => "blabla")
61
+ label.width = 100
62
+ label.length_and_gap = [200, 24]
63
+ label.print_speed = 3
64
+ label.print_density = 10
65
+ label.dump_contents(io)
66
+ io.should == "O\nQ200,24\nq100\nS3\nD10\n\nN\nfoobar\nblabla\nP0\n"
67
+ end
68
+
69
+ it "does not try to set the label width when it's not informed (falls back to autosense)" do
70
+ label.dump_contents(io)
71
+ io.should_not =~ /q/
72
+ end
73
+
74
+ it "does not try to set the length/gap when they were not informed (falls back to autosense)" do
75
+ label.dump_contents(io)
76
+ io.should_not =~ /Q/
77
+ end
78
+
79
+ it "does not try to set the print density when it's not informed (falls back to the default value)" do
80
+ label.dump_contents(io)
81
+ io.should_not =~ /D/
82
+ end
83
+
84
+ it "raises an error if the print speed was not informed" do
85
+ label = described_class.new
86
+ expect {
87
+ label.dump_contents(io)
88
+ }.to raise_error(Zebra::Epl::Label::PrintSpeedNotInformedError)
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,139 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Zebra::Epl::Text do
5
+ it "can be initialized with the position of the text to be printed" do
6
+ text = described_class.new :position => [20, 40]
7
+ text.position.should == [20,40]
8
+ text.x.should == 20
9
+ text.y.should == 40
10
+ end
11
+
12
+ it "can be initialized with the text rotation" do
13
+ rotation = Zebra::Epl::Rotation::DEGREES_90
14
+ text = described_class.new :rotation => rotation
15
+ text.rotation.should == rotation
16
+ end
17
+
18
+ it "can be initialized with the font to be used" do
19
+ font = Zebra::Epl::Font::SIZE_1
20
+ text = described_class.new :font => font
21
+ text.font.should == font
22
+ end
23
+
24
+ it "can be initialized with the horizontal multiplier" do
25
+ multiplier = Zebra::Epl::HorizontalMultiplier::VALUE_1
26
+ text = described_class.new :h_multiplier => multiplier
27
+ text.h_multiplier.should == multiplier
28
+ end
29
+
30
+ it "can be initialized with the vertical multiplier" do
31
+ multiplier = Zebra::Epl::VerticalMultiplier::VALUE_1
32
+ text = described_class.new :v_multiplier => multiplier
33
+ text.v_multiplier.should == multiplier
34
+ end
35
+
36
+ it "can be initialized with the data to be printed" do
37
+ data = "foobar"
38
+ text = described_class.new :data => data
39
+ text.data.should == data
40
+ end
41
+
42
+ it "can be initialized with the printing mode" do
43
+ print_mode = Zebra::Epl::PrintMode::REVERSE
44
+ text = described_class.new :print_mode => print_mode
45
+ text.print_mode.should == print_mode
46
+ end
47
+
48
+ describe "#rotation=" do
49
+ it "raises an error if the received rotation is invalid" do
50
+ expect {
51
+ described_class.new.rotation = 4
52
+ }.to raise_error(Zebra::Epl::Rotation::InvalidRotationError)
53
+ end
54
+ end
55
+
56
+ describe "#font=" do
57
+ it "raises an error if the received font is invalid" do
58
+ expect {
59
+ described_class.new.font = 6
60
+ }.to raise_error(Zebra::Epl::Font::InvalidFontError)
61
+ end
62
+ end
63
+
64
+ describe "#h_multiplier=" do
65
+ it "raises an error if the received multiplier is invalid" do
66
+ expect {
67
+ described_class.new.h_multiplier = 9
68
+ }.to raise_error(Zebra::Epl::HorizontalMultiplier::InvalidMultiplierError)
69
+ end
70
+ end
71
+
72
+ describe "#v_multiplier=" do
73
+ it "raises an error if the received multiplier is invalid" do
74
+ expect {
75
+ described_class.new.v_multiplier = 10
76
+ }.to raise_error(Zebra::Epl::VerticalMultiplier::InvalidMultiplierError)
77
+ end
78
+ end
79
+
80
+ describe "#print_mode=" do
81
+ it "raises an error if the received print mode is invalid" do
82
+ expect {
83
+ described_class.new.print_mode = "foo"
84
+ }.to raise_error(Zebra::Epl::PrintMode::InvalidPrintModeError)
85
+ end
86
+ end
87
+
88
+ describe "#to_epl" do
89
+ subject(:text) { described_class.new :position => [100, 150], :font => Zebra::Epl::Font::SIZE_3, :data => "foobar" }
90
+
91
+ it "raises an error if the X position was not informed" do
92
+ text = described_class.new :position => [nil, 100], :data => "foobar"
93
+ expect {
94
+ text.to_epl
95
+ }.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the X value is not given")
96
+ end
97
+
98
+ it "raises an error if the Y position was not informed" do
99
+ text = described_class.new :position => [100, nil]
100
+ expect {
101
+ text.to_epl
102
+ }.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
103
+ end
104
+
105
+ it "raises an error if the font is not informed" do
106
+ text = described_class.new :position => [100, 100], :data => "foobar"
107
+ expect {
108
+ text.to_epl
109
+ }.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the font to be used is not given")
110
+ end
111
+
112
+ it "raises an error if the data to be printed was not informed" do
113
+ text.data = nil
114
+ expect {
115
+ text.to_epl
116
+ }.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the data to be printed is not given")
117
+ end
118
+
119
+ it "begins width the 'A' command" do
120
+ text.to_epl.should =~ /\AA/
121
+ end
122
+
123
+ it "assumes 1 as the default horizontal multipler" do
124
+ text.to_epl.split(",")[4].to_i.should == Zebra::Epl::HorizontalMultiplier::VALUE_1
125
+ end
126
+
127
+ it "assumes 1 as the default vertical multiplier" do
128
+ text.to_epl.split(",")[5].to_i.should == Zebra::Epl::VerticalMultiplier::VALUE_1
129
+ end
130
+
131
+ it "assumes the normal print mode as the default" do
132
+ text.to_epl.split(",")[6].should == Zebra::Epl::PrintMode::NORMAL
133
+ end
134
+
135
+ it "assumes no rotation by default" do
136
+ text.to_epl.split(",")[2].to_i.should == Zebra::Epl::Rotation::NO_ROTATION
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Zebra::PrintJob do
4
+ before do
5
+ Cups.stub(:show_destinations).and_return(["Zebra", "Foobar"])
6
+ end
7
+
8
+ 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)
16
+ end
17
+
18
+ describe "#print" do
19
+ let(:label) { stub :path => "/foo/bar" }
20
+ let(:cups_job) { stub :print => true }
21
+
22
+ subject(:print_job) { described_class.new "Zebra" }
23
+
24
+ before { Cups::PrintJob.stub(:new).and_return(cups_job) }
25
+
26
+ 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
+ print_job.print label
29
+ end
30
+
31
+ it "prints the label" do
32
+ cups_job.should_receive(:print)
33
+ print_job.print label
34
+ end
35
+ end
36
+ end
data/zebra-epl.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'zebra/epl/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zebra-epl"
8
+ spec.version = Zebra::Epl::VERSION
9
+ spec.authors = ["Cássio Marques"]
10
+ spec.email = ["cassiommc@gmail.com"]
11
+ spec.description = %q{Print labels using EPL2 and Ruby}
12
+ spec.summary = %q{Simple DSL to create labels and send them to a Zebra printer using Ruby, EPL2 and CUPS}
13
+ spec.homepage = "http://github.com/cassiomarques/zebra-epl"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "cups"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "guard"
27
+ spec.add_development_dependency "guard-rspec"
28
+ end
metadata ADDED
@@ -0,0 +1,181 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zebra-epl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Cássio Marques
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cups
16
+ type: :runtime
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ! '>='
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ prerelease: false
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ type: :development
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '1.3'
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: '1.3'
45
+ prerelease: false
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ type: :development
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ prerelease: false
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ type: :development
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ prerelease: false
78
+ - !ruby/object:Gem::Dependency
79
+ name: guard
80
+ type: :development
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ prerelease: false
94
+ - !ruby/object:Gem::Dependency
95
+ name: guard-rspec
96
+ type: :development
97
+ requirement: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ version_requirements: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ prerelease: false
110
+ description: Print labels using EPL2 and Ruby
111
+ email:
112
+ - cassiommc@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - .rspec
119
+ - Gemfile
120
+ - Guardfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - lib/zebra/epl.rb
125
+ - lib/zebra/epl/barcode.rb
126
+ - lib/zebra/epl/barcode_type.rb
127
+ - lib/zebra/epl/font.rb
128
+ - lib/zebra/epl/label.rb
129
+ - lib/zebra/epl/multipliers.rb
130
+ - lib/zebra/epl/print_mode.rb
131
+ - lib/zebra/epl/printable.rb
132
+ - lib/zebra/epl/rotation.rb
133
+ - lib/zebra/epl/text.rb
134
+ - lib/zebra/epl/version.rb
135
+ - lib/zebra/print_job.rb
136
+ - spec/spec_helper.rb
137
+ - spec/zebra/epl/barcode_spec.rb
138
+ - spec/zebra/epl/epl_spec.rb
139
+ - spec/zebra/epl/label_spec.rb
140
+ - spec/zebra/epl/text_spec.rb
141
+ - spec/zebra/print_job_spec.rb
142
+ - zebra-epl.gemspec
143
+ homepage: http://github.com/cassiomarques/zebra-epl
144
+ licenses:
145
+ - MIT
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ none: false
152
+ requirements:
153
+ - - ! '>='
154
+ - !ruby/object:Gem::Version
155
+ segments:
156
+ - 0
157
+ version: '0'
158
+ hash: -2954244931162867193
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ none: false
161
+ requirements:
162
+ - - ! '>='
163
+ - !ruby/object:Gem::Version
164
+ segments:
165
+ - 0
166
+ version: '0'
167
+ hash: -2954244931162867193
168
+ requirements: []
169
+ rubyforge_project:
170
+ rubygems_version: 1.8.24
171
+ signing_key:
172
+ specification_version: 3
173
+ summary: Simple DSL to create labels and send them to a Zebra printer using Ruby,
174
+ EPL2 and CUPS
175
+ test_files:
176
+ - spec/spec_helper.rb
177
+ - spec/zebra/epl/barcode_spec.rb
178
+ - spec/zebra/epl/epl_spec.rb
179
+ - spec/zebra/epl/label_spec.rb
180
+ - spec/zebra/epl/text_spec.rb
181
+ - spec/zebra/print_job_spec.rb