Class: Prawn::SwissQRBill::QR::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/swiss_qr_bill/qr/data.rb

Overview

The data of the Swiss QR-bill

References: www.paymentstandards.ch/dam/downloads/ig-qr-bill-en.pdf

  • Chapter 4, Page 25

Defined Under Namespace

Classes: Field

Constant Summary collapse

FIELDS =

Available fields of the QR code data, ordered.

{
  # fixed: SPC
  qr_type: Field.new('SPC'),
  version: Field.new('0200'),
  # fixed: 1
  coding: Field.new('1'),
  iban: Field.new(nil, nil, false, :iban),
  # enum: S, K
  creditor_address_type: Field.new('K'),
  creditor_address_name: Field.new,
  creditor_address_line1: Field.new,
  creditor_address_line2: Field.new,
  creditor_address_postal_code: Field.new,
  creditor_address_city: Field.new,
  creditor_address_country: Field.new,
  # omit ultimate_creditor_* => for future use
  # enum: S, K
  ultimate_creditor_address_type: Field.new,
  ultimate_creditor_address_name: Field.new,
  ultimate_creditor_address_line1: Field.new,
  ultimate_creditor_address_line2: Field.new,
  ultimate_creditor_address_postal_code: Field.new,
  ultimate_creditor_address_city: Field.new,
  ultimate_creditor_address_country: Field.new,
  amount: Field.new(nil, ->(value) { value && format('%.2f', value) }),
  # enum: CHF, EUR
  currency: Field.new('CHF'),
  # enum: S, K
  debtor_address_type: Field.new,
  debtor_address_name: Field.new,
  debtor_address_line1: Field.new,
  debtor_address_line2: Field.new,
  debtor_address_postal_code: Field.new,
  debtor_address_city: Field.new,
  debtor_address_country: Field.new,
  # enum: QRR, SCOR, NON
  reference_type: Field.new('NON'),
  reference: Field.new(nil, ->(v) { v && v.delete(' ') }, false, :reference),
  unstructured_message: Field.new,
  # fixed: EPD
  trailer: Field.new('EPD'),
  bill_information: Field.new(nil, nil, true),
  # key-value pairs:
  alternative_parameters: Field.new(nil, nil, true)
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(fields = {}, options = {}) ⇒ Data

Returns a new instance of Data.



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/prawn/swiss_qr_bill/qr/data.rb', line 74

def initialize(fields = {}, options = {})
  @options = options || {}

  # set defaults
  FIELDS.each_key do |field|
    instance_variable_set("@#{field}", FIELDS[field].default)
  end

  # set given
  fields.each_key do |field|
    instance_variable_set("@#{field}", fields[field])
  end
end

Instance Method Details

#generateObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/prawn/swiss_qr_bill/qr/data.rb', line 88

def generate
  validate if @options[:validate]

  stack = []
  FIELDS.each_key do |k|
    var = instance_variable_get("@#{k}")

    # TODO: fix possible wrong format if alt parameters (last one) is given
    next if FIELDS[k][:skippable] && var.nil?

    # TODO: use #process; generate method should only call validate, then process, then render as string
    var = FIELDS[k][:format].call(var) if FIELDS[k][:format].is_a?(Proc)

    stack << var
  end
  stack.join("\r\n")
end

#processObject



106
107
108
109
110
111
112
# File 'lib/prawn/swiss_qr_bill/qr/data.rb', line 106

def process
  FIELDS.each_key do |k|
    var = instance_variable_get("@#{k}")

    instance_variable_set("@#{k}", FIELDS[k][:format].call(var)) if FIELDS[k][:format].is_a?(Proc)
  end
end

#validateObject



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/prawn/swiss_qr_bill/qr/data.rb', line 114

def validate
  FIELDS.each_key do |k|
    next unless FIELDS[k][:validation]

    var = instance_variable_get("@#{k}")

    call_validator(FIELDS[k][:validation], var)
  end

  true
end