Class: Laundry::PaymentsGateway::ResponseModel

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

Overview

ResponseModel

A container model for a serialized response and a Laundry::PaymentsGateway::Merchant object. The response is serialized using to_hash if possible, otherwise it's initialized to an empty hash and the value is stored in self.record

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


49
50
51
52
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 49

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


15
16
17
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 15

def merchant
  @merchant
end

#recordObject

Returns the value of attribute record


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

def record
  @record
end

Class Method Details

.dump(model) ⇒ Object


65
66
67
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 65

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

.from_response(response, merchant) ⇒ Object

ResponseModel.from_response(response, merchant)

A constructor method for building ResponseModel objects from a response and a merchant. The response

response - must implement a to_hash method merchant - A Laundry Merchant object (?)


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

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

.load(model_text) ⇒ Object


69
70
71
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 69

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

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)

39
40
41
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 39

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.


59
60
61
62
63
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 59

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

#initialize_with_response(response) ⇒ Object


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

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

#require_merchant!Object


43
44
45
46
47
# File 'lib/laundry/payments_gateway/models/response_model.rb', line 43

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


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

def to_hash
  record.try(:to_hash)
end