Sha256: 07b369ecbec3990b0ab515e692793214431f8f55860679fceeb6e284e83a7f6f

Contents?: true

Size: 1.42 KB

Versions: 11

Compression:

Stored size: 1.42 KB

Contents

module JsonApiClient
  class Connection

    attr_reader :faraday

    def initialize(options = {})
      site = options.fetch(:site)
      connection_options = options.slice(:proxy, :ssl, :request, :headers, :params)
      adapter_options = Array(options.fetch(:adapter, Faraday.default_adapter))
      status_middleware_options = {}
      status_middleware_options[:custom_handlers] = options[:status_handlers] if options[:status_handlers].present?
      @faraday = Faraday.new(site, connection_options) do |builder|
        builder.request :json
        builder.use Middleware::JsonRequest
        builder.use Middleware::Status, status_middleware_options
        builder.use Middleware::ParseJson
        builder.use ::FaradayMiddleware::Gzip
        builder.adapter(*adapter_options)
      end
      yield(self) if block_given?
    end

    # insert middleware before ParseJson - middleware executed in reverse order -
    #   inserted middleware will run after json parsed
    def use(middleware, *args, &block)
      return if faraday.builder.locked?
      faraday.builder.insert_before(Middleware::ParseJson, middleware, *args, &block)
    end

    def delete(middleware)
      faraday.builder.delete(middleware)
    end

    def run(request_method, path, params: nil, headers: {}, body: nil)
      faraday.run_request(request_method, path, body, headers) do |request|
        request.params.update(params) if params
      end
    end

  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
json_api_client-1.22.0 lib/json_api_client/connection.rb
json_api_client-1.21.1 lib/json_api_client/connection.rb
json_api_client-1.21.0 lib/json_api_client/connection.rb
json_api_client-1.20.0 lib/json_api_client/connection.rb
json_api_client-1.19.0 lib/json_api_client/connection.rb
carwow-json_api_client-1.19.0 lib/json_api_client/connection.rb
json_api_client-1.18.0 lib/json_api_client/connection.rb
json_api_client-1.17.1 lib/json_api_client/connection.rb
json_api_client-1.17.0 lib/json_api_client/connection.rb
json_api_client-1.16.1 lib/json_api_client/connection.rb
json_api_client-1.16.0 lib/json_api_client/connection.rb