Module: Parse

Defined in:
lib/parse/model/core/errors.rb,
lib/parse/query.rb,
lib/parse/stack.rb,
lib/parse/client.rb,
lib/parse/api/all.rb,
lib/parse/api/push.rb,
lib/parse/webhooks.rb,
lib/parse/api/batch.rb,
lib/parse/api/files.rb,
lib/parse/api/hooks.rb,
lib/parse/api/users.rb,
lib/parse/model/acl.rb,
lib/parse/api/config.rb,
lib/parse/api/schema.rb,
lib/parse/api/server.rb,
lib/parse/model/date.rb,
lib/parse/model/file.rb,
lib/parse/model/push.rb,
lib/parse/api/objects.rb,
lib/parse/model/bytes.rb,
lib/parse/model/model.rb,
lib/parse/stack/tasks.rb,
lib/parse/api/sessions.rb,
lib/parse/client/batch.rb,
lib/parse/model/object.rb,
lib/parse/api/aggregate.rb,
lib/parse/api/analytics.rb,
lib/parse/model/pointer.rb,
lib/parse/stack/railtie.rb,
lib/parse/stack/version.rb,
lib/parse/client/caching.rb,
lib/parse/client/request.rb,
lib/parse/model/geopoint.rb,
lib/parse/query/ordering.rb,
lib/parse/client/protocol.rb,
lib/parse/client/response.rb,
lib/parse/model/time_zone.rb,
lib/parse/query/operation.rb,
lib/parse/query/constraint.rb,
lib/parse/webhooks/payload.rb,
lib/parse/model/core/schema.rb,
lib/parse/query/constraints.rb,
lib/parse/model/classes/role.rb,
lib/parse/model/classes/user.rb,
lib/parse/model/core/actions.rb,
lib/parse/model/core/actions.rb,
lib/parse/model/core/builder.rb,
lib/parse/api/cloud_functions.rb,
lib/parse/client/body_builder.rb,
lib/parse/model/core/fetching.rb,
lib/parse/model/core/querying.rb,
lib/parse/client/authentication.rb,
lib/parse/model/classes/product.rb,
lib/parse/model/classes/session.rb,
lib/parse/model/core/properties.rb,
lib/parse/webhooks/registration.rb,
lib/parse/model/associations/has_one.rb,
lib/parse/model/classes/installation.rb,
lib/parse/model/associations/has_many.rb,
lib/parse/model/associations/belongs_to.rb,
lib/parse/model/associations/collection_proxy.rb,
lib/parse/model/associations/pointer_collection_proxy.rb,
lib/parse/model/associations/relation_collection_proxy.rb

Overview

This module is mainly all the basic orm operations. To support batching actions, we use temporary Request objects have contain the operation to be performed (in some cases). This allows to group a list of Request methods, into a batch for sending all at once to Parse.

Defined Under Namespace

Modules: API, Associations, Core, Middleware, Properties, Protocol, Stack Classes: ACL, BatchOperation, Bytes, Client, CollectionProxy, Constraint, DataType, Date, Error, File, GeoPoint, Hyperdrive, Installation, Model, Object, Operation, Order, Pointer, PointerCollectionProxy, Product, Push, Query, RecordNotSaved, RelationAction, RelationCollectionProxy, Request, Response, Role, Session, TimeZone, User, Webhooks

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggingBoolean

Sets Parse::Middleware::BodyBuilder logging. You may specify `:debug` for additional verbosity.

Returns:

  • (Boolean)

18
19
20
# File 'lib/parse/client/body_builder.rb', line 18

def self.logging
  Parse::Middleware::BodyBuilder.logging
end

Class Method Details

.auto_generate_models!Array

Create all Parse::Object subclasses, including their properties and inferred associations by importing the schema for the remote collections in a Parse application. Uses the default configured client.

Returns:

  • (Array)

    an array of created Parse::Object subclasses.

See Also:


15
16
17
18
19
# File 'lib/parse/model/core/builder.rb', line 15

def self.auto_generate_models!
  Parse.schemas.map do |schema|
    Parse::Model::Builder.build!(schema)
  end
end

.auto_upgrade!Object

Perform a non-destructive upgrade of all your Parse schemas in the backend based on the property definitions of your local Object subclasses.


57
58
59
60
61
62
63
# File 'lib/parse/model/object.rb', line 57

def self.auto_upgrade!
  klassModels = Parse::Object.descendants
  klassModels.sort_by(&:parse_class).each do |klass|
    yield(klass) if block_given?
    klass.auto_upgrade!
  end
end

.batch(reqs = nil) ⇒ BatchOperation

Create a new batch operation.

Parameters:

Returns:


12
13
14
# File 'lib/parse/client/batch.rb', line 12

def self.batch(reqs = nil)
  BatchOperation.new(reqs)
end

.cacheMoneta::Transformer, Moneta::Expires

The shared cache for the default client connection. This is useful if you want to also utilize the same cache store for other purposes in your application. This should normally be a Moneta unified cache interface.

Returns:

  • (Moneta::Transformer, Moneta::Expires)

    the cache instance

See Also:


97
98
99
# File 'lib/parse/client.rb', line 97

def self.cache
  @shared_cache ||= Parse::Client.client(:default).cache
end

.call_function(name, body = {}, **opts) ⇒ Object

Helper method to call cloud functions and get results.

Parameters:

  • name (String)

    the name of the cloud code function to call.

  • body (Hash) (defaults to: {})

    the set of parameters to pass to the function.

  • opts (Hash)

    additional options.

Returns:

  • (Object)

    the result data of the response. nil if there was an error.


602
603
604
605
606
607
# File 'lib/parse/client.rb', line 602

def self.call_function(name, body = {}, **opts)
  conn = opts[:session] || opts[:client] ||  :default
  response = Parse::Client.client(conn).call_function(name, body)
  return response if opts[:raw].present?
  response.error? ? nil : response.result["result"]
end

.classify(className) ⇒ Class

Find a corresponding Parse::Object subclass for this string or symbol

Parameters:

  • className (String)

    The name of the Parse class as string (ex. “_User”)

Returns:

  • (Class)

    The proper subclass matching the className.


15
16
17
# File 'lib/parse/model/model.rb', line 15

def self.classify(className)
  Parse::Model.find_class className.to_parse_class
end

.client(conn = :default) ⇒ Parse::Client

Helper method to get the default Parse client.

Parameters:

  • conn (Symbol) (defaults to: :default)

    the name of the client connection to use. Defaults to :default

Returns:


87
88
89
# File 'lib/parse/client.rb', line 87

def self.client(conn = :default)
  Parse::Client.client(conn)
end

.config(conn = :default) ⇒ Hash

Retrieve the App specific Parse configuration parameters. The configuration for a connection is cached after the first request. Use the bang version to force update from the Parse backend.

Examples:

val = Parse.config["myKey"]
val = Parse.config["myKey"] # cached

Parameters:

  • conn (Symbol) (defaults to: :default)

    the name of the client connection to use.

Returns:

  • (Hash)

    the Parse config hash for the session.

See Also:


50
51
52
# File 'lib/parse/client.rb', line 50

def self.config(conn = :default)
  Parse::Client.client(conn).config
end

.config!(conn = :default) ⇒ Hash

Force fetch updated Parse configuration

Parameters:

  • conn (Symbol) (defaults to: :default)

    the name of the client connection to use.

Returns:

  • (Hash)

    the Parse configuration


80
81
82
# File 'lib/parse/client.rb', line 80

def self.config!(conn = :default)
  Parse::Client.client(conn).config!
end

.registered_classesArray

Returns an array of registered Parse::Object subclasses.

Returns:

  • (Array)

    an array of registered Parse::Object subclasses.


38
39
40
# File 'lib/parse/model/object.rb', line 38

def self.registered_classes
    Parse::Object.descendants.map(&:parse_class).uniq
end

.schema(className) ⇒ Hash

Fetch the schema for a specific collection name.

Parameters:

  • className (String)

    the name collection

Returns:

  • (Hash)

    the schema document of this collection.

See Also:

  • Parse::Core::ClassBuilder.build!

51
52
53
# File 'lib/parse/model/object.rb', line 51

def self.schema(className)
  client.schema(className).result
end

.schemasArray<Hash>

Returns the list of all schemas for this application.

Returns:

  • (Array<Hash>)

    the list of all schemas for this application.


43
44
45
# File 'lib/parse/model/object.rb', line 43

def self.schemas
  client.schemas.results
end

.set_config(field, value, conn = :default) ⇒ Hash

Set a parameter in the Parse configuration for an application.

Examples:

# update a config with Parse
Parse.set_config "myKey", "someValue"

Parameters:

  • field (String)

    the name configuration variable.

  • value (Object)

    the value configuration variable. Only Parse types are supported.

  • conn (Symbol) (defaults to: :default)

    the name of the client connection to use.

Returns:

  • (Hash)

    the Parse config hash for the session.


62
63
64
# File 'lib/parse/client.rb', line 62

def self.set_config(field, value, conn = :default)
  Parse::Client.client(conn).update_config({ field => value })
end

.setup(opts = {}) { ... } ⇒ Client

Helper method that users should call to setup the client stack. A block can be passed in order to do additional client configuration. To connect to a Parse server, you will need a minimum of an application_id, an api_key and a server_url. To connect to the server endpoint, you use the setup method below.

Examples:

Parse.setup app_id: "YOUR_APP_ID",
            api_key: "YOUR_REST_API_KEY",
            master_key: "YOUR_MASTER_KEY", # optional
            server_url: 'https://localhost:1337/parse' #default

Parameters:

  • opts (Hash) (defaults to: {})

    a set of connection options to configure the client.

Options Hash (opts):

  • :server_url (String)

    The server url of your Parse Server if you are not using the hosted Parse service. By default it will use ENV if available, otherwise fallback to Parse::Protocol::SERVER_URL.

  • :app_id (String)

    The Parse application id. Defaults to ENV.

  • :api_key (String)

    Your Parse REST API Key. Defaults to ENV.

  • :master_key (String)

    The Parse application master key (optional). If this key is set, it will be sent on every request sent by the client and your models. Defaults to ENV.

  • :logging (Boolean)

    It provides you additional logging information of requests and responses. If set to the special symbol of :debug, it will provide additional payload data in the log messages. This option affects the logging performed by Parse::Middleware::BodyBuilder.

  • :adapter (Object)

    The connection adapter. By default it uses the `Faraday.default_adapter` which is Net/HTTP.

  • :cache (Moneta::Transformer, Moneta::Expires)

    A caching adapter of type Moneta::Transformer or Moneta::Expires that will be used by the caching middleware Parse::Middleware::Caching. Caching queries and object fetches can help improve the performance of your application, even if it is for a few seconds. Only successful GET object fetches and non-empty result queries will be cached by default. You may set the default expiration time with the expires option. At any point in time you may clear the cache by calling the Parse::Client#clear_cache! method on the client connection. See Moneta.

  • :expires (Integer)

    Sets the default cache expiration time (in seconds) for successful non-empty GET requests when using the caching middleware. The default value is 3 seconds. If :expires is set to 0, caching will be disabled. You can always clear the current state of the cache using the clear_cache! method on your Parse::Client instance.

  • :faraday (Hash)

    You may pass a hash of options that will be passed to the Faraday constructor.

Yields:

  • the block for additional configuration with Faraday middleware.

Returns:

  • (Client)

    a new instance of Client

See Also:


577
578
579
580
581
582
583
# File 'lib/parse/client.rb', line 577

def self.setup(opts = {})
  if block_given?
    Parse::Client.new(opts, &Proc.new)
  else
    Parse::Client.new(opts)
  end
end

.trigger_job(name, body = {}, **opts) ⇒ Object

Helper method to trigger cloud jobs and get results.

Parameters:

  • name (String)

    the name of the cloud code job to trigger.

  • body (Hash) (defaults to: {})

    the set of parameters to pass to the job.

  • opts (Hash)

    additional options.

Returns:

  • (Object)

    the result data of the response. nil if there was an error.


590
591
592
593
594
595
# File 'lib/parse/client.rb', line 590

def self.trigger_job(name, body = {}, **opts)
  conn = opts[:session] || opts[:client] ||  :default
  response = Parse::Client.client(conn).trigger_job(name, body)
  return response if opts[:raw].present?
  response.error? ? nil : response.result["result"]
end

.update_config(params, conn = :default) ⇒ Hash

Set a key value pairs in the Parse configuration for an application.

Examples:

# batch update several
Parse.update_config({fieldEnabled: true, searchMiles: 50})

Parameters:

  • params (Hash)

    a set of key value pairs to set in the Parse configuration.

  • conn (Symbol) (defaults to: :default)

    the name of the client connection to use.

Returns:

  • (Hash)

    the Parse config hash for the session.


73
74
75
# File 'lib/parse/client.rb', line 73

def self.update_config(params, conn = :default)
  Parse::Client.client(conn).update_config(params)
end

.use_shortnames!Object

Alias shorter names of core Parse class names. Ex, alias Parse::User to User, Parse::Installation to Installation, etc.


67
68
69
# File 'lib/parse/model/object.rb', line 67

def self.use_shortnames!
  require_relative 'shortnames'
end