Class: Mousetrap::Customer

Inherits:
Resource show all
Defined in:
lib/mousetrap/customer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

[], #destroy, destroy_all, exists?, #exists?, #initialize

Constructor Details

This class inherits a constructor from Mousetrap::Resource

Instance Attribute Details

#chargesObject

Returns the value of attribute charges.



3
4
5
# File 'lib/mousetrap/customer.rb', line 3

def charges
  @charges
end

#codeObject

Returns the value of attribute code.



3
4
5
# File 'lib/mousetrap/customer.rb', line 3

def code
  @code
end

#companyObject

Returns the value of attribute company.



3
4
5
# File 'lib/mousetrap/customer.rb', line 3

def company
  @company
end

#emailObject

Returns the value of attribute email.



3
4
5
# File 'lib/mousetrap/customer.rb', line 3

def email
  @email
end

#first_nameObject

Returns the value of attribute first_name.



3
4
5
# File 'lib/mousetrap/customer.rb', line 3

def first_name
  @first_name
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/mousetrap/customer.rb', line 3

def id
  @id
end

#itemsObject

Returns the value of attribute items.



3
4
5
# File 'lib/mousetrap/customer.rb', line 3

def items
  @items
end

#last_nameObject

Returns the value of attribute last_name.



3
4
5
# File 'lib/mousetrap/customer.rb', line 3

def last_name
  @last_name
end

#notesObject

Returns the value of attribute notes.



3
4
5
# File 'lib/mousetrap/customer.rb', line 3

def notes
  @notes
end

#subscriptionObject

Returns the value of attribute subscription.



3
4
5
# File 'lib/mousetrap/customer.rb', line 3

def subscription
  @subscription
end

Class Method Details

.allObject



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/mousetrap/customer.rb', line 123

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



137
138
139
140
141
# File 'lib/mousetrap/customer.rb', line 137

def self.create(attributes)
  object = new(attributes)
  object.send(:create)
  object
end

.new_from_api(attributes) ⇒ Object



143
144
145
146
147
148
# File 'lib/mousetrap/customer.rb', line 143

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



150
151
152
153
154
# File 'lib/mousetrap/customer.rb', line 150

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



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mousetrap/customer.rb', line 41

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



33
34
35
# File 'lib/mousetrap/customer.rb', line 33

def add_item_quantity(item_code, quantity = 1)
  update_tracked_item_quantity(item_code, quantity)
end

#attributesObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mousetrap/customer.rb', line 63

def attributes
  {
    :id         => id,
    :code       => code,
    :email      => email,
    :first_name => first_name,
    :last_name  => last_name,
    :company    => company,
    :notes      => notes,
    :charges    => charges,
    :items      => items
  }
end

#attributes_for_apiObject



77
78
79
80
# File 'lib/mousetrap/customer.rb', line 77

def attributes_for_api
  # TODO: superclass?
  self.class.attributes_for_api(attributes, new_record?)
end

#attributes_for_api_with_subscriptionObject



82
83
84
85
86
87
# File 'lib/mousetrap/customer.rb', line 82

def attributes_for_api_with_subscription
  raise "Must have subscription" unless subscription
  a = attributes_for_api
  a[:subscription] = subscription.attributes_for_api
  a
end

#bill_nowObject



116
117
118
119
120
121
# File 'lib/mousetrap/customer.rb', line 116

def bill_now
  raise "Can only call this on an existing CheddarGetter customer." unless exists?
  
  attributes = { :changeBillDate => 'now' }      
  self.class.put_resource('customers', 'edit-subscription', code, attributes)
end

#cancelObject



89
90
91
# File 'lib/mousetrap/customer.rb', line 89

def cancel
  member_action 'cancel' unless new_record?
end

#instant_bill_custom_charge(item_code, amount = 1.0, quantity = 1, description = nil) ⇒ Object



54
55
56
57
# File 'lib/mousetrap/customer.rb', line 54

def instant_bill_custom_charge(item_code, amount = 1.0, quantity = 1, description = nil)
  add_custom_charge(item_code, amount, quantity, description)
  bill_now
end

#new?Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
# File 'lib/mousetrap/customer.rb', line 93

def new?
  if api_customer = self.class[code]
    self.id = api_customer.id

    return false
  else
    return true
  end
end

#remove_item_quantity(item_code, quantity = 1) ⇒ Object



37
38
39
# File 'lib/mousetrap/customer.rb', line 37

def remove_item_quantity(item_code, quantity = 1)
  update_tracked_item_quantity(item_code, -quantity)
end

#saveObject



103
104
105
# File 'lib/mousetrap/customer.rb', line 103

def save
  new? ? create : update
end

#subscription_attributes=(attributes) ⇒ Object



59
60
61
# File 'lib/mousetrap/customer.rb', line 59

def subscription_attributes=(attributes)
  self.subscription = Subscription.new attributes
end

#switch_to_plan(plan_code) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/mousetrap/customer.rb', line 107

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)

  # TODO: Refresh self with reload here?
end

#update_tracked_item_quantity(item_code, quantity = 1) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mousetrap/customer.rb', line 15

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