Class: Mousetrap::Customer
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Resource
[], #destroy, destroy_all, exists?, #exists?, #initialize
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
3
4
5
|
# File 'lib/mousetrap/customer.rb', line 3
def code
@code
end
|
#company ⇒ Object
Returns the value of attribute company.
3
4
5
|
# File 'lib/mousetrap/customer.rb', line 3
def company
@company
end
|
#email ⇒ Object
Returns the value of attribute email.
3
4
5
|
# File 'lib/mousetrap/customer.rb', line 3
def email
@email
end
|
#first_name ⇒ Object
Returns the value of attribute first_name.
3
4
5
|
# File 'lib/mousetrap/customer.rb', line 3
def first_name
@first_name
end
|
#id ⇒ Object
Returns the value of attribute id.
3
4
5
|
# File 'lib/mousetrap/customer.rb', line 3
def id
@id
end
|
#last_name ⇒ Object
Returns the value of attribute last_name.
3
4
5
|
# File 'lib/mousetrap/customer.rb', line 3
def last_name
@last_name
end
|
#subscription ⇒ Object
Returns the value of attribute subscription.
3
4
5
|
# File 'lib/mousetrap/customer.rb', line 3
def subscription
@subscription
end
|
Class Method Details
.all ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/mousetrap/customer.rb', line 104
def self.all
response = get_resources 'customers'
if response['error']
if response['error'] =~ /No customers found/
return []
else
raise response['error']
end
end
build_resources_from response
end
|
.create(attributes) ⇒ Object
118
119
120
121
122
|
# File 'lib/mousetrap/customer.rb', line 118
def self.create(attributes)
object = new(attributes)
object.send(:create)
object
end
|
.new_from_api(attributes) ⇒ Object
124
125
126
127
128
129
|
# File 'lib/mousetrap/customer.rb', line 124
def self.new_from_api(attributes)
customer = new(attributes_from_api(attributes))
subscription_attrs = attributes['subscriptions']['subscription']
customer.subscription = Subscription.new_from_api(subscription_attrs.kind_of?(Array) ? subscription_attrs.first : subscription_attrs)
customer
end
|
.update(customer_code, attributes) ⇒ Object
131
132
133
134
135
|
# File 'lib/mousetrap/customer.rb', line 131
def self.update(customer_code, attributes)
customer = new(attributes)
customer.code = customer_code
customer.send :update
end
|
Instance Method Details
#add_custom_charge(item_code, amount = 1.0, quantity = 1, description = nil) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/mousetrap/customer.rb', line 37
def add_custom_charge(item_code, amount = 1.0, quantity = 1, description = nil)
attributes = {
:chargeCode => item_code,
:eachAmount => amount,
:quantity => quantity,
:description => description
}
response = self.class.put_resource 'customers', 'add-charge', code, attributes
raise response['error'] if response['error']
response
end
|
#add_item_quantity(item_code, quantity = 1) ⇒ Object
29
30
31
|
# File 'lib/mousetrap/customer.rb', line 29
def add_item_quantity(item_code, quantity = 1)
update_tracked_item_quantity(item_code, quantity)
end
|
#attributes ⇒ Object
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/mousetrap/customer.rb', line 54
def attributes
{
:id => id,
:code => code,
:email => email,
:first_name => first_name,
:last_name => last_name,
:company => company
}
end
|
#attributes_for_api ⇒ Object
65
66
67
68
|
# File 'lib/mousetrap/customer.rb', line 65
def attributes_for_api
self.class.attributes_for_api(attributes, new_record?)
end
|
#attributes_for_api_with_subscription ⇒ Object
70
71
72
73
74
75
|
# File 'lib/mousetrap/customer.rb', line 70
def attributes_for_api_with_subscription
raise "Must have subscription" unless subscription
a = attributes_for_api
a[:subscription] = subscription.attributes_for_api
a
end
|
#cancel ⇒ Object
77
78
79
|
# File 'lib/mousetrap/customer.rb', line 77
def cancel
member_action 'cancel' unless new_record?
end
|
#new? ⇒ Boolean
81
82
83
84
85
86
87
88
89
|
# File 'lib/mousetrap/customer.rb', line 81
def new?
if api_customer = self.class[code]
self.id = api_customer.id
return false
else
return true
end
end
|
#remove_item_quantity(item, quantity = 1) ⇒ Object
33
34
35
|
# File 'lib/mousetrap/customer.rb', line 33
def remove_item_quantity(item, quantity = 1)
update_tracked_item_quantity(item_code, -quantity)
end
|
#save ⇒ Object
91
92
93
|
# File 'lib/mousetrap/customer.rb', line 91
def save
new? ? create : update
end
|
#subscription_attributes=(attributes) ⇒ Object
50
51
52
|
# File 'lib/mousetrap/customer.rb', line 50
def subscription_attributes=(attributes)
self.subscription = Subscription.new attributes
end
|
#switch_to_plan(plan_code) ⇒ Object
95
96
97
98
99
100
101
102
|
# File 'lib/mousetrap/customer.rb', line 95
def switch_to_plan(plan_code)
raise "Can only call this on an existing CheddarGetter customer." unless exists?
attributes = { :planCode => plan_code }
self.class.put_resource('customers', 'edit-subscription', code, attributes)
end
|
#update_tracked_item_quantity(item_code, quantity = 1) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/mousetrap/customer.rb', line 12
def update_tracked_item_quantity(item_code, quantity = 1)
tracked_item_resource = if quantity == quantity.abs
'add-item-quantity'
else
'remove-item-quantity'
end
attributes = {
:itemCode => item_code,
:quantity => quantity.abs
}
response = self.class.put_resource 'customers', tracked_item_resource, code, attributes
raise response['error'] if response['error']
response
end
|