diff --git a/lib/parse/object.rb b/lib/parse/object.rb index bec4800..ead94ed 100644 --- a/lib/parse/object.rb +++ b/lib/parse/object.rb @@ -22,6 +22,10 @@ def initialize(class_name, data = nil) end end + def uri + Protocol.class_uri @class_name, @parse_object_id + end + # Merge a hash parsed from the JSON representation into # this instance. This will extract the reserved fields, # merge the hash keys, and then insure that the reserved @@ -52,11 +56,10 @@ def parse(data) private :parse def parse_save - uri = Protocol.class_uri @class_name, @parse_object_id method = @parse_object_id ? :put : :post body = self.to_json - data = Parse.client.request(uri, method, body) + data = Parse.client.request(self.uri, method, body) if data parse data end @@ -65,7 +68,7 @@ def parse_save def parse_refresh if @parse_object_id - data = Parse.client.get(Protocol.class_uri(@class_name, @parse_object_id)) + data = Parse.client.get self.uri if data parse data end @@ -75,10 +78,7 @@ def parse_refresh def parse_delete if @parse_object_id - uri = Protocol.class_uri @class_name, @parse_object_id - - response = Parse.client.request(:delete, uri, {}) - response + response = Parse.client.request(:delete, self.uri, {}) end nil end @@ -95,7 +95,7 @@ def parse_increment(field, amount = 1) op = amount > 0 ? Protocol::OP_INCREMENT : Protocol::OP_DECREMENT body = "{\"#{field}\": {\"__op\": \"#{op}\", \"amount\" : #{amount.abs}}}" puts body - data = Parse.client.request( Protocol.class_uri(@class_name, @parse_object_id), :put, body) + data = Parse.client.request( self.uri, :put, body) parse data end self diff --git a/lib/parse/protocol.rb b/lib/parse/protocol.rb index d9a6c4b..96699eb 100644 --- a/lib/parse/protocol.rb +++ b/lib/parse/protocol.rb @@ -39,7 +39,7 @@ module Protocol # Construct a uri referencing a given Parse object # class or instance (of object_id is non-nil). - def self.class_uri(class_name, object_id = nil) + def Protocol.class_uri(class_name, object_id = nil) if object_id "/#{VERSION}/classes/#{class_name}/#{object_id}" else @@ -47,4 +47,14 @@ def self.class_uri(class_name, object_id = nil) end end + # Construct a uri referencing a give Parse user + # instance or the users category. + def Protocol.user_uri(user_id = nil) + if user_id + "/#{VERSION}/users/#{user_id}" + else + "/#{VERSION}/users" + end + end + end