zebra-epl 0.0.2 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,9 @@
1
1
  require "cups"
2
2
  require "tempfile"
3
3
  require "zebra/epl/version"
4
+ require "zebra/epl/language"
5
+ require "zebra/epl/country_code"
6
+ require "zebra/epl/character_set"
4
7
  require "zebra/epl/rotation"
5
8
  require "zebra/epl/multipliers"
6
9
  require "zebra/epl/print_mode"
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+ module Zebra
3
+ module Epl
4
+ class CharacterSet
5
+ class InvalidNumberOfDataBits < StandardError; end
6
+ class CountryCodeNotApplicableForNumberOfDataBits < StandardError; end
7
+ class MissingAttributeError < StandardError
8
+ def initialize(attr)
9
+ super("Can't set character set if the #{attr} is not given")
10
+ end
11
+ end
12
+
13
+ attr_reader :number_of_data_bits, :language, :country_code
14
+
15
+ def initialize(options = {})
16
+ options.each_pair { |attribute, value| self.__send__ "#{attribute}=", value }
17
+ end
18
+
19
+ def number_of_data_bits=(nodb)
20
+ raise InvalidNumberOfDataBits unless [7, 8, nil].include?(nodb)
21
+ @number_of_data_bits = nodb
22
+ end
23
+
24
+ def language=(l)
25
+ Language.validate_language(l) unless l.nil?
26
+ @language = l
27
+ end
28
+
29
+ def country_code=(code)
30
+ CountryCode.validate_country_code(code) unless code.nil?
31
+ @country_code = code
32
+ end
33
+
34
+ def to_epl
35
+ raise MissingAttributeError.new("language") if language.nil?
36
+ raise MissingAttributeError.new("number of data bits") if number_of_data_bits.nil?
37
+ raise MissingAttributeError.new("country code") if number_of_data_bits == 8 && country_code.nil?
38
+ raise CountryCodeNotApplicableForNumberOfDataBits if number_of_data_bits == 7 && !country_code.nil?
39
+ Language.validate_language_for_number_of_data_bits language, number_of_data_bits
40
+
41
+ ["I#{number_of_data_bits}", language, country_code].compact.join(",")
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+ module Zebra
3
+ module Epl
4
+ class CountryCode
5
+ class InvalidCountryCodeError < StandardError; end
6
+
7
+ BELGIUM = "032"
8
+ CANADA = "002"
9
+ DENMARK = "045"
10
+ FINLAND = "358"
11
+ FRANCE = "033"
12
+ GERMANY = "049"
13
+ NETHERLANDS = "031"
14
+ ITALY = "039"
15
+ LATIN_AMERICA = "003"
16
+ NORWAY = "047"
17
+ PORTUGAL = "351"
18
+ SOUTH_AFRICA = "027"
19
+ SPAIN = "034"
20
+ SWEDEN = "046"
21
+ SWITZERLAND = "041"
22
+ UK = "044"
23
+ USA = "001"
24
+
25
+ def self.valid_country_code?(code)
26
+ [BELGIUM, CANADA, DENMARK, FINLAND, FRANCE, GERMANY, NETHERLANDS,
27
+ ITALY, LATIN_AMERICA, NORWAY, PORTUGAL, SOUTH_AFRICA, SPAIN, SWEDEN, SWITZERLAND,
28
+ UK, USA].include?(code)
29
+ end
30
+
31
+ def self.validate_country_code(code)
32
+ raise InvalidCountryCodeError unless valid_country_code?(code)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -7,7 +7,7 @@ module Zebra
7
7
  class InvalidPrintDensityError < StandardError; end
8
8
  class PrintSpeedNotInformedError < StandardError; end
9
9
 
10
- attr_reader :elements
10
+ attr_reader :elements, :tempfile
11
11
  attr_accessor :width, :length, :gap, :print_speed, :print_density
12
12
 
13
13
  def initialize(options = {})
@@ -63,9 +63,14 @@ module Zebra
63
63
  tempfile = Tempfile.new "zebra_label"
64
64
  dump_contents tempfile
65
65
  tempfile.rewind
66
+ @tempfile = tempfile
66
67
  tempfile
67
68
  end
68
69
 
70
+ def persisted?
71
+ !!self.tempfile
72
+ end
73
+
69
74
  private
70
75
 
71
76
  def check_required_configurations
@@ -0,0 +1,76 @@
1
+ # encoding: utf-8
2
+ module Zebra
3
+ module Epl
4
+ class Language
5
+ class InvalidLanguageError < StandardError; end
6
+ class InvalidLanguageForNumberOfDataBitsError < StandardError; end
7
+
8
+ # 8 bits languages
9
+ ENGLISH_US = "0"
10
+ LATIN_1 = "1"
11
+ LATIN_2 = "2"
12
+ PORTUGUESE = "3"
13
+ FRENCH_CANADIAN = "4"
14
+ NORDIC = "5"
15
+ TURKISH = "6"
16
+ ICELANDIC = "7"
17
+ HEBREW = "8"
18
+ CYRILLIC = "9"
19
+ CYRILLIC_CIS_1 = "10"
20
+ GREEK = "11"
21
+ GREEK_1 = "12"
22
+ GREEK_2 = "13"
23
+ LATIN_1_WINDOWS = "A"
24
+ LATIN_2_WINDOWS = "B"
25
+ CYRILLIC_WINDOWS = "C"
26
+ GREEK_WINDOWS = "D"
27
+ TURKISH_WINDOWS = "E"
28
+ HEBREW_WINDOWS = "F"
29
+
30
+ # 7 bits languages
31
+ USA = "0"
32
+ BRITISH = "1"
33
+ GERMAN = "2"
34
+ FRENCH = "3"
35
+ DANISH = "4"
36
+ ITALIAN = "5"
37
+ SPANISH = "6"
38
+ SWEDISH = "7"
39
+ SWISS = "8"
40
+
41
+ def self.valid_language?(language)
42
+ ("0".."13").include?(language) || ("A".."F").include?(language)
43
+ end
44
+
45
+ def self.validate_language(language)
46
+ raise InvalidLanguageError unless valid_language?(language)
47
+ end
48
+
49
+ def self.validate_language_for_number_of_data_bits(language, number_of_data_bits)
50
+ if number_of_data_bits == 8
51
+ validate_8_data_bits_language language
52
+ elsif number_of_data_bits == 7
53
+ validate_7_data_bits_language language
54
+ else
55
+ raise ArgumentError.new("Unknown number of data bits")
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def self.validate_8_data_bits_language(language)
62
+ raise InvalidLanguageForNumberOfDataBitsError unless [ENGLISH_US,
63
+ LATIN_1, LATIN_2, PORTUGUESE, FRENCH_CANADIAN, NORDIC,
64
+ TURKISH, ICELANDIC, HEBREW, CYRILLIC, CYRILLIC_CIS_1, GREEK,
65
+ GREEK_1, GREEK_2, LATIN_1_WINDOWS, LATIN_2_WINDOWS, CYRILLIC_WINDOWS,
66
+ GREEK_WINDOWS, TURKISH_WINDOWS, HEBREW_WINDOWS].include?(language)
67
+ end
68
+
69
+ def self.validate_7_data_bits_language(language)
70
+ raise InvalidLanguageForNumberOfDataBitsError unless [USA, BRITISH,
71
+ GERMAN, FRENCH, DANISH, ITALIAN, SPANISH, SWEDISH, SWISS].include?(language)
72
+ end
73
+ end
74
+ end
75
+ end
76
+
@@ -1,5 +1,5 @@
1
1
  module Zebra
2
2
  module Epl
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
@@ -18,10 +18,9 @@ module Zebra
18
18
  tempfile = label.persist
19
19
 
20
20
  begin
21
- `lpr -P #{@printer} -o raw #{tempfile.path}`
21
+ send_to_printer tempfile.path
22
22
  ensure
23
23
  tempfile.close
24
- tempfile.unlink
25
24
  end
26
25
  end
27
26
 
@@ -31,5 +30,13 @@ module Zebra
31
30
  existent_printers = Cups.show_destinations
32
31
  raise UnknownPrinter.new(printer) unless existent_printers.include?(printer)
33
32
  end
33
+
34
+ def send_to_printer(path)
35
+ if RUBY_PLATFORM =~ /darwin/
36
+ `lpr -P #{@printer} -o raw #{path}`
37
+ else
38
+ `lp -d #{@printer} -o raw #{path}`
39
+ end
40
+ end
34
41
  end
35
42
  end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ describe Zebra::Epl::CharacterSet do
4
+ it "can be initialized with the number of data bits" do
5
+ character_set = described_class.new :number_of_data_bits => 8
6
+ character_set.number_of_data_bits.should == 8
7
+ end
8
+
9
+ it "raises an error when receiving an invalid number of data bits" do
10
+ expect {
11
+ described_class.new :number_of_data_bits => 9
12
+ }.to raise_error(Zebra::Epl::CharacterSet::InvalidNumberOfDataBits)
13
+ end
14
+
15
+ it "can be initialized with the language" do
16
+ character_set = described_class.new :language => Zebra::Epl::Language::PORTUGUESE
17
+ character_set.language.should == Zebra::Epl::Language::PORTUGUESE
18
+ end
19
+
20
+ it "raises an error with an invalid language" do
21
+ expect {
22
+ described_class.new :language => "G"
23
+ }.to raise_error(Zebra::Epl::Language::InvalidLanguageError)
24
+ end
25
+
26
+ it "can be initialized with the country code" do
27
+ character_set = described_class.new :country_code => Zebra::Epl::CountryCode::LATIN_AMERICA
28
+ character_set.country_code.should == Zebra::Epl::CountryCode::LATIN_AMERICA
29
+ end
30
+
31
+ it "raises an error with an invalid country code" do
32
+ expect {
33
+ described_class.new :country_code => "999"
34
+ }.to raise_error(Zebra::Epl::CountryCode::InvalidCountryCodeError)
35
+ end
36
+
37
+ describe "#to_epl" do
38
+ let(:valid_attributes) { {
39
+ :number_of_data_bits => 8,
40
+ :language => Zebra::Epl::Language::PORTUGUESE,
41
+ :country_code => Zebra::Epl::CountryCode::LATIN_AMERICA
42
+ } }
43
+ let(:character_set) { described_class.new valid_attributes }
44
+ let(:tokens) { character_set.to_epl.split(",") }
45
+
46
+ it "raises an error if the number of data bits was not informed" do
47
+ character_set = described_class.new valid_attributes.merge :number_of_data_bits => nil
48
+ expect {
49
+ character_set.to_epl
50
+ }.to raise_error(Zebra::Epl::CharacterSet::MissingAttributeError, "Can't set character set if the number of data bits is not given")
51
+ end
52
+
53
+ it "raises an error if the language was not informed" do
54
+ character_set = described_class.new valid_attributes.merge :language => nil
55
+ expect {
56
+ character_set.to_epl
57
+ }.to raise_error(Zebra::Epl::CharacterSet::MissingAttributeError, "Can't set character set if the language is not given")
58
+ end
59
+
60
+ it "raises an error if the country code was not informed and the number of bits is 8" do
61
+ character_set = described_class.new valid_attributes.merge :country_code => nil
62
+ expect {
63
+ character_set.to_epl
64
+ }.to raise_error(Zebra::Epl::CharacterSet::MissingAttributeError, "Can't set character set if the country code is not given")
65
+ end
66
+
67
+ it "raises an error if the country code was informed and the number of bits is 7" do
68
+ character_set = described_class.new valid_attributes.merge :country_code => Zebra::Epl::CountryCode::LATIN_AMERICA, :number_of_data_bits => 7
69
+ expect {
70
+ character_set.to_epl
71
+ }.to raise_error(Zebra::Epl::CharacterSet::CountryCodeNotApplicableForNumberOfDataBits)
72
+ end
73
+
74
+ it "does not raise an error if the country code was not informed and the number of data bits is 7" do
75
+ character_set = described_class.new :number_of_data_bits => 7, :language => Zebra::Epl::Language::USA, :country_code => nil
76
+ end
77
+
78
+ it "raises an error if the language is not supported by the given number of data bits" do
79
+ expect {
80
+ described_class.new(:number_of_data_bits => 7, :language => Zebra::Epl::Language::LATIN_1_WINDOWS).to_epl
81
+ }.to raise_error(Zebra::Epl::Language::InvalidLanguageForNumberOfDataBitsError)
82
+ end
83
+
84
+ it "begins with the command 'I'" do
85
+ character_set.to_epl.should =~ /\AI/
86
+ end
87
+
88
+ it "contains the number of data bits" do
89
+ tokens[0].match(/I(\d)/)[1].should == "8"
90
+ end
91
+
92
+ it "contains the language" do
93
+ tokens[1].should == Zebra::Epl::Language::PORTUGUESE
94
+ end
95
+
96
+ it "contains the country code" do
97
+ tokens[2].should == Zebra::Epl::CountryCode::LATIN_AMERICA
98
+ end
99
+ end
100
+ end
@@ -111,5 +111,23 @@ describe Zebra::Epl::Label do
111
111
  tempfile.should_receive(:rewind)
112
112
  label.persist
113
113
  end
114
+
115
+ it "sets the `tempfile` attribute" do
116
+ label.persist
117
+ label.tempfile.should == tempfile
118
+ end
119
+ end
120
+
121
+ describe "#persisted?" do
122
+ it "returns false if the `tempfile` attribute is nil" do
123
+ label = described_class.new :print_speed => 2
124
+ label.should_not be_persisted
125
+ end
126
+
127
+ it "returns true if the `tempfile` attribute is not nil" do
128
+ label = described_class.new :print_speed => 2
129
+ label.instance_variable_set(:@tempfile, stub)
130
+ label.should be_persisted
131
+ end
114
132
  end
115
133
  end
@@ -32,11 +32,5 @@ describe Zebra::PrintJob do
32
32
  print_job.should_receive(:`).with("lpr -P Zebra -o raw /foo/bar")
33
33
  print_job.print label
34
34
  end
35
-
36
- it "unlinks the file" do
37
- tempfile.should_receive(:close)
38
- tempfile.should_receive(:unlink)
39
- print_job.print label
40
- end
41
35
  end
42
36
  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.2
4
+ version: 0.0.5
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-11-24 00:00:00.000000000 Z
12
+ date: 2014-01-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cups
@@ -125,8 +125,11 @@ files:
125
125
  - lib/zebra/epl/barcode.rb
126
126
  - lib/zebra/epl/barcode_type.rb
127
127
  - lib/zebra/epl/box.rb
128
+ - lib/zebra/epl/character_set.rb
129
+ - lib/zebra/epl/country_code.rb
128
130
  - lib/zebra/epl/font.rb
129
131
  - lib/zebra/epl/label.rb
132
+ - lib/zebra/epl/language.rb
130
133
  - lib/zebra/epl/multipliers.rb
131
134
  - lib/zebra/epl/print_mode.rb
132
135
  - lib/zebra/epl/printable.rb
@@ -137,6 +140,7 @@ files:
137
140
  - spec/spec_helper.rb
138
141
  - spec/zebra/epl/barcode_spec.rb
139
142
  - spec/zebra/epl/box_spec.rb
143
+ - spec/zebra/epl/character_set_spec.rb
140
144
  - spec/zebra/epl/epl_spec.rb
141
145
  - spec/zebra/epl/label_spec.rb
142
146
  - spec/zebra/epl/text_spec.rb
@@ -157,7 +161,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
157
161
  segments:
158
162
  - 0
159
163
  version: '0'
160
- hash: 4481700405794095244
164
+ hash: -988489516192615375
161
165
  required_rubygems_version: !ruby/object:Gem::Requirement
162
166
  none: false
163
167
  requirements:
@@ -166,7 +170,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
166
170
  segments:
167
171
  - 0
168
172
  version: '0'
169
- hash: 4481700405794095244
173
+ hash: -988489516192615375
170
174
  requirements: []
171
175
  rubyforge_project:
172
176
  rubygems_version: 1.8.24
@@ -178,6 +182,7 @@ test_files:
178
182
  - spec/spec_helper.rb
179
183
  - spec/zebra/epl/barcode_spec.rb
180
184
  - spec/zebra/epl/box_spec.rb
185
+ - spec/zebra/epl/character_set_spec.rb
181
186
  - spec/zebra/epl/epl_spec.rb
182
187
  - spec/zebra/epl/label_spec.rb
183
188
  - spec/zebra/epl/text_spec.rb