lib/grendel/user.rb in grendel-ruby-0.1.1 vs lib/grendel/user.rb in grendel-ruby-0.1.2

- old
+ new

@@ -1,20 +1,20 @@ module Grendel class User attr_accessor :id, :password, :uri attr_reader :client, :modified_at, :created_at, :keys - + # create a new Grendel::User object # params: # id # uri # password def initialize(client, params) params.symbolize_keys! @client = client @id = params[:id] - @uri = params[:uri] ? + @uri = params[:uri] ? URI.parse(params[:uri]).path : "/users/" + @id # escape this? @password = params[:password] @modified_at = DateTime.parse(params[:"modified-at"]) if params[:"modified-at"] @created_at = DateTime.parse(params[:"created-at"]) if params[:"created-at"] @@ -29,40 +29,59 @@ # # methods to do authenticated client calls with the user's base_uri # def get(uri = "", options = {}) options.merge!(auth) - @client.get(@uri + uri, options) + @client.get(child_uri(uri), options) end - + + def head(uri = "", options = {}) + options.merge!(auth) + @client.head(child_uri(uri), options) + end + def post(uri = "", data = {}, options = {}) options.merge!(auth) - @client.post(@uri + uri, data, options) + @client.post(child_uri(uri), data, options) end - + def put(uri = "", data = {}, options = {}) options.merge!(auth) - @client.put(@uri + uri, data, options) + @client.put(child_uri(uri), data, options) end def delete(uri = "", options = {}) options.merge!(auth) - @client.delete(@uri + uri, options) + @client.delete(child_uri(uri), options) end - + # change the user's password def change_password(new_password) put("", {:password => new_password}) @password = new_password end - + # send documents calls to the DocumentManager def documents DocumentManager.new(self) end - + # send linked documents calls to the LinkedDocumentManager def linked_documents LinkedDocumentManager.new(self) end + + private + def child_uri(uri) + if uri.nil? || uri.empty? + # blank uri, just use the user's uri + @uri + elsif uri.index(@uri) == 0 + # uri already starts with user's uri, just leave it alone + uri + else + # otherwise just tack the uri onto the end of the user's uri + @uri + uri + end + end end end \ No newline at end of file