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

#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

#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

#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



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mousetrap/customer.rb', line 76

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



90
91
92
93
94
# File 'lib/mousetrap/customer.rb', line 90

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

.new_from_api(attributes) ⇒ Object



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

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



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

def self.update(customer_code, attributes)
  customer = new(attributes)
  customer.code = customer_code
  customer.send :update
end

Instance Method Details

#add_item_quantity(item_code, quantity = 1) ⇒ Object



27
28
29
30
# File 'lib/mousetrap/customer.rb', line 27

def add_item_quantity(item_code, quantity = 1)
  attrs = { :itemCode => item_code, :quantity => quantity }
  self.class.put_resource 'customers', 'add-item-quantity', code, attrs
end

#attributesObject



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_apiObject



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

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

#attributes_for_api_with_subscriptionObject



42
43
44
45
46
47
# File 'lib/mousetrap/customer.rb', line 42

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

#cancelObject



49
50
51
# File 'lib/mousetrap/customer.rb', line 49

def cancel
  member_action 'cancel' unless new_record?
end

#new?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
# File 'lib/mousetrap/customer.rb', line 53

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



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

def remove_item_quantity(item_code, quantity = 1)
  attrs = { :itemCode => item_code, :quantity => quantity }
  self.class.put_resource 'customers', 'remove-item-quantity', code, attrs
end

#saveObject



63
64
65
# File 'lib/mousetrap/customer.rb', line 63

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



67
68
69
70
71
72
73
74
# File 'lib/mousetrap/customer.rb', line 67

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