Sha256: 70c118b26e0f45fcd5ab30bde535e8c72f527b06e05855de15d904238c655363

Contents?: true

Size: 1.51 KB

Versions: 5

Compression:

Stored size: 1.51 KB

Contents

# encoding: utf-8

module BitBucket
  module Connection
    extend self
    include BitBucket::Constants

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

    def default_options(options={})
      {
          :headers => {
              USER_AGENT       => options[:user_agent]
          },
          :ssl => { :verify => false },
          :url => options.fetch(:endpoint) { BitBucket.endpoint }
      }.merge(options)
    end

    def clear_cache
      @connection = nil
    end

    def caching?
      !@connection.nil?
    end

    # Exposes middleware builder to facilitate custom stacks and easy
    # addition of new extensions such as cache adapter.
    #
    def stack(options={})
      @stack ||= begin
        builder_class = defined?(Faraday::RackBuilder) ? Faraday::RackBuilder : Faraday::Builder
        builder_class.new(&BitBucket.default_middleware(options))
      end
    end

    # Returns a Fraday::Connection object
    #
    def connection(api, options = {})
      connection_options = default_options(options)
      clear_cache unless options.empty?
      builder = api.stack ? api.stack : stack(options.merge!(api: api))
      connection_options.merge!(builder: builder)
      connection_options.keep_if {|key, value| ALLOWED_OPTIONS.include?(key) }

      puts "OPTIONS:#{connection_options.inspect}" if ENV['DEBUG']

      @connection ||= Faraday.new(connection_options)
    end

  end # Connection
end # BitBucket

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
codenamev_bitbucket_api-0.4.1 lib/bitbucket_rest_api/connection.rb
codenamev_bitbucket_api-0.4.0 lib/bitbucket_rest_api/connection.rb
reenhanced_bitbucket_api-0.3.2 lib/bitbucket_rest_api/connection.rb
reenhanced_bitbucket_api-0.3.1 lib/bitbucket_rest_api/connection.rb
reenhanced_bitbucket_api-0.3.0 lib/bitbucket_rest_api/connection.rb