Class: HealthCards::Chunk

Inherits:
Object
  • Object
show all
Defined in:
lib/health_cards/chunk.rb

Overview

Represents a single QRCode in a sequence. This class is a shim to the RQRCode library to enable multimode encoding

Constant Summary collapse

SINGLE_REGEX =
%r{shc:/}.freeze
MULTI_REGEX =
%r{shc:/[0-9]*/[0-9]*/}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input:, ordinal: 1) ⇒ Chunk

Returns a new instance of Chunk.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/health_cards/chunk.rb', line 12

def initialize(input:, ordinal: 1)
  @ordinal = ordinal
  @data = input
  multi = MULTI_REGEX.match(input)

  prefix = multi ? multi.to_s : SINGLE_REGEX.match(input).to_s
  content = input.delete_prefix(prefix)

  @qrcode = RQRCode::QRCode.new([{ mode: :byte_8bit, data: prefix }, { mode: :number, data: content }],
                                max_size: 22, level: :l)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/health_cards/chunk.rb', line 7

def data
  @data
end

#ordinalObject (readonly)

Returns the value of attribute ordinal.



7
8
9
# File 'lib/health_cards/chunk.rb', line 7

def ordinal
  @ordinal
end

#qrcodeObject (readonly)

Returns the value of attribute qrcode.



7
8
9
# File 'lib/health_cards/chunk.rb', line 7

def qrcode
  @qrcode
end

Instance Method Details

#imageObject



28
29
30
# File 'lib/health_cards/chunk.rb', line 28

def image
  @qrcode.as_png(module_px_size: 2)
end

#qr_codeObject



24
25
26
# File 'lib/health_cards/chunk.rb', line 24

def qr_code
  @qrcode.qrcode
end