lib/rest/api/client.rb in rest-api-client-0.1.2 vs lib/rest/api/client.rb in rest-api-client-0.1.3

- old
+ new

@@ -14,37 +14,55 @@ include Virtus.model PATH = '' SERVICE_KEY = '' + attr_reader :errors + attribute :id, Integer - attribute :created_at, Date - attribute :updated_at, Date + attribute :created_at, DateTime + attribute :updated_at, DateTime - def self.list - perform_get path, {:type => self} + def self.list(params = {}) + perform_get path, {:type => self, :params => params} end + def self.count(params = {}) + perform_get "#{path}/count", {:params => params} + end + def self.find(id) perform_get "#{path}/#{id}", {:type => self} end def self.get(id) self.find id end def save! - klazz = self.class - response = perform_post path, {:type => klazz, :params => {get_model_name => self.attributes}} - self.attributes = response && response.is_a?(klazz) ? response.attributes : {} + begin + klazz = self.class + response = perform_post path, {:type => klazz, :params => {get_model_name => self.attributes}} + self.attributes = response && response.is_a?(klazz) ? response.attributes : {} + rescue RestApiClient::ModelErrorsException => e + @errors = e.errors + return false + end end def delete perform_delete "#{path}/#{id}" end def update! - perform_put "#{path}/#{id}", {:type => self.class, :params => self.attributes} + begin + klazz = self.class + response = perform_put "#{path}/#{id}", {:type => klazz, :params => {get_model_name => self.attributes}} + self.attributes = response && response.is_a?(klazz) ? response.attributes : {} + rescue RestApiClient::ModelErrorsException => e + @errors = e.errors + return false + end end def perform_get(path, args = {}, headers = {}) RequestsHandler.perform_get(service_key, path, args, headers) end