Module: Bakong::Khqr::Controllers::Decode

Defined in:
lib/bakong/khqr/controllers/decode.rb

Overview

Parses a KHQR string into a snake_case symbol-keyed Hash. Tag 30 (merchant) is normalized to tag 29 in the output so downstream consumers only need to handle one merchant_type discriminator.

Class Method Summary collapse

Class Method Details

.call(khqr_string) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bakong/khqr/controllers/decode.rb', line 17

def call(khqr_string)
  all_fields = KHQR_TAG.map { |el| el[:tag] }
  subtag_set = KHQR_TAG.select { |el| el[:sub] }.map { |el| el[:tag] }
  sub_tag_input = KHQR_SUBTAG[:input]
  sub_tag_compare = KHQR_SUBTAG[:compare]

  tags = {}
  merchant_type = nil
  last_tag = ""
  is_merchant_tag = false
  remainder = khqr_string

  until remainder.nil? || remainder.empty?
    slice = Helpers::CutString.call(remainder)
    tag = slice[:tag]
    value = slice[:value]
    remainder = slice[:remainder]

    break if tag == last_tag

    if tag == "30"
      merchant_type = "30"
      tag = "29"
      is_merchant_tag = true
    elsif tag == "29"
      merchant_type = "29"
    end

    tags[tag] = value if all_fields.include?(tag)
    last_tag = tag
  end

  decode_value = { merchant_type: merchant_type }
  sub_tag_input.each { |el| decode_value.merge!(el[:data]) }

  KHQR_TAG.each do |khqr_tag|
    tag = khqr_tag[:tag]
    value = tags[tag]
    input_value = value

    if subtag_set.include?(tag)
      schema = sub_tag_input.find { |el| el[:tag] == tag }[:data]
      input_data = deep_dup(schema)

      while value && !value.empty?
        cut = Helpers::CutString.call(value)
        sub_tag = cut[:tag]
        sub_value = cut[:value]
        value = cut[:remainder]

        name_subtag = sub_tag_compare
                      .select { |el| el[:tag] == tag }
                      .find { |el| el[:sub_tag] == sub_tag }

        next unless name_subtag

        name = name_subtag[:name]
        name = :merchant_id if is_merchant_tag && name == :account_information
        input_data[name] = sub_value
        input_value = input_data
      end

      decode_value.merge!(input_value) if input_value.is_a?(Hash)
    else
      decode_value[khqr_tag[:type]] = value
    end
  end

  decode_value
end

.deep_dup(hash) ⇒ Object



88
89
90
# File 'lib/bakong/khqr/controllers/decode.rb', line 88

def deep_dup(hash)
  hash.each_with_object({}) { |(k, v), acc| acc[k] = v.is_a?(Hash) ? deep_dup(v) : v }
end