module Restforce::Concerns::Connection

Public Instance Methods

builder()
Alias for: middleware
middleware() click to toggle source

Public: The Faraday::Builder instance used for the middleware stack. This can be used to insert an custom middleware.

Examples

# Add the instrumentation middleware for Rails.
client.middleware.use FaradayMiddleware::Instrumentation

Returns the Faraday::Builder for the Faraday connection.

# File lib/restforce/concerns/connection.rb, line 15
def middleware
  connection.builder
end
Also aliased as: builder

Private Instance Methods

adapter() click to toggle source

rubocop:enable Metrics/BlockLength rubocop:enable Metrics/AbcSize

# File lib/restforce/concerns/connection.rb, line 75
def adapter
  options[:adapter]
end
connection() click to toggle source

Internal: Internal faraday connection where all requests go through rubocop:disable Metrics/AbcSize rubocop:disable Metrics/BlockLength

# File lib/restforce/concerns/connection.rb, line 25
def connection
  @connection ||= Faraday.new(options[:instance_url],
                              connection_options) do |builder|
    # Parses JSON into Hashie::Mash structures.
    unless options[:mashify] == false
      builder.use Restforce::Middleware::Mashify, self, options
    end

    # Handles multipart file uploads for blobs.
    builder.use Restforce::Middleware::Multipart
    # Converts the request into JSON.
    builder.request :restforce_json
    # Handles reauthentication for 403 responses.
    if authentication_middleware
      builder.use authentication_middleware, self, options
    end
    # Sets the oauth token in the headers.
    builder.use Restforce::Middleware::Authorization, self, options
    # Ensures the instance url is set.
    builder.use Restforce::Middleware::InstanceURL, self, options
    # Caches GET requests.
    builder.use Restforce::Middleware::Caching, cache, options if cache
    # Follows 30x redirects.
    builder.use Faraday::FollowRedirects::Middleware, {
      # Pass the option to clear or send the auth header on redirects through
      clear_authorization_header: options[:clear_authorization_header]
    }
    # Raises errors for 40x responses.
    builder.use Restforce::Middleware::RaiseError
    # Parses returned JSON response into a hash.
    builder.response :restforce_json, content_type: /\bjson$/
    # Compress/Decompress the request/response
    unless adapter == :httpclient
      builder.use Restforce::Middleware::Gzip, self, options
    end
    # Inject custom headers into requests
    builder.use Restforce::Middleware::CustomHeaders, self, options
    # Log request/responses
    if Restforce.log?
      builder.use Restforce::Middleware::Logger,
                  Restforce.configuration.logger,
                  options
    end

    builder.adapter adapter
  end
end
connection_options() click to toggle source

Internal: Faraday Connection options

# File lib/restforce/concerns/connection.rb, line 80
def connection_options
  { request: {
    timeout: options[:timeout],
    open_timeout: options[:timeout]
  },
    proxy: options[:proxy_uri],
    ssl: options[:ssl] }
end
mashify?() click to toggle source

Internal: Returns true if the middlware stack includes the Restforce::Middleware::Mashify middleware.

# File lib/restforce/concerns/connection.rb, line 91
def mashify?
  middleware.handlers.index(Restforce::Middleware::Mashify)
end