Class: CheddargetterClientRails::Subscription

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/cheddargetter_client_rails/subscription.rb

Constant Summary collapse

Months =
("01".."12").freeze
Years =
(Date.current.year..Date.current.year+10).collect{|y| y.to_s }.freeze
CGKeys =
[   
  :planCode,
  :company,
  :firstName,
  :lastName,
  :ccFirstName,
  :ccLastName,
  :ccExpiration,
  :ccNumber,
  :ccLastFour,
  :ccCountry,
  :ccAddress,
  :ccCity,
  :ccState,
  :customerCode,
  :email,
  :zip    
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Subscription

Returns a new instance of Subscription.



85
86
87
# File 'lib/cheddargetter_client_rails/subscription.rb', line 85

def initialize(hash = {})
  hash.each { |k, v| send("#{k}=", v) }
end

Instance Attribute Details

#ccAddressObject

Returns the value of attribute ccAddress.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def ccAddress
  @ccAddress
end

#ccCityObject

Returns the value of attribute ccCity.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def ccCity
  @ccCity
end

#ccCountryObject

Returns the value of attribute ccCountry.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def ccCountry
  @ccCountry
end

#ccExpirationObject

Returns the value of attribute ccExpiration.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def ccExpiration
  @ccExpiration
end

#ccExpirationMonthObject

Returns the value of attribute ccExpirationMonth.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def ccExpirationMonth
  @ccExpirationMonth
end

#ccExpirationYearObject

Returns the value of attribute ccExpirationYear.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def ccExpirationYear
  @ccExpirationYear
end

#ccFirstNameObject

Returns the value of attribute ccFirstName.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def ccFirstName
  @ccFirstName
end

#ccLastFourObject

Returns the value of attribute ccLastFour.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def ccLastFour
  @ccLastFour
end

#ccLastNameObject

Returns the value of attribute ccLastName.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def ccLastName
  @ccLastName
end

#ccNumberObject

Returns the value of attribute ccNumber.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def ccNumber
  @ccNumber
end

#ccStateObject

Returns the value of attribute ccState.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def ccState
  @ccState
end

#companyObject

Returns the value of attribute company.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def company
  @company
end

#customerCodeObject

Returns the value of attribute customerCode.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def customerCode
  @customerCode
end

#emailObject

Returns the value of attribute email.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def email
  @email
end

#firstNameObject

Returns the value of attribute firstName.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def firstName
  @firstName
end

#lastNameObject

Returns the value of attribute lastName.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def lastName
  @lastName
end

#planCodeObject

Returns the value of attribute planCode.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def planCode
  @planCode
end

#zipObject

Returns the value of attribute zip.



27
28
29
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27

def zip
  @zip
end

Class Method Details

.build_from_response(response) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cheddargetter_client_rails/subscription.rb', line 97

def self.build_from_response(response)
  customer_subscription = response.try(:customer_subscription)
  customer_plan         = response.try(:customer_plan)

  if customer_plan and customer_subscription
    new(
      :firstName    => response.customer[:firstName],
      :lastName     => response.customer[:lastName],
      :email        => response.customer[:email],
      :ccLastFour   => customer_subscription[:ccLastFour], 
      :ccFirstName    => customer_subscription[:ccFirstName],
      :ccLastName     => customer_subscription[:ccLastName],
      :planCode     => customer_plan[:code],
      :zip          => customer_subscription[:ccZip],
      :ccExpiration => customer_subscription[:ccExpirationDate],
      :customerCode => response.customer[:code]        
    )
  end
end

.get(customer_code) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/cheddargetter_client_rails/subscription.rb', line 89

def self.get(customer_code)
  response = CGClient.customers_get(:customer_code => customer_code)

  if response.errors.blank?
    build_from_response(response)
  end
end

Instance Method Details

#add_errors_or_return_valid_response(response) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/cheddargetter_client_rails/subscription.rb', line 150

def add_errors_or_return_valid_response(response)
  #this returns cheddargetter errors.
  #hopefully most errors are handled before this in the valid? calls
  #which return prettier errors, but inevitably some will not be caught.
  if response.try(:errors).try(:any?)
    response.errors.each do |error|
      errors.add(:base, error[:text])
    end
  
    return false
  else
    response
  end    
end

#createObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/cheddargetter_client_rails/subscription.rb', line 131

def create
  response = CGClient.customers_new(
    :customer_code      => customerCode,
    :firstName => firstName,
    :lastName  => lastName,
    :email     => email,
    :subscription => {
      :planCode     => planCode,
      :ccFirstName  => ccFirstName,
      :ccLastName   => ccLastName,
      :ccNumber     => ccNumber,
      :ccExpiration => ccExpiration,
      :ccZip        => zip
    }
  )    

  add_errors_or_return_valid_response(response)
end

#destroyObject



186
187
188
189
190
191
# File 'lib/cheddargetter_client_rails/subscription.rb', line 186

def destroy
  raise "Invalid customer code" if customerCode.blank?
  response = CGClient.customers_delete({ :code => customerCode })

  add_errors_or_return_valid_response(response)    
end

#expiration_in_valid_format?Boolean

Returns:

  • (Boolean)


233
234
235
# File 'lib/cheddargetter_client_rails/subscription.rb', line 233

def expiration_in_valid_format?
  ccExpiration.match(/^\d\d\/\d\d\d\d$/)
end

#expiration_in_valid_month_format?Boolean

Returns:

  • (Boolean)


237
238
239
# File 'lib/cheddargetter_client_rails/subscription.rb', line 237

def expiration_in_valid_month_format?
  ccExpiration.match(/^\d\d\/$/)
end

#expiration_in_valid_year_format?Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/cheddargetter_client_rails/subscription.rb', line 241

def expiration_in_valid_year_format?
  ccExpiration.match(/^\/\d\d\d\d$/)
end

#fields_present?Boolean

Returns:

  • (Boolean)


210
211
212
213
214
215
# File 'lib/cheddargetter_client_rails/subscription.rb', line 210

def fields_present?
  CGKeys.collect do |key|
    val = send(key)
    val if val.present?
  end.compact.present?
end

#instance_variables_hashObject



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/cheddargetter_client_rails/subscription.rb', line 193

def instance_variables_hash
  {
    :customerCode => customerCode, 
    :firstName => firstName,
    :ccLastName => ccLastName,
    :ccFirstName => ccFirstName,
    :planCode => planCode,
    :zip => zip,
    :lastName => lastName,
    :email => email
  }     
end

#new_record?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/cheddargetter_client_rails/subscription.rb', line 117

def new_record?
  !CheddargetterClientRails::Subscription.get(customerCode)
end

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/cheddargetter_client_rails/subscription.rb', line 60

def paid_plan?
  if planCode
    plans = CGClient.plans_get
    !plans.plan(planCode)[:isFree]
  end
end

#saveObject



121
122
123
124
125
126
127
128
129
# File 'lib/cheddargetter_client_rails/subscription.rb', line 121

def save
  return false if !valid?
  
  if new_record?
    create
  else
    update
  end
end

#to_keyObject



206
207
208
# File 'lib/cheddargetter_client_rails/subscription.rb', line 206

def to_key
  nil
end

#unexpiredObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cheddargetter_client_rails/subscription.rb', line 67

def unexpired
  if ccExpiration.present?
    month, year = ccExpiration.split("/").collect{|string| string.to_i }
    year  = ('20' + year.to_s).to_i if year.size < 4
    month = ('0' + month.to_s).to_i if month.size < 2

    if Date.civil(year, month + 1) <= Date.today
      errors.add(:ccExpiration, 'has been reached')
    end
  end
end

#updateObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/cheddargetter_client_rails/subscription.rb', line 165

def update
  response = CGClient.customers_edit(
    {:customer_code      => customerCode},
    {
      :firstName => firstName,
      :lastName  => lastName,
      :email     => email,
      :subscription => {
        :planCode     => planCode,
        :ccFirstName  => ccFirstName,
        :ccLastName   => ccLastName,
        :ccNumber     => ccNumber,
        :ccExpiration => ccExpiration,
        :ccZip        => zip
      }
    }
  )    

  add_errors_or_return_valid_response(response)
end

#validates_presence_of_humanizedObject



79
80
81
82
83
# File 'lib/cheddargetter_client_rails/subscription.rb', line 79

def validates_presence_of_humanized
  if planCode.blank?
    self.errors.add(:base, 'You must select a billing plan')
  end
end