Module: Parse

Defined in:
lib/parse/push.rb,
lib/parse/user.rb,
lib/parse/util.rb,
lib/parse/batch.rb,
lib/parse/cloud.rb,
lib/parse/error.rb,
lib/parse/model.rb,
lib/parse/query.rb,
lib/parse/client.rb,
lib/parse/object.rb,
lib/parse/protocol.rb,
lib/parse/datatypes.rb,
lib/parse/http_client.rb

Defined Under Namespace

Modules: Cloud, Protocol Classes: ArrayOp, Batch, Bytes, Client, Date, File, GeoPoint, HttpClient, Increment, Model, NetHttpClient, Object, ParseError, ParseProtocolError, PatronHttpClient, Pointer, Push, Query, User

Constant Summary collapse

DEFAULT_HTTP_CLIENT =
defined?(JRUBY_VERSION) ? NetHttpClient : PatronHttpClient
@@client =

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

nil

Class Method Summary collapse

Class Method Details

.clientObject



213
214
215
216
217
218
# File 'lib/parse/client.rb', line 213

def Parse.client
  if !@@client
    raise ParseError, "API not initialized"
  end
  @@client
end

.destroyObject

Used mostly for testing. Lets you delete the api key global vars.



208
209
210
211
# File 'lib/parse/client.rb', line 208

def Parse.destroy
  @@client = nil
  self
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.



224
225
226
227
228
229
230
231
232
233
# File 'lib/parse/client.rb', line 224

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
rescue ParseProtocolError => e
  if e.code == Protocol::ERROR_OBJECT_NOT_FOUND_FOR_GET
    e.message += ": #{class_name}:#{object_id}"
  end

  raise
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.



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/parse/client.rb', line 185

def Parse.init(data = {})
  defaulted = {:application_id => ENV["PARSE_APPLICATION_ID"],
               :api_key => ENV["PARSE_REST_API_KEY"]}
  defaulted.merge!(data)

  # use less permissive key if both are specified
  defaulted[:master_key] = ENV["PARSE_MASTER_API_KEY"] unless data[:master_key] || defaulted[:api_key]


  @@client = Client.new(defaulted)
end

.init_from_cloud_code(path = "../config/global.json") ⇒ Object

A convenience method for using global.json



198
199
200
201
202
203
204
205
# File 'lib/parse/client.rb', line 198

def Parse.init_from_cloud_code(path="../config/global.json")
  global = JSON.parse(Object::File.open(path).read) # warning: toplevel constant File referenced by Parse::Object::File
  application_name = global["applications"]["_default"]["link"]
  application_id = global["applications"][application_name]["applicationId"]
  master_key = global["applications"][application_name]["masterKey"]
  Parse.init :application_id => application_id,
             :master_key     => master_key
end

.object_pointer_equality?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
# File 'lib/parse/util.rb', line 71

def Parse.object_pointer_equality?(a, b)
  classes = [Parse::Object, Parse::Pointer]
  return false unless classes.any? { |c| a.kind_of?(c) } && classes.any? { |c| b.kind_of?(c) }
  return true if a.equal?(b)
  return false if a.new? || b.new?

  a.class_name == b.class_name && a.id == b.id
end

.object_pointer_hash(v) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/parse/util.rb', line 80

def Parse.object_pointer_hash(v)
  if v.new?
    v.object_id
  else
    v.class_name.hash ^ v.id.hash
  end
end

.parse_datatype(obj) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/parse/util.rb', line 34

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
    when Protocol::TYPE_GEOPOINT
      Parse::GeoPoint.new obj
    when Protocol::TYPE_FILE
      Parse::File.new obj
    when Protocol::TYPE_OBJECT # used for relation queries, e.g. "?include=post"
      Parse::Object.new obj[Protocol::KEY_CLASS_NAME], Hash[obj.map{|k,v| [k, parse_json(nil, v)]}]
  end
end

.parse_json(class_name, obj) ⇒ Object

Parse a JSON representation into a fully instantiated class. obj can be either a primitive or a Hash of primitives as parsed by JSON.parse

Parameters:



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
# File 'lib/parse/util.rb', line 8

def Parse.parse_json(class_name, obj)
  if obj.nil?
    nil

  # 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 class_name # otherwise it must be a regular object, so deep parse it avoiding re-JSON.parsing raw Strings
      Parse::Object.new class_name, Hash[obj.map{|k,v| [k, parse_json(nil, v)]}]
    else # plain old hash
      obj
    end

  # primitive
  else
    obj
  end
end

.pointerize_value(obj) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/parse/util.rb', line 53

def Parse.pointerize_value(obj)
  if obj.kind_of?(Parse::Object)
    p = obj.pointer
    raise ArgumentError.new("new object used in context requiring pointer #{obj}") unless p
    p
  elsif obj.is_a?(Array)
    obj.map do |v|
      Parse.pointerize_value(v)
    end
  elsif obj.is_a?(Hash)
    Hash[obj.map do |k, v|
      [k, Parse.pointerize_value(v)]
    end]
  else
    obj
  end
end