Class: PayCertify::Gateway::Base::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/paycertify/gateway/base/resource.rb

Direct Known Subclasses

Charge, CreditCard, Customer, Transaction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Resource

Returns a new instance of Resource.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/paycertify/gateway/base/resource.rb', line 9

def initialize(attributes)
  self.original_attributes = attributes
  self.client = PayCertify::Gateway::Client.new(api_key: api_key, mode: mode)

  if validatable?
    # Validate + attribute assignment
    validation.attributes.each do |key, value|
      self.send("#{key}=", value)
    end

    self.errors = validation.errors
  else
    # Attribute assignment only
    self.class.const_get('ATTRIBUTES').each do |key|
      self.send("#{key}=", attributes[key])
    end
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/paycertify/gateway/base/resource.rb', line 5

def client
  @client
end

#errorsObject

Returns the value of attribute errors.



5
6
7
# File 'lib/paycertify/gateway/base/resource.rb', line 5

def errors
  @errors
end

#original_attributesObject

Returns the value of attribute original_attributes.



5
6
7
# File 'lib/paycertify/gateway/base/resource.rb', line 5

def original_attributes
  @original_attributes
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/paycertify/gateway/base/resource.rb', line 5

def response
  @response
end

Instance Method Details

#attributesObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/paycertify/gateway/base/resource.rb', line 43

def attributes
  {}.tap do |attributes|
    self.class.const_get('ATTRIBUTES').each do |attribute|
      value = self.send(attribute)
      attributes[attribute] = value if value.present?
    end

    if response.present?
      attributes['gateway_response'] = response
    end

    attributes
  end
end

#attributes_to_gateway_formatObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/paycertify/gateway/base/resource.rb', line 69

def attributes_to_gateway_format
  {}.tap do |formatted|
    attribute_mapping = PayCertify::Gateway::AttributeMapping
    mapping_name = self.class.name.underscore.split('/').last

    attribute_mapping.send(mapping_name).each do |key, value|
      [value].flatten.tap do |method_chain|
        new_value = method_chain.map { |method_name| self.send(method_name) }.join
        formatted[key] = new_value
      end
    end

    formatted
  end
end

#save!Object



62
63
64
65
66
67
# File 'lib/paycertify/gateway/base/resource.rb', line 62

def save!
  self.response = client.post(
    path: self.class.const_get('API_ENDPOINT'),
    data: attributes_to_gateway_format
  )
end

#success?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/paycertify/gateway/base/resource.rb', line 28

def success?
  errors.empty?
end

#to_jsonObject



58
59
60
# File 'lib/paycertify/gateway/base/resource.rb', line 58

def to_json
  JSON.generate(attributes)
end

#validatable?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
# File 'lib/paycertify/gateway/base/resource.rb', line 32

def validatable?
  self.class.const_get('Validation')
  true
rescue NameError
  false
end

#validationObject



39
40
41
# File 'lib/paycertify/gateway/base/resource.rb', line 39

def validation
  @validation ||= self.class.const_get('Validation').new(original_attributes)
end