Class: QrCodeMaker::QRAlphanumeric

Inherits:
Object
  • Object
show all
Defined in:
lib/qr_code_maker/encoder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ QRAlphanumeric

Returns a new instance of QRAlphanumeric.



560
561
562
563
# File 'lib/qr_code_maker/encoder.rb', line 560

def initialize(data)
  raise QRCodeArgumentError, "Not a alpha numeric uppercase string `#{data}`" unless QRAlphanumeric.valid_data?(data)
  @data = data
end

Class Method Details

.valid_data?(data) ⇒ Boolean

Returns:

  • (Boolean)


565
566
567
# File 'lib/qr_code_maker/encoder.rb', line 565

def self.valid_data?(data)
  (data.chars - ALPHANUMERIC).empty?
end

Instance Method Details

#write(buffer) ⇒ Object



569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/qr_code_maker/encoder.rb', line 569

def write(buffer)
  buffer.alphanumeric_encoding_start(@data.size)
  @data.size.times do |i|
    if i % 2 == 0
      if i == (@data.size - 1)
        value = ALPHANUMERIC.index(@data[i])
        buffer.put(value, 6)
      else
        value = (ALPHANUMERIC.index(@data[i]) * 45) + ALPHANUMERIC.index(@data[i + 1])
        buffer.put(value, 11)
      end
    end
  end
end