lib/prx/model/base.rb in prx_client-0.1.3 vs lib/prx/model/base.rb in prx_client-0.2.0

- old
+ new

@@ -1,19 +1,42 @@ -require 'roar/representer' -require 'roar/representer/json' - module PRX module Model class Base - include Roar::Representer::JSON + attr_accessor :id def initialize(*args) return unless args if args[0].is_a?(Hash) args[0].each{|k,v| self.send("#{k.to_s}=", v)} end end - + + def find_by_id(id) + request(:path => "#{class_path_part}/#{id}") + end + + def save + opts = { + :action => (id ? :put : :post), + :path => (id ? "#{class_path_part}/#{id}" : class_path_part), + :body => as_json + } + response = request(opts) + self.from_json(response.response.body) + end + + def class_path_part + self.class.name.demodulize.underscore.pluralize + end + + def request(opts) + self.class.request(opts) + end + + def self.request(opts) + PRX::Client.request(opts) + end + end end end