lib/mousetrap/resource.rb in hashrocket-mousetrap-0.2.1 vs lib/mousetrap/resource.rb in hashrocket-mousetrap-0.3.0
- old
+ new
@@ -13,29 +13,28 @@
def self.[](code)
response = get_resource plural_resource_name, code
build_resource_from response
end
- def self.all
- response = get_resources plural_resource_name
- build_resources_from response
- end
-
def self.create(attributes = {})
raise NotImplementedError, NO_BUSINESS_NEED
end
def self.delete(code)
raise NotImplementedError, NO_BUSINESS_NEED
end
+ def self.destroy_all
+ all.each { |object| object.destroy }
+ end
+
def self.exists?(code)
raise NotImplementedError, NO_BUSINESS_NEED
end
def destroy
- raise NotImplementedError, NO_BUSINESS_NEED
+ member_action 'delete' unless new_record?
end
def exists?(code)
raise NotImplementedError, NO_BUSINESS_NEED
end
@@ -55,31 +54,47 @@
end
protected
+ def member_action(action)
+ self.class.member_action(self.class.plural_resource_name, action, code)
+ end
+
+ def self.resource_path(resource, action, code = nil)
+ path = "/xml/#{resource}/#{action}/productCode/#{Mousetrap.product_code}"
+ path += "/code/#{code}" if code
+ path
+ end
+
+ def self.member_action(resource, action, code, attributes = nil)
+ path = resource_path(resource, action, code)
+
+ if attributes
+ post path, :body => attributes
+ else
+ post path
+ end
+ end
+
def self.delete_resource(resource, code)
- path = "/xml/#{resource}/delete/productCode/#{Mousetrap.product_code}/code/#{code}"
- post path
+ member_action(resource, 'delete', code)
end
+ def self.put_resource(resource, action, code, attributes)
+ member_action(resource, action, code, attributes)
+ end
+
def self.get_resource(resource, code)
- path = "/xml/#{resource}/get/productCode/#{Mousetrap.product_code}/code/#{code}"
- get path
+ get resource_path(resource, 'get', code)
end
def self.get_resources(resource)
- path = "/xml/#{resource}/get/productCode/#{Mousetrap.product_code}"
- get path
+ get resource_path(resource, 'get')
end
def self.post_resource(resource, action, attributes)
- path = "/xml/#{resource}/#{action}/productCode/#{Mousetrap.product_code}"
- post path, :body => attributes
- end
-
- def self.put_resource(resource, action, resource_code, attributes)
- path = "/xml/#{resource}/#{action}/productCode/#{Mousetrap.product_code}/code/#{resource_code}"
+ path = resource_path(resource, action)
post path, :body => attributes
end
def self.plural_resource_name
raise 'You must implement self.plural_resource_name in your subclass.'