Class: Laundry::PaymentsGateway::ResponseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/laundry/payments_gateway/models/response_model.rb

Direct Known Subclasses

Account, Client, Transaction, TransactionResponse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args) ⇒ Object



36
37
38
39
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 36

def method_missing(id, *args)
  return record[id.to_sym] if record.is_a?(Hash) && record.has_key?(id.to_sym)
  super
end

Instance Attribute Details

#merchantObject

Returns the value of attribute merchant.



9
10
11
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 9

def merchant
  @merchant
end

#recordObject

Returns the value of attribute record.



8
9
10
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 8

def record
  @record
end

Class Method Details

.dump(model) ⇒ Object



52
53
54
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 52

def self.dump(model)
  model ? YAML::dump(model.dumpable) : nil
end

.from_response(response, merchant) ⇒ Object



11
12
13
14
15
16
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 11

def self.from_response(response, merchant)
  model = self.new
  model.merchant = merchant
  model.initialize_with_response response
  model
end

.load(model_text) ⇒ Object



56
57
58
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 56

def self.load(model_text)
  model_text ? YAML::load(model_text) : nil
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 26

def blank?
  record == {} || record.nil? || !record
end

#dumpableObject

We don’t want to serialize the merchant because it has upsetting amounts of passwords in it.



46
47
48
49
50
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 46

def dumpable
  d = self.dup
  d.merchant = nil
  d
end

#initialize_with_response(response) ⇒ Object



18
19
20
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 18

def initialize_with_response(response)
  self.record = response.try(:to_hash) || {}
end

#require_merchant!Object



30
31
32
33
34
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 30

def require_merchant!
  unless merchant && merchant.class == Laundry::PaymentsGateway::Merchant
    raise MerchantNotSetError, "Tried to call a method that requires a merchant to be set on the model. Try calling merchant= first."
  end
end

#to_hashObject



22
23
24
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 22

def to_hash
  record.try(:to_hash)
end