Sha256: 41c6782a5dc4b9c42fc88143159270a73abd0a15b08272ccf1095cf3c74ebec8

Contents?: true

Size: 1.81 KB

Versions: 8

Compression:

Stored size: 1.81 KB

Contents

require 'faraday'
require 'faraday_middleware'
require 'request/basic_auth'
require 'response/raise_error'
require 'authorization'

module Vertebrae
  class Connection
    include Vertebrae::Constants
    include Vertebrae::Authorization

    attr_accessor :options
    attr_accessor :configuration

    ALLOWED_OPTIONS = [
        :headers,
        :url,
        :params,
        :request,
        :ssl
    ].freeze

    def initialize(options)
      @options = options
      @configuration = Vertebrae::Configuration.new(options)
      @connection = nil
      @stack = nil
    end

    # Default middleware stack that uses default adapter as specified at
    # configuration stage.
    #
    def default_middleware
      Proc.new do |builder|
        builder.use Faraday::Request::Multipart
        builder.use Faraday::Request::UrlEncoded
        builder.use Vertebrae::Request::BasicAuth, configuration.authentication if configuration.authenticated?

        builder.use Faraday::Response::Logger if ENV['DEBUG']
        unless options[:raw]
          builder.use FaradayMiddleware::Mashify
          builder.use FaradayMiddleware::ParseJson
        end
        builder.use Vertebrae::Response::RaiseError
        builder.adapter configuration.adapter
      end
    end

    # Exposes middleware builder to facilitate custom stacks and easy
    # addition of new extensions such as cache adapter.
    #
    def stack(&block)
      @stack ||= begin
        if block_given?
          Faraday::Builder.new(&block)
        else
          Faraday::Builder.new(&default_middleware)
        end
      end
    end

    # Returns a Fraday::Connection object
    #
    def connection
      if @connection
        @connection
      else
        @connection ||= Faraday.new(configuration.faraday_options.merge(:builder => stack))
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
vertebrae-0.2.8 lib/connection.rb
vertebrae-0.2.7 lib/connection.rb
vertebrae-0.2.6 lib/connection.rb
vertebrae-0.2.5 lib/connection.rb
vertebrae-0.2.4 lib/connection.rb
vertebrae-0.2.2 lib/connection.rb
vertebrae-0.2.1 lib/connection.rb
vertebrae-0.2.0 lib/connection.rb