lib/rest/api/client.rb in rest-api-client-0.1.0 vs lib/rest/api/client.rb in rest-api-client-0.1.1
- old
+ new
@@ -1,10 +1,13 @@
require 'virtus'
require 'rest/api/client/version'
+require 'rest/api/client/logger'
require 'rest/api/client/json_parser'
require 'rest/api/client/config'
require 'rest/api/exceptions/service_url_exception'
+require 'rest/api/exceptions/unauthorized_exception'
+require 'rest/api/exceptions/model_errors_exception'
require 'rest/api/client/request_handler'
module RestApiClient
class RestModel
@@ -28,38 +31,48 @@
def self.get(id)
self.find id
end
def save!
- perform_post path, {:type => self, :params => self.attributes}
+ klazz = self.class
+ response = perform_post path, {:type => klazz, :params => {get_model_name => self.attributes}}
+ self.attributes = response && response.is_a?(klazz) ? response.attributes : {}
end
def delete
perform_delete "#{path}/#{id}"
end
def update!
- perform_put "#{path}/#{id}", {:type => self, :params => self.attributes}
+ perform_put "#{path}/#{id}", {:type => self.class, :params => self.attributes}
end
- def perform_get(path, args = {})
- RequestsHandler.perform_get(service_key, path, args)
+ def perform_get(path, args = {}, headers = {})
+ RequestsHandler.perform_get(service_key, path, args, headers)
end
- def perform_post(path, args = {})
- RequestsHandler.perform_post(service_key, path, args)
+ def perform_post(path, args = {}, headers = {})
+ RequestsHandler.perform_post(service_key, path, args, headers)
end
- def perform_put(path, args = {})
- RequestsHandler.perform_put(service_key, path, args)
+ def perform_put(path, args = {}, headers = {})
+ RequestsHandler.perform_put(service_key, path, args, headers)
end
- def perform_delete(path, args = {})
- RequestsHandler.perform_delete(service_key, path, args)
+ def perform_delete(path, args = {}, headers = {})
+ RequestsHandler.perform_delete(service_key, path, args, headers)
end
- def self.perform_get(path, args = {})
- RequestsHandler.perform_get(service_key, path, args)
+ def self.perform_get(path, args = {}, headers = {})
+ RequestsHandler.perform_get(service_key, path, args, headers)
+ end
+
+ def get_model_name
+ self.class.name.split('::').last.downcase
+ end
+
+ def self.get_model_name
+ self.name.split('::').last.downcase
end
# default service_key to instance methods
def service_key
SERVICE_KEY