Method: Parse::Client#initialize

Defined in:
lib/parse/client.rb

#initialize(opts = {}) ⇒ Client

Create a new client connected to the Parse Server REST API endpoint.

Parameters:

  • (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 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 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 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 #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.

Raises:

  • Parse::Error::ConnectionError if the client was not properly configured with required keys or url.

  • ArgumentError if the cache instance passed to the :cache option is not of Moneta::Transformer or Moneta::Expires

See Also:



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/parse/client.rb', line 244

def initialize(opts = {})
  @server_url     = opts[:server_url] || ENV["PARSE_SERVER_URL"] || Parse::Protocol::SERVER_URL
  @application_id = opts[:application_id] || opts[:app_id] || ENV["PARSE_SERVER_APPLICATION_ID"] || ENV['PARSE_APP_ID']
  @api_key        = opts[:api_key] || opts[:rest_api_key]  || ENV["PARSE_SERVER_REST_API_KEY"] || ENV["PARSE_API_KEY"]
  @master_key     = opts[:master_key] || ENV['PARSE_SERVER_MASTER_KEY'] || ENV["PARSE_MASTER_KEY"]
  opts[:adapter] ||= Faraday.default_adapter
  opts[:expires] ||= 3
  if @server_url.nil? || @application_id.nil? || ( @api_key.nil? && @master_key.nil? )
    raise Parse::Error::ConnectionError, "Please call Parse.setup(server_url:, application_id:, api_key:) to setup a client"
  end
  @server_url += '/' unless @server_url.ends_with?('/')
  #Configure Faraday
  opts[:faraday] ||= {}
  opts[:faraday].merge!(:url => @server_url)
  @conn = Faraday.new(opts[:faraday]) do |conn|
    #conn.request :json

    conn.response :logger if opts[:logging]

    # This middleware handles sending the proper authentication headers to Parse
    # on each request.

    # this is the required authentication middleware. Should be the first thing
    # so that other middlewares have access to the env that is being set by
    # this middleware. First added is first to brocess.
    conn.use Parse::Middleware::Authentication,
                application_id: @application_id,
                master_key: @master_key,
                api_key: @api_key
    # This middleware turns the result from Parse into a Parse::Response object
    # and making sure request that are going out, follow the proper MIME format.
    # We place it after the Authentication middleware in case we need to use then
    # authentication information when building request and responses.
    conn.use Parse::Middleware::BodyBuilder
    if opts[:logging].present? && opts[:logging] == :debug
      Parse::Middleware::BodyBuilder.logging = true
    end

    if opts[:cache].present? && opts[:expires].to_i > 0
      # advanced: provide a REDIS url, we'll configure a Moneta Redis store.
      if opts[:cache].is_a?(String) && opts[:cache].starts_with?("redis://")
        begin
          opts[:cache] = Moneta.new(:Redis, url: opts[:cache])
        rescue LoadError
          puts "[Parse::Middleware::Caching] Did you forget to load the redis gem (Gemfile)?"
          raise
        end
      end

      unless [:key?, :[], :delete, :store].all? { |method| opts[:cache].respond_to?(method) }
        raise ArgumentError, "Parse::Client option :cache needs to be a type of Moneta store"
      end
      self.cache = opts[:cache]
      conn.use Parse::Middleware::Caching, self.cache, {expires: opts[:expires].to_i }
    end

    yield(conn) if block_given?

    conn.adapter opts[:adapter]

  end
  Parse::Client.clients[:default] ||= self
end