Module: Parse

Defined in:
lib/parse/util.rb,
lib/parse/push.rb,
lib/parse/user.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/version.rb,
lib/parse/protocol.rb,
lib/parse/datatypes.rb,
lib/parse/application.rb,
lib/parse/installation.rb

Overview

This module contains all the code

Defined Under Namespace

Modules: Cloud, Protocol Classes: Application, ArrayOp, Batch, Bytes, Client, ConnectionError, Date, File, GeoPoint, Increment, Installation, Model, Object, ParseError, ParseProtocolError, ParseProtocolRetry, Pointer, Push, Query, User

Constant Summary collapse

VERSION =
'1.0.0.alpha3'

Class Method Summary collapse

Class Method Details

.copy_client(client, parsed_data) ⇒ Object

NOTE: this mess is used to pass along the @client to internal objects



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/parse/util.rb', line 100

def self.copy_client(client, parsed_data)
  do_copy = lambda do |object|
    if object.is_a?(Parse::Object)
      object.client = client
      object.each do |_key, value|
        value.client = client if value.is_a?(Parse::Object)
      end
    end
    object
  end

  if parsed_data.is_a?(Array)
    parsed_data.map(&:do_copy)
  else
    parsed_data = do_copy.call(parsed_data)
  end

  parsed_data
end

.create(data = {}, &blk) ⇒ Object Also known as: init

Factory to create instances of Client.



168
169
170
171
172
173
174
175
176
177
# File 'lib/parse/client.rb', line 168

def create(data = {}, &blk)
  options = defaults = {
    application_id: ENV['PARSE_APPLICATION_ID'],
    master_key: ENV['PARSE_MASTER_API_KEY'],
    api_key: ENV['PARSE_REST_API_KEY'],
    get_method_override: true
  }.merge(data)

  Client.new(options, &blk)
end

.get(class_name, object_id = nil, parse_client = 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. Accepts an explicit client object to avoid using the legacy singleton.



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

def get(class_name, object_id = nil, parse_client = nil)
  c = parse_client || client
  data = c.get(Protocol.class_uri(class_name, object_id))
  object = Parse.parse_json(class_name, data)
  object = Parse.copy_client(c, object)
  object
rescue ParseProtocolError => e
  if e.code == Protocol::ERROR_OBJECT_NOT_FOUND_FOR_GET
    e.message += ": #{class_name}:#{object_id}"
  end
  raise
end

.init_from_cloud_code(path = '../config/global.json', app_name = nil) ⇒ Object

A convenience method for using global.json



181
182
183
184
185
186
187
188
# File 'lib/parse/client.rb', line 181

def init_from_cloud_code(path = '../config/global.json', app_name = nil)
  global = JSON.parse(::File.open(path).read)
  applications = global['applications']
  app_name = applications['_default']['link'] if app_name.nil?
  application_id = applications[app_name]['applicationId']
  master_key = applications[app_name]['masterKey']
  create(application_id: application_id, master_key: master_key)
end

.object_pointer_equality?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
# File 'lib/parse/util.rb', line 82

def self.object_pointer_equality?(a, b)
  classes = [Parse::Object, Parse::Pointer]
  return false unless classes.any? { |c| a.is_a?(c) } && classes.any? { |c| b.is_a?(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



91
92
93
94
95
96
97
# File 'lib/parse/util.rb', line 91

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

.parse_datatype(obj) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/parse/util.rb', line 39

def self.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
    # NOTE: passing '' for client to avoid passing nil
    # to trigger the singleton. It's ugly!
    Parse::File.new(obj, '')
  when Protocol::TYPE_OBJECT # used for relation queries, e.g. "?include=post"
    # NOTE: passing '' for client to avoid passing nil
    # to trigger the singleton. It's ugly!
    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:



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

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

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

  # Hash
  elsif obj.is_a? Hash

    # If it's a datatype hash
    if obj.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
      # NOTE: passing '' for client to avoid passing nil to trigger the singleton. It's ugly!
      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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/parse/util.rb', line 64

def self.pointerize_value(obj)
  if obj.is_a?(Parse::Object)
    p = obj.pointer
    raise ArgumentError, "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