Class: CheddargetterClientRails::Subscription
- Inherits:
-
Object
- Object
- CheddargetterClientRails::Subscription
- 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
-
#ccAddress ⇒ Object
Returns the value of attribute ccAddress.
-
#ccCity ⇒ Object
Returns the value of attribute ccCity.
-
#ccCountry ⇒ Object
Returns the value of attribute ccCountry.
-
#ccExpiration ⇒ Object
Returns the value of attribute ccExpiration.
-
#ccExpirationMonth ⇒ Object
Returns the value of attribute ccExpirationMonth.
-
#ccExpirationYear ⇒ Object
Returns the value of attribute ccExpirationYear.
-
#ccFirstName ⇒ Object
Returns the value of attribute ccFirstName.
-
#ccLastFour ⇒ Object
Returns the value of attribute ccLastFour.
-
#ccLastName ⇒ Object
Returns the value of attribute ccLastName.
-
#ccNumber ⇒ Object
Returns the value of attribute ccNumber.
-
#ccState ⇒ Object
Returns the value of attribute ccState.
-
#company ⇒ Object
Returns the value of attribute company.
-
#customerCode ⇒ Object
Returns the value of attribute customerCode.
-
#email ⇒ Object
Returns the value of attribute email.
-
#firstName ⇒ Object
Returns the value of attribute firstName.
-
#lastName ⇒ Object
Returns the value of attribute lastName.
-
#planCode ⇒ Object
Returns the value of attribute planCode.
-
#zip ⇒ Object
Returns the value of attribute zip.
Class Method Summary collapse
Instance Method Summary collapse
- #add_errors_or_return_valid_response(response) ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
- #expiration_in_valid_format? ⇒ Boolean
- #expiration_in_valid_month_format? ⇒ Boolean
- #expiration_in_valid_year_format? ⇒ Boolean
- #fields_present? ⇒ Boolean
-
#initialize(hash = {}) ⇒ Subscription
constructor
A new instance of Subscription.
- #instance_variables_hash ⇒ Object
- #new_record? ⇒ Boolean
- #paid_plan? ⇒ Boolean
- #save ⇒ Object
- #to_key ⇒ Object
- #unexpired ⇒ Object
- #update ⇒ Object
- #validates_presence_of_humanized ⇒ Object
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
#ccAddress ⇒ Object
Returns the value of attribute ccAddress.
27 28 29 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27 def ccAddress @ccAddress end |
#ccCity ⇒ Object
Returns the value of attribute ccCity.
27 28 29 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27 def ccCity @ccCity end |
#ccCountry ⇒ Object
Returns the value of attribute ccCountry.
27 28 29 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27 def ccCountry @ccCountry end |
#ccExpiration ⇒ Object
Returns the value of attribute ccExpiration.
27 28 29 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27 def ccExpiration @ccExpiration end |
#ccExpirationMonth ⇒ Object
Returns the value of attribute ccExpirationMonth.
27 28 29 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27 def ccExpirationMonth @ccExpirationMonth end |
#ccExpirationYear ⇒ Object
Returns the value of attribute ccExpirationYear.
27 28 29 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27 def ccExpirationYear @ccExpirationYear end |
#ccFirstName ⇒ Object
Returns the value of attribute ccFirstName.
27 28 29 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27 def ccFirstName @ccFirstName end |
#ccLastFour ⇒ Object
Returns the value of attribute ccLastFour.
27 28 29 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27 def ccLastFour @ccLastFour end |
#ccLastName ⇒ Object
Returns the value of attribute ccLastName.
27 28 29 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27 def ccLastName @ccLastName end |
#ccNumber ⇒ Object
Returns the value of attribute ccNumber.
27 28 29 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27 def ccNumber @ccNumber end |
#ccState ⇒ Object
Returns the value of attribute ccState.
27 28 29 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27 def ccState @ccState end |
#company ⇒ Object
Returns the value of attribute company.
27 28 29 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27 def company @company end |
#customerCode ⇒ Object
Returns the value of attribute customerCode.
27 28 29 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27 def customerCode @customerCode end |
#email ⇒ Object
Returns the value of attribute email.
27 28 29 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27 def email @email end |
#firstName ⇒ Object
Returns the value of attribute firstName.
27 28 29 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27 def firstName @firstName end |
#lastName ⇒ Object
Returns the value of attribute lastName.
27 28 29 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27 def lastName @lastName end |
#planCode ⇒ Object
Returns the value of attribute planCode.
27 28 29 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 27 def planCode @planCode end |
#zip ⇒ Object
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 |
#create ⇒ Object
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 |
#destroy ⇒ Object
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
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
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
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
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_hash ⇒ Object
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
117 118 119 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 117 def new_record? !CheddargetterClientRails::Subscription.get(customerCode) end |
#paid_plan? ⇒ 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 |
#save ⇒ Object
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_key ⇒ Object
206 207 208 |
# File 'lib/cheddargetter_client_rails/subscription.rb', line 206 def to_key nil end |
#unexpired ⇒ Object
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 |
#update ⇒ Object
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_humanized ⇒ Object
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 |