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

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

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

#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



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

def self.all
  response = get_resources plural_resource_name

  if response['error']
    if response['error'] == "Bad request: No customers found."
      return []
    else
      raise response['error']
    end
  end

  build_resources_from response
end

.create(attributes) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/mousetrap/customer.rb', line 57

def self.create(attributes)
  object = new(attributes)
  response = object.save
  returned_customer = build_resource_from response
  object.id = returned_customer.id
  # TODO: what other attrs to copy over?
  object
end

.new_from_api(attributes) ⇒ Object



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

def self.new_from_api(attributes)
  customer = new(attributes_from_api(attributes))
  customer.subscription = Subscription.new_from_api(
    attributes['subscriptions']['subscription'])
  customer
end

Instance Method Details

#attributesObject



15
16
17
18
19
20
21
22
23
# File 'lib/mousetrap/customer.rb', line 15

def attributes
  {
    :id         => id,
    :code       => code,
    :email      => email,
    :first_name => first_name,
    :last_name  => last_name,
  }
end

#attributes_for_apiObject



25
26
27
28
29
30
31
32
33
# File 'lib/mousetrap/customer.rb', line 25

def attributes_for_api
  a = self.class.attributes_for_api(attributes, new_record?)

  if subscription
    a[:subscription] = subscription.attributes_for_api
  end

  a
end

#cancelObject



35
36
37
# File 'lib/mousetrap/customer.rb', line 35

def cancel
  member_action 'cancel' unless new_record?
end

#saveObject



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

def save
  new? ? create : update
end

#subscription_attributes=(attributes) ⇒ Object



11
12
13
# File 'lib/mousetrap/customer.rb', line 11

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