Module: QRParams

Defined in:
lib/qr-bills/qr-params.rb

Constant Summary collapse

QR_BILL_WITH_QR_REFERENCE =
"orange_with_reference"
QR_BILL_WITH_CREDITOR_REFERENCE =
"red_with_reference"
QR_BILL_WITHOUT_REFERENCE =
"red_without_reference"

Class Method Summary collapse

Class Method Details

.base_params_valid?(params) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
85
# File 'lib/qr-bills/qr-params.rb', line 75

def self.base_params_valid?(params)
  if params[:bill_type] == "" || params[:bill_type] == nil
    raise ArgumentError, "#{QRExceptions::INVALID_PARAMETERS}: bill type cannot be blank"
  end

  if params.dig(:bill_params, :currency) == "" || params.dig(:bill_params, :currency) == nil
    raise ArgumentError, "#{QRExceptions::INVALID_PARAMETERS}: currency cannot be blank"
  end
 
  true
end

.get_qr_paramsObject



6
7
8
9
10
11
12
13
14
15
16
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
# File 'lib/qr-bills/qr-params.rb', line 6

def self.get_qr_params
  {
    bill_type: "", # see global variables / README
    qrcode_format: nil, # png or svg, overwrites qrcode_filepath
    qrcode_filepath: "", # deprecated, where to store the qrcode, i.e. : /tmp/qrcode_1234.png
    fonts: {
      eot: File.expand_path("#{File.dirname(__FILE__)}/../../web/assets/fonts/LiberationSans-Regular.eot"),
      woff: File.expand_path("#{File.dirname(__FILE__)}/../../web/assets/fonts/LiberationSans-Regular.woff"),
      ttf: File.expand_path("#{File.dirname(__FILE__)}/../../web/assets/fonts/LiberationSans-Regular.ttf"),
      svg: File.expand_path("#{File.dirname(__FILE__)}/../../web/assets/fonts/LiberationSans-Regular.svg")
    },
    locales: {
      path: File.expand_path("#{File.dirname(__FILE__)}/../../config/locales")
    },
    bill_params: {
      language: I18n.locale,
      amount: 0.0,
      currency: "CHF",
      reference_type: "", # QRR = QR reference, SCOR = Creditor reference, NON = without reference
      reference: "", # qr reference or creditor reference (iso-11649)
      additionally_information: "",
      bill_information_coded: "",
      alternative_scheme_parameters: "",
      creditor: {
        address: {
          type: "S",
          name: "",
          street_name: "",
          building_number: "",
          postal_code: "",
          town: "",
          country: "",
          iban: ""
        },
      },
      debtor: {
        address: {
          type: "S",
          name: "",
          street_name: "",
          building_number: "",
          postal_code: "",
          town: "",
          country: "",
        },
      }
    },
    output_params: {
      format: "html"
    }
  }
end

.qr_bill_with_creditor_reference_valid?(params) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
108
109
# File 'lib/qr-bills/qr-params.rb', line 99

def self.qr_bill_with_creditor_reference_valid?(params)
  if params[:bill_params][:reference_type] != "SCOR"
    raise ArgumentError, "#{QRExceptions::INVALID_PARAMETERS}: reference type must be 'SCOR' for QR bill with (new) creditor reference"
  end
  
  if params[:bill_params][:reference] == "" || params[:bill_params][:reference] == nil
    raise ArgumentError, "#{QRExceptions::INVALID_PARAMETERS}: reference cannot be blank for QR bill with (new) creditor reference"
  end

  true
end

.qr_bill_with_qr_reference_valid?(params) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
97
# File 'lib/qr-bills/qr-params.rb', line 87

def self.qr_bill_with_qr_reference_valid?(params)
  if params[:bill_params][:reference_type] != "QRR"
    raise ArgumentError, "#{QRExceptions::INVALID_PARAMETERS}: reference type must be 'QRR' for QR bill with standard reference"
  end
  
  if params[:bill_params][:reference] == "" || params[:bill_params][:reference] == nil
    raise ArgumentError, "#{QRExceptions::INVALID_PARAMETERS}: reference cannot be blank for QR bill with standard reference"
  end

  true
end

.qr_bill_without_reference_valid?(params) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
120
121
# File 'lib/qr-bills/qr-params.rb', line 111

def self.qr_bill_without_reference_valid?(params)
  if params[:bill_params][:reference_type] != "NON"
    raise ArgumentError, "#{QRExceptions::INVALID_PARAMETERS}: reference type must be 'NON' for QR bill without reference"
  end
  
  if params[:bill_params][:reference] != ""
    raise ArgumentError, "#{QRExceptions::INVALID_PARAMETERS}: reference must be blank for QR bill without reference"
  end

  true
end

.valid?(params) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/qr-bills/qr-params.rb', line 59

def self.valid?(params)
  return false unless params.key?(:bill_type)
  return false unless QRParams.base_params_valid?(params)

  case params[:bill_type]
  when QRParams::QR_BILL_WITH_QR_REFERENCE
    QRParams.qr_bill_with_qr_reference_valid?(params)
  when QRParams::QR_BILL_WITH_CREDITOR_REFERENCE
    QRParams.qr_bill_with_creditor_reference_valid?(params)
  when QRParams::QR_BILL_WITHOUT_REFERENCE
    QRParams.qr_bill_without_reference_valid?(params)
  else
    raise ArgumentError, "#{QRExceptions::INVALID_PARAMETERS}: bill type is not supported"
  end
end