lib/mousetrap/customer.rb in hashrocket-mousetrap-0.2.1 vs lib/mousetrap/customer.rb in hashrocket-mousetrap-0.3.0
- old
+ new
@@ -30,18 +30,32 @@
end
a
end
- def destroy
- self.class.delete_resource('customers', code) unless new_record?
+ def cancel
+ member_action 'cancel' unless new_record?
end
def save
new? ? create : update
end
+ 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
+
def self.create(attributes)
object = new(attributes)
response = object.save
returned_customer = build_resource_from response
object.id = returned_customer.id
@@ -86,10 +100,16 @@
:email => attributes['email']
}
end
def create
- self.class.post_resource 'customers', 'new', attributes_for_api
+ response = self.class.post_resource 'customers', 'new', attributes_for_api
+
+ raise response['error'] if response['error']
+
+ returned_customer = self.class.build_resource_from response
+ self.id = returned_customer.id
+ response
end
def update
self.class.put_resource 'customers', 'edit-customer', code, attributes_for_api
end