Module: Parse::API::CloudFunctions

Included in:
Client
Defined in:
lib/parse/api/cloud_functions.rb

Overview

Defines the CloudCode interface for the Parse REST API

Instance Method Summary collapse

Instance Method Details

#call_function(name, body = {}, opts: {}, context: nil, **kwopts) ⇒ Parse::Response

Call a cloud function.

Parameters:

  • name (String)

    the name of the cloud function.

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

    the parameters to forward to the function.

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

    additional options for the request. Accepts the explicit opts: hash for back-compat, but request options may also be passed as plain keyword arguments (e.g. call_function("f", {}, session_token: t)) to match the rest of the API surface — those are merged into opts (explicit opts: keys win on conflict).

  • context (Hash, nil) (defaults to: nil)

    an optional caller context forwarded as the X-Parse-Cloud-Context header. Parse Server maps it to req.info.context in the cloud function handler. Omit or pass nil to leave behavior unchanged.

Options Hash (opts:):

  • :session_token (String)

    The session token for authenticated requests.

  • :master_key (String)

    Whether to use the master key for this request.

Returns:



25
26
27
28
29
30
31
# File 'lib/parse/api/cloud_functions.rb', line 25

def call_function(name, body = {}, opts: {}, context: nil, **kwopts)
  safe = Parse::API::PathSegment.identifier!(name, kind: "function name")
  headers = {}
  headers[Parse::Protocol::CLOUD_CONTEXT] = context.to_json unless context.nil?
  merged = kwopts.empty? ? opts : kwopts.merge(opts)
  request :post, "functions/#{safe}", body: body, headers: headers, opts: merged
end

#call_function_with_session(name, body = {}, session_token, context: nil) ⇒ Parse::Response

Call a cloud function with a specific session token. This is a convenience method that ensures the session token is properly passed.

Parameters:

  • name (String)

    the name of the cloud function.

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

    the parameters to forward to the function.

  • session_token (String)

    the session token for authenticated requests.

  • context (Hash, nil) (defaults to: nil)

    an optional caller context forwarded as the X-Parse-Cloud-Context header.

Returns:



56
57
58
59
60
# File 'lib/parse/api/cloud_functions.rb', line 56

def call_function_with_session(name, body = {}, session_token, context: nil)
  opts = {}
  opts[:session_token] = session_token if session_token.present?
  call_function(name, body, opts: opts, context: context)
end

#trigger_job(name, body = {}, opts: {}, **kwopts) ⇒ Parse::Response

Trigger a job.

Parameters:

  • name (String)

    the name of the job to trigger.

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

    the parameters to forward to the job.

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

    additional options for the request. Like #call_function, request options may be passed either as the explicit opts: hash or as plain keyword arguments.

Options Hash (opts:):

  • :session_token (String)

    The session token for authenticated requests.

  • :master_key (String)

    Whether to use the master key for this request.

Returns:



42
43
44
45
46
# File 'lib/parse/api/cloud_functions.rb', line 42

def trigger_job(name, body = {}, opts: {}, **kwopts)
  safe = Parse::API::PathSegment.identifier!(name, kind: "job name")
  merged = kwopts.empty? ? opts : kwopts.merge(opts)
  request :post, "jobs/#{safe}", body: body, opts: merged
end

#trigger_job_with_session(name, body = {}, session_token) ⇒ Parse::Response

Trigger a job with a specific session token. This is a convenience method that ensures the session token is properly passed.

Parameters:

  • name (String)

    the name of the job to trigger.

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

    the parameters to forward to the job.

  • session_token (String)

    the session token for authenticated requests.

Returns:



68
69
70
71
72
# File 'lib/parse/api/cloud_functions.rb', line 68

def trigger_job_with_session(name, body = {}, session_token)
  opts = {}
  opts[:session_token] = session_token if session_token.present?
  trigger_job(name, body, opts: opts)
end