lib/mousetrap/customer.rb in cameroncox-mousetrap-0.5.3.10 vs lib/mousetrap/customer.rb in cameroncox-mousetrap-0.6.1
- old
+ new
@@ -5,12 +5,53 @@
:code,
:email,
:first_name,
:last_name,
:company,
- :subscription
+ :subscription,
+ :charges,
+ :items
+ 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
+
+ def add_item_quantity(item_code, quantity = 1)
+ update_tracked_item_quantity(item_code, quantity)
+ end
+
+ def remove_item_quantity(item_code, quantity = 1)
+ update_tracked_item_quantity(item_code, -quantity)
+ end
+
+ 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
+
def subscription_attributes=(attributes)
self.subscription = Subscription.new attributes
end
def attributes
@@ -18,24 +59,16 @@
:id => id,
:code => code,
:email => email,
:first_name => first_name,
:last_name => last_name,
- :company => company
+ :company => company,
+ :charges => charges,
+ :items => items
}
end
- 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
-
- 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
-
def attributes_for_api
# TODO: superclass?
self.class.attributes_for_api(attributes, new_record?)
end
@@ -122,9 +155,12 @@
:email => attributes[:email],
:firstName => attributes[:first_name],
:lastName => attributes[:last_name],
:company => attributes[:company]
}
+
+ mutated_hash.merge!(:charges => attributes[:charges]) if attributes[:charges]
+ mutated_hash.merge!(:items => attributes[:items]) if attributes[:items]
mutated_hash.merge!(:code => attributes[:code]) if new_record
mutated_hash
end
def self.attributes_from_api(attributes)