Class: CheddarGetter::Response
- Inherits:
-
Object
- Object
- CheddarGetter::Response
- Defined in:
- lib/cheddar_getter/response.rb
Constant Summary collapse
- SPECIAL_ARRAY_KEYS =
{ :plans => :plan, :items => :item, :subscriptions => :subscription, :customers => :customer, :invoices => :invoice, :charges => :charge, :transactions => :transaction, :errors => :error }
- ARRAY_TO_HASH_BY_CODE =
not charges, because codes aren’t unique not invoices, transactions, subscriptions, or errors because they don’t have codes
[:plans, :items, :customers]
- KEY_TO_DATA_TYPE =
{ :isActive => :boolean, :isFree => :boolean, :trialDays => :integer, :setupChargeAmount => :float, :recurringChargeAmount => :float, :billingFrequencyQuantity => :integer, :createdDatetime => :datetime, :quantityIncluded => :float, :isPeriodic => :boolean, :overageAmount => :float, :isVatExempt => :boolean, :firstContactDatetime => :datetime, :modifiedDatetime => :datetime, :canceledDatetime => :datetime, :ccExpirationDate => :date, :quantity => :float, :billingDatetime => :datetime, :eachAmount => :float, :number => :integer, :amount => :float, :transactedDatetime => :datetime, :vatRate => :float }
Instance Attribute Summary collapse
-
#clean_response ⇒ Object
Returns the value of attribute clean_response.
-
#raw_response ⇒ Object
Returns the value of attribute raw_response.
Instance Method Summary collapse
-
#[](value) ⇒ Object
access the root keys of the response directly, such as :customers, :plans, or :errors.
-
#customer(code = nil) ⇒ Object
Returns the given customer.
-
#customer_active?(code = nil) ⇒ Boolean
Get an array representation of a single customer’s current subscription is not provided and the response contains more than one customer.
-
#customer_canceled?(code = nil) ⇒ Boolean
true if the customer is canceled.
-
#customer_invoice(code = nil) ⇒ Object
Returns the current open invoice for the given customer.
-
#customer_invoices(code = nil) ⇒ Object
Returns all of the invoices for the given customer.
-
#customer_item(item_code = nil, code = nil) ⇒ Object
Info about the given item for the given customer.
-
#customer_item_quantity_overage(item_code = nil, code = nil) ⇒ Object
The overage amount for the given item for the given customer.
-
#customer_item_quantity_overage_cost(item_code = nil, code = nil) ⇒ Object
The current overage cost for the given item for the given customer.
-
#customer_item_quantity_remaining(item_code = nil, code = nil) ⇒ Object
The amount remaining for a given item for a given customer.
-
#customer_last_billed_invoice(code = nil) ⇒ Object
Returns the last billed invoice for the given customer.
-
#customer_last_transaction(code = nil) ⇒ Object
Returns the last transaction for the given customer.
- #customer_one_time_invoices(code = nil) ⇒ Object
-
#customer_outstanding_invoices(code = nil) ⇒ Object
Returns an array of any currently outstanding invoices for the given customer.
- #customer_paypal_preapproval_url(code = nil) ⇒ Object
-
#customer_plan(code = nil) ⇒ Object
Returns the current plan for the given customer.
-
#customer_subscription(code = nil) ⇒ Object
Returns the current subscription for the given customer.
-
#customer_subscriptions(code = nil) ⇒ Object
Returns all the subscriptions for the given customer.
-
#customer_transactions(code = nil) ⇒ Object
Returns all of the transactions for the given customer.
-
#customer_waiting_for_paypal?(code = nil) ⇒ Boolean
Is this customer’s account pending paypal preapproval confirmation?.
-
#error_messages ⇒ Object
the error messages (if there were any) if the CheddarGetter response was invalid.
-
#initialize(response) ⇒ Response
constructor
:nodoc:.
-
#plan(code = nil) ⇒ Object
Returns the given plan.
-
#plan_item(item_code = nil, code = nil) ⇒ Object
Returns the given item for the given plan.
-
#plan_items(code = nil) ⇒ Object
Returns the items for the given plan.
-
#valid? ⇒ Boolean
true if the response from CheddarGetter was valid.
Constructor Details
#initialize(response) ⇒ Response
:nodoc:
46 47 48 49 |
# File 'lib/cheddar_getter/response.rb', line 46 def initialize(response) #:nodoc: self.raw_response = response create_clean_response end |
Instance Attribute Details
#clean_response ⇒ Object
Returns the value of attribute clean_response.
44 45 46 |
# File 'lib/cheddar_getter/response.rb', line 44 def clean_response @clean_response end |
#raw_response ⇒ Object
Returns the value of attribute raw_response.
44 45 46 |
# File 'lib/cheddar_getter/response.rb', line 44 def raw_response @raw_response end |
Instance Method Details
#[](value) ⇒ Object
access the root keys of the response directly, such as :customers, :plans, or :errors
268 269 270 |
# File 'lib/cheddar_getter/response.rb', line 268 def [](value) self.clean_response[value] end |
#customer(code = nil) ⇒ Object
Returns the given customer.
code must be provided if this response contains more than one customer.
100 101 102 |
# File 'lib/cheddar_getter/response.rb', line 100 def customer(code = nil) retrieve_item(self, :customers, code) end |
#customer_active?(code = nil) ⇒ Boolean
Get an array representation of a single customer’s current subscription is not provided and the response contains more than one customer
247 248 249 250 251 252 253 254 |
# File 'lib/cheddar_getter/response.rb', line 247 def customer_active?(code = nil) subscription = customer_subscription(code) if subscription[:canceledDatetime] && subscription[:canceledDatetime] <= Time.now false else true end end |
#customer_canceled?(code = nil) ⇒ Boolean
true if the customer is canceled
code must be provided if this reponse contains more than one customer
238 239 240 241 |
# File 'lib/cheddar_getter/response.rb', line 238 def customer_canceled?(code = nil) sub = customer_subscription(code) sub ? !!sub[:canceledDatetime] : nil end |
#customer_invoice(code = nil) ⇒ Object
Returns the current open invoice for the given customer.
code must be provided if this response contains more than one customer.
134 135 136 137 |
# File 'lib/cheddar_getter/response.rb', line 134 def customer_invoice(code = nil) #current invoice is always the first one ((customer_subscription(code) || { })[:invoices] || []).first end |
#customer_invoices(code = nil) ⇒ Object
Returns all of the invoices for the given customer.
code must be provided if this response contains more than one customer.
142 143 144 |
# File 'lib/cheddar_getter/response.rb', line 142 def customer_invoices(code = nil) (customer_subscriptions(code) || []).map{ |s| s[:invoices] || [] }.flatten end |
#customer_item(item_code = nil, code = nil) ⇒ Object
Info about the given item for the given customer.
Merges the plan item info with the subscription item info.
item_code must be provided if the customer is on a plan with more than one item.
code must be provided if this response contains more than one customer.
193 194 195 196 197 198 199 200 |
# File 'lib/cheddar_getter/response.rb', line 193 def customer_item(item_code = nil, code = nil) sub_item = retrieve_item(customer_subscription(code), :items, item_code) plan_item = retrieve_item(customer_plan(code), :items, item_code) return nil unless sub_item && plan_item item = plan_item.dup item[:quantity] = sub_item[:quantity] item end |
#customer_item_quantity_overage(item_code = nil, code = nil) ⇒ Object
The overage amount for the given item for the given customer. 0 if they are still under their limit.
item_code must be provided if the customer is on a plan with more than one item.
code must be provided if this response contains more than one customer.
217 218 219 220 221 |
# File 'lib/cheddar_getter/response.rb', line 217 def customer_item_quantity_overage(item_code = nil, code = nil) over = -customer_item_quantity_remaining(item_code, code) over = 0 if over <= 0 over end |
#customer_item_quantity_overage_cost(item_code = nil, code = nil) ⇒ Object
The current overage cost for the given item for the given customer.
item_code must be provided if the customer is on a plan with more than one item.
code must be provided if this response contains more than one customer.
228 229 230 231 232 233 |
# File 'lib/cheddar_getter/response.rb', line 228 def customer_item_quantity_overage_cost(item_code = nil, code = nil) item = customer_item(item_code, code) return 0 unless item overage = customer_item_quantity_overage(item_code, code) item[:overageAmount] * overage end |
#customer_item_quantity_remaining(item_code = nil, code = nil) ⇒ Object
The amount remaining for a given item for a given customer.
item_code must be provided if the customer is on a plan with more than one item.
code must be provided if this response contains more than one customer.
207 208 209 210 |
# File 'lib/cheddar_getter/response.rb', line 207 def customer_item_quantity_remaining(item_code = nil, code = nil) item = customer_item(item_code, code) item ? item[:quantityIncluded] - item[:quantity] : 0 end |
#customer_last_billed_invoice(code = nil) ⇒ Object
Returns the last billed invoice for the given customer. This is not the same as the currect active invoice.
nil if there is no last billed invoice.
code must be provided if this response contains more than one customer.
152 153 154 155 |
# File 'lib/cheddar_getter/response.rb', line 152 def customer_last_billed_invoice(code = nil) #last billed invoice is always the second one (customer_invoices(code) || [])[1] end |
#customer_last_transaction(code = nil) ⇒ Object
Returns the last transaction for the given customer.
nil if there are no transactions.
code must be provided if this response contains more than one customer.
172 173 174 175 |
# File 'lib/cheddar_getter/response.rb', line 172 def customer_last_transaction(code = nil) invoice = customer_last_billed_invoice(code) || { } (invoice[:transactions] || []).first end |
#customer_one_time_invoices(code = nil) ⇒ Object
164 165 166 |
# File 'lib/cheddar_getter/response.rb', line 164 def customer_one_time_invoices(code = nil) customer_invoices(code).select{ |s| s[:type] == 'one-time' } end |
#customer_outstanding_invoices(code = nil) ⇒ Object
Returns an array of any currently outstanding invoices for the given customer.
code must be provided if this response contains more than one customer.
180 181 182 183 184 185 |
# File 'lib/cheddar_getter/response.rb', line 180 def customer_outstanding_invoices(code = nil) now = Time.now customer_invoices(code).reject do |i| i[:paidTransactionId] || i[:billingDatetime] > now end end |
#customer_paypal_preapproval_url(code = nil) ⇒ Object
112 113 114 |
# File 'lib/cheddar_getter/response.rb', line 112 def customer_paypal_preapproval_url(code = nil) customer_subscription[:redirectUrl] || "" end |
#customer_plan(code = nil) ⇒ Object
Returns the current plan for the given customer
code must be provided if this response contains more than one customer.
127 128 129 |
# File 'lib/cheddar_getter/response.rb', line 127 def customer_plan(code = nil) (customer_subscription(code) || { })[:plan] end |
#customer_subscription(code = nil) ⇒ Object
Returns the current subscription for the given customer.
code must be provided if this response contains more than one customer.
107 108 109 110 |
# File 'lib/cheddar_getter/response.rb', line 107 def customer_subscription(code = nil) #current subscription is always the first one (customer_subscriptions(code) || []).first end |
#customer_subscriptions(code = nil) ⇒ Object
Returns all the subscriptions for the given customer.
Only the first one is active, the rest is historical.
code must be provided if this response contains more than one customer.
120 121 122 |
# File 'lib/cheddar_getter/response.rb', line 120 def customer_subscriptions(code = nil) (customer(code) || { })[:subscriptions] end |
#customer_transactions(code = nil) ⇒ Object
Returns all of the transactions for the given customer.
code must be provided if this response contains more than one customer.
160 161 162 |
# File 'lib/cheddar_getter/response.rb', line 160 def customer_transactions(code = nil) customer_invoices(code).map{ |s| s[:transactions] || [] }.flatten end |
#customer_waiting_for_paypal?(code = nil) ⇒ Boolean
Is this customer’s account pending paypal preapproval confirmation?
257 258 259 260 261 262 263 264 |
# File 'lib/cheddar_getter/response.rb', line 257 def customer_waiting_for_paypal?(code = nil) subscription = customer_subscription(code) if subscription[:canceledDatetime] && subscription[:canceledDatetime] <= Time.now && subscription[:cancelType] == 'paypal-wait' true else false end end |
#error_messages ⇒ Object
the error messages (if there were any) if the CheddarGetter response was invalid
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cheddar_getter/response.rb', line 63 def self.errors.map do |e| msg = nil if e msg = e[:text] msg += ": #{e[:fieldName]}" unless e[:fieldName].blank? end msg end end |
#plan(code = nil) ⇒ Object
Returns the given plan.
code must be provided if this response contains more than one plan.
77 78 79 |
# File 'lib/cheddar_getter/response.rb', line 77 def plan(code = nil) retrieve_item(self, :plans, code) end |
#plan_item(item_code = nil, code = nil) ⇒ Object
Returns the given item for the given plan.
item_code must be provided if the plan has more than one item.
code must be provided if this response contains more than one plan.
93 94 95 |
# File 'lib/cheddar_getter/response.rb', line 93 def plan_item(item_code = nil, code = nil) retrieve_item(plan(code), :items, item_code) end |
#plan_items(code = nil) ⇒ Object
Returns the items for the given plan.
code must be provided if this response contains more than one plan.
84 85 86 |
# File 'lib/cheddar_getter/response.rb', line 84 def plan_items(code = nil) (plan(code) || { })[:items] end |
#valid? ⇒ Boolean
true if the response from CheddarGetter was valid
58 59 60 |
# File 'lib/cheddar_getter/response.rb', line 58 def valid? self.errors.size == 0 && self.raw_response.code < 400 end |