Skip to content

Commit

Permalink
Move URI generation into a uri() method on Object, so it can be overr…
Browse files Browse the repository at this point in the history
…idden in User later
  • Loading branch information
aalpern committed Jan 25, 2012
1 parent 5d5409a commit 6bc3b17
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
16 changes: 8 additions & 8 deletions lib/parse/object.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion lib/parse/protocol.rb
Expand Up @@ -39,12 +39,22 @@ 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
"/#{VERSION}/classes/#{class_name}"
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

0 comments on commit 6bc3b17

Please sign in to comment.