Class: Spree::BankPayments::DetailSet

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/bank_payments/detail_set.rb

Overview

One set of payable coordinates for an account -- a local scheme, or SWIFT.

fields is an ordered list of label/value pairs rather than named keys because bank coordinates are not standardised: the UK uses a sort code and account number, the US a routing number, Poland's Elixir something else again. Named columns or fixed keys would mean a migration per market.

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ DetailSet

Returns a new instance of DetailSet.

Parameters:

  • raw (Hash)

    anything that is not a Hash is treated as an empty detail set rather than raising. details is admin-editable JSON and provider-supplied data, so the shape is not guaranteed: a JSON object pasted where an array belongs arrives here as an [key, value] pair, and a provider following the README's old (wrong) [[label, value]] field shape arrives as an Array too. Both used to raise NoMethodError deep inside a validation or mid-sync. Unusable input must surface as a form error or a skipped account, never a 500.



18
19
20
# File 'app/models/spree/bank_payments/detail_set.rb', line 18

def initialize(raw)
  @raw = raw.is_a?(Hash) ? raw.transform_keys(&:to_s) : {}
end

Instance Method Details

#beneficiary_addressObject



34
35
36
# File 'app/models/spree/bank_payments/detail_set.rb', line 34

def beneficiary_address
  @raw['beneficiary_address']
end

#beneficiary_nameObject



30
31
32
# File 'app/models/spree/bank_payments/detail_set.rb', line 30

def beneficiary_name
  @raw['beneficiary_name'].presence
end

#fieldsArray<Array(String, String)>

Returns ordered [label, value] pairs.

Returns:

  • (Array<Array(String, String)>)

    ordered [label, value] pairs



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/spree/bank_payments/detail_set.rb', line 39

def fields
  # grep(Hash): a field entry that is not an object (e.g. the
  # `[label, value]` pair the README wrongly documented until 5.2.0) is
  # dropped, not raised on. An account whose every field is dropped is
  # simply not `usable?`.
  Array(@raw['fields']).grep(Hash).filter_map do |field|
    f = field.transform_keys(&:to_s)
    value = f['value'].to_s.strip
    next if value.empty?

    [f['label'].to_s, value]
  end
end

#labelObject



22
23
24
# File 'app/models/spree/bank_payments/detail_set.rb', line 22

def label
  @raw['label'].presence
end

#schemesObject



26
27
28
# File 'app/models/spree/bank_payments/detail_set.rb', line 26

def schemes
  Array(@raw['schemes']).map(&:to_s)
end

#to_hObject



57
58
59
# File 'app/models/spree/bank_payments/detail_set.rb', line 57

def to_h
  @raw
end

#usable?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/models/spree/bank_payments/detail_set.rb', line 53

def usable?
  fields.any?
end