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

- old
+ new

@@ -38,34 +38,52 @@ self.find id end def save! 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 : {} + update_attributes(perform_post path, get_params) + self rescue RestApiClient::ModelErrorsException => e @errors = e.errors + raise e + end + end + + def save + begin + update_attributes(perform_post path, get_params) + self + rescue RestApiClient::ModelErrorsException => e + @errors = e.errors return false end end def delete perform_delete "#{path}/#{id}" end - def update! + def update 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 : {} + update_attributes(perform_put "#{path}/#{id}", get_params) + self rescue RestApiClient::ModelErrorsException => e @errors = e.errors return false end end + def update! + begin + update_attributes(perform_put "#{path}/#{id}", get_params) + self + rescue RestApiClient::ModelErrorsException => e + @errors = e.errors + raise e + end + end + def perform_get(path, args = {}, headers = {}) RequestsHandler.perform_get(service_key, path, args, headers) end def perform_post(path, args = {}, headers = {}) @@ -112,9 +130,18 @@ PATH end def ==(other) id == other.id + end + + private + def get_params + {:type => self.class, :params => {get_model_name => self.attributes}} + end + + def update_attributes(response) + self.attributes = response && response.is_a?(self.class) ? response.attributes : {} end end end