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



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

#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



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

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

#attributes_for_api_with_subscriptionObject



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

#cancelObject



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

def cancel
  member_action 'cancel' unless new_record?
end

#new?Boolean

Returns:

  • (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

#saveObject



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)

  # TODO: Refresh self with reload here?
end