Class: NmiDirectPost::CustomerVault

Inherits:
Base
  • Object
show all
Defined in:
lib/nmi_direct_post/customer_vault.rb

Constant Summary

Constants inherited from Base

Base::AUTH_PARAMS, Base::GET_URI, Base::NO_CONNECTION, Base::POST_URI

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Base

#response, #response_code, #response_text

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

establish_connection, generate_query_string, get, #logger, password, #persisted?, post, #success?, username

Constructor Details

#initialize(attributes) ⇒ CustomerVault

Returns a new instance of CustomerVault.



41
42
43
44
45
46
47
48
49
# File 'lib/nmi_direct_post/customer_vault.rb', line 41

def initialize(attributes)
  super
  if attributes[:customer_vault_id].blank?
    set_attributes(attributes.dup) unless attributes.empty?
  else
    @customer_vault_id = attributes[:customer_vault_id].to_i
    reload
  end
end

Class Attribute Details

.report_typeObject (readonly)

Returns the value of attribute report_type.



127
128
129
# File 'lib/nmi_direct_post/customer_vault.rb', line 127

def report_type
  @report_type
end

Instance Attribute Details

#customer_vaultObject (readonly)

Returns the value of attribute customer_vault.



29
30
31
# File 'lib/nmi_direct_post/customer_vault.rb', line 29

def customer_vault
  @customer_vault
end

#customer_vault_idObject (readonly)

Returns the value of attribute customer_vault_id.



29
30
31
# File 'lib/nmi_direct_post/customer_vault.rb', line 29

def customer_vault_id
  @customer_vault_id
end

#report_typeObject (readonly)

Returns the value of attribute report_type.



29
30
31
# File 'lib/nmi_direct_post/customer_vault.rb', line 29

def report_type
  @report_type
end

Class Method Details

.allObject



160
161
162
# File 'lib/nmi_direct_post/customer_vault.rb', line 160

def all
  limit
end

.all_idsObject



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/nmi_direct_post/customer_vault.rb', line 146

def all_ids
  @report_type = :customer_vault
  safe_params = generate_query_string([:report_type])
  NmiDirectPost.logger.debug { "Loading all NMI customer vaults using query: #{safe_params}" }
  begin
    customers = get(all_params(safe_params))["customer_vault"]
  ensure
    @report_type = nil
  end
  return [] if customers.nil?
  customers = customers["customer"]
  customers.collect { |customer| customer["customer_vault_id"].to_i }
end

.all_params(safe_params, target = self) ⇒ Object



164
165
166
# File 'lib/nmi_direct_post/customer_vault.rb', line 164

def all_params(safe_params, target=self)
  [safe_params, generate_query_string(Base::AUTH_PARAMS, target)].join('&')
end

.find_by_customer_vault_id(customer_vault_id, username = nil, password = nil) ⇒ Object

Raises:

  • (StandardError)


129
130
131
132
133
134
135
136
# File 'lib/nmi_direct_post/customer_vault.rb', line 129

def find_by_customer_vault_id(customer_vault_id, username=nil, password=nil)
  raise StandardError, "CustomerVaultID cannot be blank" if customer_vault_id.blank?
  begin
    new(customer_vault_id: customer_vault_id, username: username, password: password)
  rescue CustomerVaultNotFoundError
    return nil
  end
end

.first(limit = 1) ⇒ Object



138
139
140
# File 'lib/nmi_direct_post/customer_vault.rb', line 138

def first(limit = 1)
  limit(0, limit-1).first
end

.last(limit = 1) ⇒ Object



142
143
144
# File 'lib/nmi_direct_post/customer_vault.rb', line 142

def last(limit = 1)
  limit(-limit, -1).first
end

Instance Method Details

#checking?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/nmi_direct_post/customer_vault.rb', line 109

def checking?
  !@check_hash.blank?
end

#createObject



51
52
53
54
# File 'lib/nmi_direct_post/customer_vault.rb', line 51

def create
  post_action(:add)
  self.success?
end

#credit_card?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/nmi_direct_post/customer_vault.rb', line 105

def credit_card?
  !@cc_hash.blank?
end

#destroyObject



73
74
75
76
# File 'lib/nmi_direct_post/customer_vault.rb', line 73

def destroy
  post_action(:delete)
  self
end

#find!Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/nmi_direct_post/customer_vault.rb', line 113

def find!
  begin
    @report_type = :customer_vault
    safe_params = generate_query_string(MERCHANT_DEFINED_FIELDS + [:last_name, :email, :report_type]) # These are the only fields you can use when looking up without a customer_vault_id
    logger.info { "Querying NMI customer vault: #{safe_params}" }
    @customer_vault_id = self.class.get(self.class.all_params(safe_params, self))['customer_vault'][0]['customer_vault_id'] # This assumes there is only 1 result.
    # TODO: When there are multiple results, we don't know which one you want.  Maybe raise an error in that case?
    reload
  ensure
    @report_type = nil
  end
end

#reloadObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/nmi_direct_post/customer_vault.rb', line 78

def reload
  @report_type = :customer_vault
  if invalid?
    @report_type = nil
    return self
  end
  begin
    safe_params = customer_vault_instance_params
    logger.debug { "Loading NMI customer vault from customer_vault_id(#{customer_vault_id}) using query: #{safe_params}" }
    response = self.class.get(self.class.all_params(safe_params, self))["customer_vault"]
    raise CustomerVaultNotFoundError, "No record found for customer vault ID #{self.customer_vault_id}" if response.nil?
    attributes = response["customer"].with_indifferent_access
    READ_ONLY_ATTRIBUTES.each do |a|
      if attributes.key?(a)
        val = attributes.delete(a)
        instance_variable_set("@#{a}",val)
      end
    end
    set_attributes(attributes.tap { |_| _.delete(:customer_vault_id) })
  ensure
    @report_type = nil
    @attributes_to_update = nil
    @attributes_to_save = nil
  end
  self
end

#save!Object



67
68
69
70
71
# File 'lib/nmi_direct_post/customer_vault.rb', line 67

def save!
  post_action(:update)
  reload
  self.success?
end

#update!(attributes) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/nmi_direct_post/customer_vault.rb', line 56

def update!(attributes)
  begin
    set_attributes(attributes)
    post_action(:update)
  ensure
    @attributes_to_save.delete_if {|v| @attributes_to_update.include?(v) } if @attributes_to_save
    @attributes_to_update = nil
  end
  self
end