Class: RQRCodeCore::QRAlphanumeric

Inherits:
Object
  • Object
show all
Defined in:
lib/rqrcode_core/qrcode/qr_alphanumeric.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ QRAlphanumeric

Returns a new instance of QRAlphanumeric.



11
12
13
14
15
16
17
# File 'lib/rqrcode_core/qrcode/qr_alphanumeric.rb', line 11

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

  @data = data
end

Class Method Details

.valid_data?(data) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/rqrcode_core/qrcode/qr_alphanumeric.rb', line 19

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

Instance Method Details

#write(buffer) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rqrcode_core/qrcode/qr_alphanumeric.rb', line 23

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