Class: BankCredentials::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/bank_credentials/base.rb

Direct Known Subclasses

Ebics, Hbci

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credential_hash, options = {}) ⇒ Base



25
26
27
28
29
# File 'lib/bank_credentials/base.rb', line 25

def initialize(credential_hash, options = {})
  credential_hash[:type] = credential_hash[:type] || self.class.type || nil
  @credentials = OpenStruct.new(credential_hash)
  validate! if options[:validate]
end

Instance Attribute Details

#credentialsObject (readonly)

Returns the value of attribute credentials.



6
7
8
# File 'lib/bank_credentials/base.rb', line 6

def credentials
  @credentials
end

Class Method Details

.attribute(attribute) ⇒ Object



18
19
20
21
22
23
# File 'lib/bank_credentials/base.rb', line 18

def self.attribute(attribute)
  @attributes = [] if @attributes.nil?
  @attributes << attribute
  def_delegator :@credentials, attribute
  def_delegator :@credentials, "#{attribute}=".to_sym
end

.attributesObject



13
14
15
16
# File 'lib/bank_credentials/base.rb', line 13

def self.attributes
  @attributes = [] if @attributes.nil?
  @attributes
end

.typeObject



9
10
11
# File 'lib/bank_credentials/base.rb', line 9

def self.type
  name.nil? ? 'base' : name.split('::').last.downcase
end

Instance Method Details

#attributesObject



39
40
41
# File 'lib/bank_credentials/base.rb', line 39

def attributes
  self.class.attributes
end

#encodeObject



35
36
37
# File 'lib/bank_credentials/base.rb', line 35

def encode
  Base64.urlsafe_encode64(to_json)
end

#to_jsonObject



31
32
33
# File 'lib/bank_credentials/base.rb', line 31

def to_json
  to_h.to_json
end

#valid?Boolean



43
44
45
46
47
# File 'lib/bank_credentials/base.rb', line 43

def valid?
  attributes.each do |attribute|
    return false if @credentials[attribute].nil? || @credentials[attribute].empty?
  end
end

#validate!Object

Raises:

  • (self.class::Errors::Invalid)


49
50
51
# File 'lib/bank_credentials/base.rb', line 49

def validate!
  raise self.class::Errors::Invalid unless valid?
end