Skip to content

Commit

Permalink
Added datatype support
Browse files Browse the repository at this point in the history
- Added new parsing methods in util.rb to recursively translate raw
hashes from JSON.parse() to Parse objects, including the special
datatype representations (Byte, Pointer, Date)
- Parse::Date can be initialized with a string, DateTime, or Hash
  • Loading branch information
aalpern committed Jan 27, 2012
1 parent f1692b5 commit e804895
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
16 changes: 2 additions & 14 deletions lib/parse/client.rb
@@ -1,5 +1,6 @@
require 'parse/protocol'
require 'parse/error'
require 'parse/util'

module Parse

Expand Down Expand Up @@ -49,19 +50,6 @@ def request(uri, method = :get, body = nil, query = nil)
end
end

# Interpret a parsed JSON object, instantiating new instances
# of Parse::Object as appropriate. Does not handle datatypes
# recursively (nor does it need to be an instance method) yet
def parse_response(class_name, data)
if data
if data.size == 1 && data[Protocol::RESPONSE_KEY_RESULTS]
data[Protocol::RESPONSE_KEY_RESULTS].collect { |o| Parse::Object.new class_name, o }
else
Parse::Object.new class_name, data
end
end
end

def get(uri)
request(uri)
end
Expand Down Expand Up @@ -108,7 +96,7 @@ def Parse.client
# given class will be retrieved and returned in an Array.
def Parse.get(class_name, object_id = nil)
data = Parse.client.get( Protocol.class_uri(class_name, object_id) )
Parse.client.parse_response class_name, data
Parse.parse_json class_name, data
end

end
Expand Down
8 changes: 7 additions & 1 deletion lib/parse/datatypes.rb
Expand Up @@ -33,7 +33,13 @@ class Date
attr_accessor :value

def initialize(data)
value = DateTime.parse data["iso"]
if data.is_a? DateTime
@value = data
elsif data.is_a? Hash
@value = DateTime.parse data["iso"]
elsif data.is_a? String
@value = DateTime.parse data
end
end

def to_json(*a)
Expand Down
4 changes: 2 additions & 2 deletions lib/parse/object.rb
Expand Up @@ -33,11 +33,11 @@ def parse(data)

@parse_object_id ||= data[Protocol::KEY_OBJECT_ID]

if data[Protocol::KEY_CREATED_AT]
if data.has_key? Protocol::KEY_CREATED_AT
@created_at = DateTime.parse data[Protocol::KEY_CREATED_AT]
end

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

Expand Down
49 changes: 49 additions & 0 deletions lib/parse/util.rb
@@ -0,0 +1,49 @@
module Parse

# Parse a JSON representation into a fully instantiated
# class. obj can be either a string or a Hash as parsed
# by JSON.parse
# @param class_name [Object]
# @param obj [Object]
def Parse.parse_json(class_name, obj)

if obj.nil?
nil
# String
elsif obj.is_a? String
parse_json class_name, JSON.parse(obj)

# Array
elsif obj.is_a? Array
obj.collect { |o| parse_json(class_name, o) }

# Hash
elsif obj.is_a? Hash

# If it's a datatype hash
if obj.has_key?(Protocol::KEY_TYPE)
parse_datatype obj
elsif obj.size == 1 && obj.has_key?(Protocol::KEY_RESULTS) && obj[Protocol::KEY_RESULTS].is_a?(Array)
obj[Protocol::KEY_RESULTS].collect { |o| parse_json(class_name, o) }
else # otherwise it must be a regular object
Parse::Object.new class_name, obj
end

else
obj
end
end

def Parse.parse_datatype(obj)
type = obj[Protocol::KEY_TYPE]

case type
when Protocol::TYPE_POINTER
Parse::Pointer.new obj
when Protocol::TYPE_BYTES
Parse::Bytes.new obj
when Protocol::TYPE_DATE
Parse::Date.new obj
end
end
end

0 comments on commit e804895

Please sign in to comment.