Module: HealthCards::ChunkingUtils

Extended by:
ChunkingUtils
Included in:
ChunkingUtils
Defined in:
lib/health_cards/chunking_utils.rb

Overview

Split up a JWS into chunks if encoded size is above QR Code Size constraint

Constant Summary collapse

MAX_SINGLE_JWS_SIZE =
1195
MAX_CHUNK_SIZE =
1191

Instance Method Summary collapse

Instance Method Details

#jws_to_qr_chunks(jws) ⇒ Object

Splits jws into chunks and converts each string into numeric



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/health_cards/chunking_utils.rb', line 21

def jws_to_qr_chunks(jws)
  chunks = split_jws(jws.to_s).map { |c| convert_jws_to_numeric(c) }

  # if 1 chunk, attach prefix shc:/
  # if multiple chunks, attach prefix shc:/$orderNumber/$totalChunkCount
  if chunks.length == 1
    chunks[0] = "shc:/#{chunks[0]}"
  else
    chunks.map!.with_index(1) { |ch, i| "shc:/#{i}/#{chunks.length}/#{ch}" }
  end
  chunks
end

#qr_chunks_to_jws(qr_chunks) ⇒ Object

Assemble jws from qr code chunks



35
36
37
38
39
40
41
42
43
44
# File 'lib/health_cards/chunking_utils.rb', line 35

def qr_chunks_to_jws(qr_chunks)
  if qr_chunks.length == 1
    # Strip off shc:/ and convert numeric jws
    numeric_jws = qr_chunks[0].delete_prefix('shc:/')
    convert_numeric_jws numeric_jws
  else
    ordered_qr_chunks = strip_prefix_and_sort qr_chunks
    ordered_qr_chunks.map { |c| convert_numeric_jws(c) }.join
  end
end

#split_jws(jws) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/health_cards/chunking_utils.rb', line 10

def split_jws(jws)
  if jws.length <= MAX_SINGLE_JWS_SIZE
    [jws]
  else
    chunk_count = (jws.length / MAX_CHUNK_SIZE.to_f).ceil
    chunk_size  = (jws.length / chunk_count.to_f).ceil
    jws.scan(/.{1,#{chunk_size}}/)
  end
end