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
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/mousetrap/customer.rb', line 66
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
80
81
82
83
84
|
# File 'lib/mousetrap/customer.rb', line 80
def self.create(attributes)
object = new(attributes)
object.send(:create)
object
end
|
.new_from_api(attributes) ⇒ Object
86
87
88
89
90
91
|
# File 'lib/mousetrap/customer.rb', line 86
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
93
94
95
96
97
|
# File 'lib/mousetrap/customer.rb', line 93
def self.update(customer_code, attributes)
customer = new(attributes)
customer.code = customer_code
customer.send :update
end
|
Instance Method Details
#attributes ⇒ Object
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/mousetrap/customer.rb', line 16
def attributes
{
:id => id,
:code => code,
:email => email,
:first_name => first_name,
:last_name => last_name,
:company => company
}
end
|
#attributes_for_api ⇒ Object
27
28
29
30
|
# File 'lib/mousetrap/customer.rb', line 27
def attributes_for_api
self.class.attributes_for_api(attributes, new_record?)
end
|
#attributes_for_api_with_subscription ⇒ Object
32
33
34
35
36
37
|
# File 'lib/mousetrap/customer.rb', line 32
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
39
40
41
|
# File 'lib/mousetrap/customer.rb', line 39
def cancel
member_action 'cancel' unless new_record?
end
|
#new? ⇒ Boolean
43
44
45
46
47
48
49
50
51
|
# File 'lib/mousetrap/customer.rb', line 43
def new?
if api_customer = self.class[code]
self.id = api_customer.id
return false
else
return true
end
end
|
#save ⇒ Object
53
54
55
|
# File 'lib/mousetrap/customer.rb', line 53
def save
new? ? create : update
end
|
#subscription_attributes=(attributes) ⇒ Object
12
13
14
|
# File 'lib/mousetrap/customer.rb', line 12
def subscription_attributes=(attributes)
self.subscription = Subscription.new attributes
end
|
#switch_to_plan(plan_code) ⇒ Object
57
58
59
60
61
62
63
64
|
# File 'lib/mousetrap/customer.rb', line 57
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
|