Class: Girocode::Code

Inherits:
Object
  • Object
show all
Defined in:
lib/girocode/code.rb

Constant Summary collapse

ATTRIBUTES =
%i[bic name iban currency amount purpose creditor_reference reference bto_info]
MAX_PAYLOAD_BYTES =
331
AMOUNT_RANGE =
BigDecimal('0.01')..BigDecimal('999999999.99')

Instance Method Summary collapse

Constructor Details

#initialize(**attrs) ⇒ Code

Returns a new instance of Code.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/girocode/code.rb', line 11

def initialize(**attrs)
  if keys = attrs.keys - ATTRIBUTES and not keys.empty?
    raise ArgumentError, "Illegal attributes #{keys.inspect}"
  end
  attrs.each { |attr, value| send("#{attr}=", value) }
  raise ArgumentError, "iban is required" unless iban?
  raise ArgumentError, "name is required" unless name?
  raise ArgumentError, 'currency is required for amount' if amount && !currency?
  raise ArgumentError, "either creditor reference or reference may be set" if creditor_reference? && reference?
  raise ArgumentError, "payload too long" if payload.bytesize > MAX_PAYLOAD_BYTES
end

Instance Method Details

#amount=(value) ⇒ Object

Raises:

  • (ArgumentError)


53
54
55
56
57
58
# File 'lib/girocode/code.rb', line 53

def amount=(value)
  raise ArgumentError, 'amount is required' unless value
  value = BigDecimal(value, Float::DIG + 1)
  raise ArgumentError, "invalid amount #{value.inspect}" unless AMOUNT_RANGE.cover?(value)
  @amount = value
end

#bic=(value) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/girocode/code.rb', line 23

def bic=(value)
  if value.nil?
    @bic = nil
  else
    bic = Bank::BIC.new(value)
    raise ArgumentError, "Invalid BIC #{value.inspect}" unless bic.valid?
    @bic = bic.to_s
  end
end

#bto_info=(value) ⇒ Object



81
82
83
84
85
86
# File 'lib/girocode/code.rb', line 81

def bto_info=(value)
  unless value.nil?
    raise ArgumentError, "invalid bto_info #{value.inspect}" if value.include?("\n") || value.include?("\r") || value.size > 70
  end
  @bto_info = value
end

#creditor_reference=(value) ⇒ Object



67
68
69
70
71
72
# File 'lib/girocode/code.rb', line 67

def creditor_reference=(value)
  unless value.nil?
    raise ArgumentError, "invalid creditor reference #{value.inspect}" if value.include?("\n") || value.include?("\r") || value.size > 35
  end
  @creditor_reference = value
end

#currency=(value) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
# File 'lib/girocode/code.rb', line 47

def currency=(value)
  value = value.to_s.upcase
  raise ArgumentError, "Invalid currency" unless value.match?(/\A[A-Z]{3}\z/)
  @currency = value
end

#iban=(value) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
# File 'lib/girocode/code.rb', line 41

def iban=(value)
  iban = Bank::IBAN.new(value)
  raise ArgumentError, "Invalid IBAN #{value.inspect}" unless iban.valid?
  @iban = iban.to_s
end

#name=(value) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
# File 'lib/girocode/code.rb', line 33

def name=(value)
  value = value.strip
  raise ArgumentError, 'name is required' unless value
  raise ArgumentError, 'name too long' if value.size > 70
  raise ArgumentError, 'Illegal name' if value.include?("\n") || value.include?("\r")
  @name = value
end

#payloadObject



95
96
97
98
99
# File 'lib/girocode/code.rb', line 95

def payload
  ['BCD', '002', '1', 'SCT',
   bic, name, iban,formatted_amount, purpose,
   creditor_reference || reference, bto_info].map(&:to_s).join("\n")
end

#purpose=(value) ⇒ Object



60
61
62
63
64
65
# File 'lib/girocode/code.rb', line 60

def purpose=(value)
  unless value.nil?
    raise ArgumentError, "invalid purpose #{value.inspect}" unless value.match?(/\A[A-z0-9]{0,4}\z/)
  end
  @purpose = value
end

#reference=(value) ⇒ Object



74
75
76
77
78
79
# File 'lib/girocode/code.rb', line 74

def reference=(value)
  unless value.nil?
    raise ArgumentError, "invalid reference #{value.inspect}" if value.include?("\n") || value.include?("\r") || value.size > 140
  end
  @reference = value
end

#to_asciiObject



105
106
107
# File 'lib/girocode/code.rb', line 105

def to_ascii
  to_qrcode.to_s
end

#to_qrcodeObject



101
102
103
# File 'lib/girocode/code.rb', line 101

def to_qrcode
  RQRCode::QRCode.new(payload, level: :m, mode: :byte_8bit)
end