Adds character set stuff
Cássio Marques
committed Jan 08, 2014
commit ebf759b533356ba7e623622ffae13d6caa02b0d2
Showing 5
changed files with
260 additions
and 0 deletions
zebra/epl.rb b/lib/zebra/epl.rb
+3
-0
| @@ | @@ -1,6 +1,9 @@ |
| require "cups" | |
| require "tempfile" | |
| require "zebra/epl/version" | |
| + | require "zebra/epl/language" |
| + | require "zebra/epl/country_code" |
| + | require "zebra/epl/character_set" |
| require "zebra/epl/rotation" | |
| require "zebra/epl/multipliers" | |
| require "zebra/epl/print_mode" | |
zebra/epl/character_set.rb b/lib/zebra/epl/character_set.rb
+45
-0
| @@ | @@ -0,0 +1,45 @@ |
| + | # encoding: utf-8 |
| + | module Zebra |
| + | module Epl |
| + | class CharacterSet |
| + | class InvalidNumberOfDataBits < StandardError; end |
| + | class CountryCodeNotApplicableForNumberOfDataBits < StandardError; end |
| + | class MissingAttributeError < StandardError |
| + | def initialize(attr) |
| + | super("Can't set character set if the #{attr} is not given") |
| + | end |
| + | end |
| + | |
| + | attr_reader :number_of_data_bits, :language, :country_code |
| + | |
| + | def initialize(options = {}) |
| + | options.each_pair { |attribute, value| self.__send__ "#{attribute}=", value } |
| + | end |
| + | |
| + | def number_of_data_bits=(nodb) |
| + | raise InvalidNumberOfDataBits unless [7, 8, nil].include?(nodb) |
| + | @number_of_data_bits = nodb |
| + | end |
| + | |
| + | def language=(l) |
| + | Language.validate_language(l) unless l.nil? |
| + | @language = l |
| + | end |
| + | |
| + | def country_code=(code) |
| + | CountryCode.validate_country_code(code) unless code.nil? |
| + | @country_code = code |
| + | end |
| + | |
| + | def to_epl |
| + | raise MissingAttributeError.new("language") if language.nil? |
| + | raise MissingAttributeError.new("number of data bits") if number_of_data_bits.nil? |
| + | raise MissingAttributeError.new("country code") if number_of_data_bits == 8 && country_code.nil? |
| + | raise CountryCodeNotApplicableForNumberOfDataBits if number_of_data_bits == 7 && !country_code.nil? |
| + | Language.validate_language_for_number_of_data_bits language, number_of_data_bits |
| + | |
| + | ["I#{number_of_data_bits}", language, country_code].compact.join(",") |
| + | end |
| + | end |
| + | end |
| + | end |
zebra/epl/country_code.rb b/lib/zebra/epl/country_code.rb
+36
-0
| @@ | @@ -0,0 +1,36 @@ |
| + | # encoding: utf-8 |
| + | module Zebra |
| + | module Epl |
| + | class CountryCode |
| + | class InvalidCountryCodeError < StandardError; end |
| + | |
| + | BELGIUM = "032" |
| + | CANADA = "002" |
| + | DENMARK = "045" |
| + | FINLAND = "358" |
| + | FRANCE = "033" |
| + | GERMANY = "049" |
| + | NETHERLANDS = "031" |
| + | ITALY = "039" |
| + | LATIN_AMERICA = "003" |
| + | NORWAY = "047" |
| + | PORTUGAL = "351" |
| + | SOUTH_AFRICA = "027" |
| + | SPAIN = "034" |
| + | SWEDEN = "046" |
| + | SWITZERLAND = "041" |
| + | UK = "044" |
| + | USA = "001" |
| + | |
| + | def self.valid_country_code?(code) |
| + | [BELGIUM, CANADA, DENMARK, FINLAND, FRANCE, GERMANY, NETHERLANDS, |
| + | ITALY, LATIN_AMERICA, NORWAY, PORTUGAL, SOUTH_AFRICA, SPAIN, SWEDEN, SWITZERLAND, |
| + | UK, USA].include?(code) |
| + | end |
| + | |
| + | def self.validate_country_code(code) |
| + | raise InvalidCountryCodeError unless valid_country_code?(code) |
| + | end |
| + | end |
| + | end |
| + | end |
zebra/epl/language.rb b/lib/zebra/epl/language.rb
+76
-0
| @@ | @@ -0,0 +1,76 @@ |
| + | # encoding: utf-8 |
| + | module Zebra |
| + | module Epl |
| + | class Language |
| + | class InvalidLanguageError < StandardError; end |
| + | class InvalidLanguageForNumberOfDataBitsError < StandardError; end |
| + | |
| + | # 8 bits languages |
| + | ENGLISH_US = "0" |
| + | LATIN_1 = "1" |
| + | LATIN_2 = "2" |
| + | PORTUGUESE = "3" |
| + | FRENCH_CANADIAN = "4" |
| + | NORDIC = "5" |
| + | TURKISH = "6" |
| + | ICELANDIC = "7" |
| + | HEBREW = "8" |
| + | CYRILLIC = "9" |
| + | CYRILLIC_CIS_1 = "10" |
| + | GREEK = "11" |
| + | GREEK_1 = "12" |
| + | GREEK_2 = "13" |
| + | LATIN_1_WINDOWS = "A" |
| + | LATIN_2_WINDOWS = "B" |
| + | CYRILLIC_WINDOWS = "C" |
| + | GREEK_WINDOWS = "D" |
| + | TURKISH_WINDOWS = "E" |
| + | HEBREW_WINDOWS = "F" |
| + | |
| + | # 7 bits languages |
| + | USA = "0" |
| + | BRITISH = "1" |
| + | GERMAN = "2" |
| + | FRENCH = "3" |
| + | DANISH = "4" |
| + | ITALIAN = "5" |
| + | SPANISH = "6" |
| + | SWEDISH = "7" |
| + | SWISS = "8" |
| + | |
| + | def self.valid_language?(language) |
| + | ("0".."13").include?(language) || ("A".."F").include?(language) |
| + | end |
| + | |
| + | def self.validate_language(language) |
| + | raise InvalidLanguageError unless valid_language?(language) |
| + | end |
| + | |
| + | def self.validate_language_for_number_of_data_bits(language, number_of_data_bits) |
| + | if number_of_data_bits == 8 |
| + | validate_8_data_bits_language language |
| + | elsif number_of_data_bits == 7 |
| + | validate_7_data_bits_language language |
| + | else |
| + | raise ArgumentError.new("Unknown number of data bits") |
| + | end |
| + | end |
| + | |
| + | private |
| + | |
| + | def self.validate_8_data_bits_language(language) |
| + | raise InvalidLanguageForNumberOfDataBitsError unless [ENGLISH_US, |
| + | LATIN_1, LATIN_2, PORTUGUESE, FRENCH_CANADIAN, NORDIC, |
| + | TURKISH, ICELANDIC, HEBREW, CYRILLIC, CYRILLIC_CIS_1, GREEK, |
| + | GREEK_1, GREEK_2, LATIN_1_WINDOWS, LATIN_2_WINDOWS, CYRILLIC_WINDOWS, |
| + | GREEK_WINDOWS, TURKISH_WINDOWS, HEBREW_WINDOWS].include?(language) |
| + | end |
| + | |
| + | def self.validate_7_data_bits_language(language) |
| + | raise InvalidLanguageForNumberOfDataBitsError unless [USA, BRITISH, |
| + | GERMAN, FRENCH, DANISH, ITALIAN, SPANISH, SWEDISH, SWISS].include?(language) |
| + | end |
| + | end |
| + | end |
| + | end |
| + | |
spec/zebra/epl/character_set_spec.rb
+100
-0
| @@ | @@ -0,0 +1,100 @@ |
| + | require 'spec_helper' |
| + | |
| + | describe Zebra::Epl::CharacterSet do |
| + | it "can be initialized with the number of data bits" do |
| + | character_set = described_class.new :number_of_data_bits => 8 |
| + | character_set.number_of_data_bits.should == 8 |
| + | end |
| + | |
| + | it "raises an error when receiving an invalid number of data bits" do |
| + | expect { |
| + | described_class.new :number_of_data_bits => 9 |
| + | }.to raise_error(Zebra::Epl::CharacterSet::InvalidNumberOfDataBits) |
| + | end |
| + | |
| + | it "can be initialized with the language" do |
| + | character_set = described_class.new :language => Zebra::Epl::Language::PORTUGUESE |
| + | character_set.language.should == Zebra::Epl::Language::PORTUGUESE |
| + | end |
| + | |
| + | it "raises an error with an invalid language" do |
| + | expect { |
| + | described_class.new :language => "G" |
| + | }.to raise_error(Zebra::Epl::Language::InvalidLanguageError) |
| + | end |
| + | |
| + | it "can be initialized with the country code" do |
| + | character_set = described_class.new :country_code => Zebra::Epl::CountryCode::LATIN_AMERICA |
| + | character_set.country_code.should == Zebra::Epl::CountryCode::LATIN_AMERICA |
| + | end |
| + | |
| + | it "raises an error with an invalid country code" do |
| + | expect { |
| + | described_class.new :country_code => "999" |
| + | }.to raise_error(Zebra::Epl::CountryCode::InvalidCountryCodeError) |
| + | end |
| + | |
| + | describe "#to_epl" do |
| + | let(:valid_attributes) { { |
| + | :number_of_data_bits => 8, |
| + | :language => Zebra::Epl::Language::PORTUGUESE, |
| + | :country_code => Zebra::Epl::CountryCode::LATIN_AMERICA |
| + | } } |
| + | let(:character_set) { described_class.new valid_attributes } |
| + | let(:tokens) { character_set.to_epl.split(",") } |
| + | |
| + | it "raises an error if the number of data bits was not informed" do |
| + | character_set = described_class.new valid_attributes.merge :number_of_data_bits => nil |
| + | expect { |
| + | character_set.to_epl |
| + | }.to raise_error(Zebra::Epl::CharacterSet::MissingAttributeError, "Can't set character set if the number of data bits is not given") |
| + | end |
| + | |
| + | it "raises an error if the language was not informed" do |
| + | character_set = described_class.new valid_attributes.merge :language => nil |
| + | expect { |
| + | character_set.to_epl |
| + | }.to raise_error(Zebra::Epl::CharacterSet::MissingAttributeError, "Can't set character set if the language is not given") |
| + | end |
| + | |
| + | it "raises an error if the country code was not informed and the number of bits is 8" do |
| + | character_set = described_class.new valid_attributes.merge :country_code => nil |
| + | expect { |
| + | character_set.to_epl |
| + | }.to raise_error(Zebra::Epl::CharacterSet::MissingAttributeError, "Can't set character set if the country code is not given") |
| + | end |
| + | |
| + | it "raises an error if the country code was informed and the number of bits is 7" do |
| + | character_set = described_class.new valid_attributes.merge :country_code => Zebra::Epl::CountryCode::LATIN_AMERICA, :number_of_data_bits => 7 |
| + | expect { |
| + | character_set.to_epl |
| + | }.to raise_error(Zebra::Epl::CharacterSet::CountryCodeNotApplicableForNumberOfDataBits) |
| + | end |
| + | |
| + | it "does not raise an error if the country code was not informed and the number of data bits is 7" do |
| + | character_set = described_class.new :number_of_data_bits => 7, :language => Zebra::Epl::Language::USA, :country_code => nil |
| + | end |
| + | |
| + | it "raises an error if the language is not supported by the given number of data bits" do |
| + | expect { |
| + | described_class.new(:number_of_data_bits => 7, :language => Zebra::Epl::Language::LATIN_1_WINDOWS).to_epl |
| + | }.to raise_error(Zebra::Epl::Language::InvalidLanguageForNumberOfDataBitsError) |
| + | end |
| + | |
| + | it "begins with the command 'I'" do |
| + | character_set.to_epl.should =~ /\AI/ |
| + | end |
| + | |
| + | it "contains the number of data bits" do |
| + | tokens[0].match(/I(\d)/)[1].should == "8" |
| + | end |
| + | |
| + | it "contains the language" do |
| + | tokens[1].should == Zebra::Epl::Language::PORTUGUESE |
| + | end |
| + | |
| + | it "contains the country code" do |
| + | tokens[2].should == Zebra::Epl::CountryCode::LATIN_AMERICA |
| + | end |
| + | end |
| + | end |