Class: HealthCards::QRCodes

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

Overview

Implements QR Code chunking in ruby

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chunks) ⇒ QRCodes

Creates a QRCodes from a set of encoded chunks

Parameters:

  • chunks (Array<String>)

    An array of QR Code chunks as a string



19
20
21
# File 'lib/health_cards/qr_codes.rb', line 19

def initialize(chunks)
  @chunks = chunks.sort.map.with_index(1) { |ch, i| Chunk.new(ordinal: i, input: ch) }
end

Instance Attribute Details

#chunksObject (readonly)

Returns the value of attribute chunks.



8
9
10
# File 'lib/health_cards/qr_codes.rb', line 8

def chunks
  @chunks
end

Class Method Details

.from_jws(jws) ⇒ HealthCards::QRCodes

Creates a QRCodes from a JWS

Parameters:

  • jws (String)

    the JWS string

Returns:



13
14
15
# File 'lib/health_cards/qr_codes.rb', line 13

def self.from_jws(jws)
  QRCodes.new(ChunkingUtils.jws_to_qr_chunks(jws.to_s))
end

Instance Method Details

#code_by_ordinal(num) ⇒ HealthCards::Chunk

Find a single QR Code chunk from this collection based on its ordinal position

Returns:



25
26
27
# File 'lib/health_cards/qr_codes.rb', line 25

def code_by_ordinal(num)
  chunks.find { |ch| ch.ordinal == num }
end

#to_jwsHealthCards::JWS

Combine all chunks and decodes it into a JWS object

Returns:



31
32
33
34
# File 'lib/health_cards/qr_codes.rb', line 31

def to_jws
  jws_string = ChunkingUtils.qr_chunks_to_jws(chunks.map(&:data))
  JWS.from_jws(jws_string)
end