Sha256: 86436bd68f7ff8501c68db28f2e0ec10c0b48696613c2b16d1ddf6e909cf1796

Contents?: true

Size: 957 Bytes

Versions: 16

Compression:

Stored size: 957 Bytes

Contents

module JsonApiClient
  class Connection

    attr_reader :faraday

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

  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
json_api_client-0.9.6 lib/json_api_client/connection.rb
json_api_client-0.9.5 lib/json_api_client/connection.rb
json_api_client-0.9.4 lib/json_api_client/connection.rb
json_api_client-0.9.3 lib/json_api_client/connection.rb
json_api_client-0.9.2 lib/json_api_client/connection.rb
json_api_client-0.9.0 lib/json_api_client/connection.rb
json_api_client-0.8.1 lib/json_api_client/connection.rb
json_api_client-0.8.0 lib/json_api_client/connection.rb
json_api_client-0.7.1 lib/json_api_client/connection.rb
json_api_client-0.7.0 lib/json_api_client/connection.rb
json_api_client-0.6.0 lib/json_api_client/connection.rb
json_api_client-0.5.1 lib/json_api_client/connection.rb
json_api_client-0.5.0 lib/json_api_client/connection.rb
json_api_client-0.4.0 lib/json_api_client/connection.rb
json_api_client-0.3.1 lib/json_api_client/connection.rb
json_api_client-0.3.0 lib/json_api_client/connection.rb