Module: Parse
- Defined in:
- lib/parse/util.rb,
lib/parse/error.rb,
lib/parse/query.rb,
lib/parse/client.rb,
lib/parse/object.rb,
lib/parse/protocol.rb,
lib/parse/datatypes.rb
Defined Under Namespace
Modules: Protocol Classes: Bytes, Client, Date, Object, ParseError, ParseProtocolError, Pointer, Query
Constant Summary collapse
- @@client =
A singleton client for use by methods in Object. Always use Parse.client to retrieve the client object.
nil
Class Method Summary collapse
- .client ⇒ Object
-
.get(class_name, object_id = nil) ⇒ Object
Perform a simple retrieval of a simple object, or all objects of a given class.
-
.init(data) ⇒ Object
Initialize the singleton instance of Client which is used by all API methods.
- .parse_datatype(obj) ⇒ Object
-
.parse_json(class_name, obj) ⇒ Object
Parse a JSON representation into a fully instantiated class.
Class Method Details
.client ⇒ Object
86 87 88 89 90 91 |
# File 'lib/parse/client.rb', line 86 def Parse.client if !@@client raise ParseError, "API not initialized" end @@client end |
.get(class_name, object_id = nil) ⇒ Object
Perform a simple retrieval of a simple object, or all objects of a given class. If object_id is supplied, a single object will be retrieved. If object_id is not supplied, then all objects of the given class will be retrieved and returned in an Array.
97 98 99 100 |
# File 'lib/parse/client.rb', line 97 def Parse.get(class_name, object_id = nil) data = Parse.client.get( Protocol.class_uri(class_name, object_id) ) Parse.parse_json class_name, data end |
.init(data) ⇒ Object
Initialize the singleton instance of Client which is used by all API methods. Parse.init must be called before saving or retrieving any objects.
82 83 84 |
# File 'lib/parse/client.rb', line 82 def Parse.init(data) @@client = Client.new(data) end |
.parse_datatype(obj) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/parse/util.rb', line 37 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 |
.parse_json(class_name, obj) ⇒ Object
Parse a JSON representation into a fully instantiated class. obj can be either a string or a Hash as parsed by JSON.parse
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/parse/util.rb', line 8 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 |