Skip to content

Commit

Permalink
Cleaned up client & object methods to centralize HTTP calls and respo…
Browse files Browse the repository at this point in the history
…nse handling.
  • Loading branch information
aalpern committed Jan 25, 2012
1 parent c0e32a2 commit 76601d6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 36 deletions.
54 changes: 35 additions & 19 deletions lib/parse/client.rb
Expand Up @@ -26,15 +26,29 @@ def initialize(data = {})
@session.headers[Protocol::HEADER_APP_ID] = @application_id
end

def get(uri)
response = @session.get(uri)
# Perform an HTTP request for the given uri and method
# with common basic response handling. Will raise a
# ParseProtocolError if the response has an error status code,
# and will return the parsed JSON body on success, if there is one.
def request(uri, method = :get, body = nil)
options = {}
if body
options[:data] = body
end

response = @session.request(method, uri, {}, options)
if response.status >= 400
raise ParseProtocolError, response
else
if response.body
JSON.parse response.body
end
end
parse_response response
end

def parse_response(response)
# Interpret a parsed JSON object, instantiating new instances
# of Parse::Object as appropriate.
def parse_response(data)
if response.body
data = JSON.parse response.body
if data.size == 1 && data[Protocol::RESPONSE_KEY_RESULTS]
Expand All @@ -44,18 +58,28 @@ def parse_response(response)
end
end
end
private :parse_response

end

# A singleton client for use by methods in Object
def get(uri)
request(uri)
end

# Module methods
# ------------------------------------------------------------

# A singleton client for use by methods in Object.
# Always use Parse.client to retrieve the client object.
@@client = nil

def self.init(data)
# Initialize the singleton instance of Client which is used
# by all API methods. Parse.init must be called before saving
# or retrieving any objects.
def Parse.init(data)
@@client = Client.new(data)
end

def self.client
def Parse.client
if !@@client
raise ParseError, "API not initialized"
end
Expand All @@ -64,17 +88,9 @@ def self.client

# Perform a simple retrieval of a simple object, or all objects of a
# given class.
def self.get(class_name, object_id = nil)
uri = Protocol.class_uri(class_name, object_id)
response = self.client.session.get(uri)
if response.status == 200
data = JSON.parse response.body
if data.size == 1 && data["results"].is_a?( Array )
data["results"].collect { |hash| Parse::Object.new class_name, hash }
else
Parse::Object.new class_name, data
end
end
def Parse.get(class_name, object_id = nil)
data = Parse.client.get( Protocol.class_uri(class_name, object_id) )
Parse.client.parse_response data
end

end
Expand Down
39 changes: 22 additions & 17 deletions lib/parse/object.rb
Expand Up @@ -22,18 +22,25 @@ def initialize(class_name, data = nil)
end
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
# fields do not occur in the underlying hash storage.
def parse(data)
@parse_object_id = data[Protocol::KEY_OBJECT_ID]
@created_at = data[Protocol::KEY_CREATED_AT]
if @created_at
@created_at = DateTime.parse @created_at
end

@updated_at = data[Protocol::KEY_UPDATED_AT]
if @updated_at
@updated_at = DateTime.parse @updated_at
end

self.merge! data
# Remove the reserved keywords, so they won't be serialized

# Remove the reserved keys, so they won't be serialized
# on save'
self.delete Protocol::KEY_CREATED_AT
self.delete Protocol::KEY_OBJECT_ID
Expand All @@ -42,37 +49,35 @@ 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
response = Parse.client.session.request(method, uri, {}, :data => body)
if response.status >= 200 && response.status <= 300
if response.body
data = JSON.parse response.body
parse data
end
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)
if data
parse data
end
response
self
end

def parse_refresh
if @parse_object_id
uri = Protocol.class_uri @class_name, @parse_object_id
response = Parse.client.session.request(:get, uri, {})
if response.status == 200
data = JSON.parse response.body
data = Protocol.client.get(@class_name, @parse_object_id)
if data
parse data
end
response
self
end
end

def parse_delete
if @parse_object_id
uri = Protocol.class_uri @class_name, @parse_object_id
uri = Protocol.class_uri @class_name, @parse_object_id

response = parse.client.session.request(:delete, uri, {})
response
end
nil
end
end

Expand Down

0 comments on commit 76601d6

Please sign in to comment.