Sha256: ac1cebf2841c555602381d647edb37c2b97cd6e0f9e67583e32fcd904f2a625c

Contents?: true

Size: 1.01 KB

Versions: 10

Compression:

Stored size: 1.01 KB

Contents

module JsonApiClient
  class Connection

    attr_reader :faraday

    def initialize(options = {})
      site = options.fetch(:site)
      adapter_options = Array(options.fetch(:adapter, Faraday.default_adapter))
      @faraday = Faraday.new(site) do |builder|
        builder.request :json
        builder.use Middleware::JsonRequest
        builder.use Middleware::Status
        builder.use Middleware::ParseJson
        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 = {}, headers = {})
      faraday.send(request_method, path, params, headers)
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
json_api_client-1.1.0 lib/json_api_client/connection.rb
json_api_client-1.0.2 lib/json_api_client/connection.rb
json_api_client-1.0.1 lib/json_api_client/connection.rb
json_api_client-1.0.0 lib/json_api_client/connection.rb
json_api_client-1.0.0.beta7 lib/json_api_client/connection.rb
json_api_client-1.0.0.beta6 lib/json_api_client/connection.rb
json_api_client-1.0.0.beta5 lib/json_api_client/connection.rb
json_api_client-1.0.0.beta4 lib/json_api_client/connection.rb
json_api_client-1.0.0.beta3 lib/json_api_client/connection.rb
json_api_client-1.0.0.beta2 lib/json_api_client/connection.rb