Module: BitBucket::Connection

Extended by:
Connection
Includes:
Constants
Included in:
Connection, Request
Defined in:
lib/bitbucket_rest_api/connection.rb

Constant Summary collapse

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

Constants included from Constants

BitBucket::Constants::ACCEPT, BitBucket::Constants::ACCEPT_CHARSET, BitBucket::Constants::CACHE_CONTROL, BitBucket::Constants::CONTENT_LENGTH, BitBucket::Constants::CONTENT_TYPE, BitBucket::Constants::DATE, BitBucket::Constants::ETAG, BitBucket::Constants::LOCATION, BitBucket::Constants::META_FIRST, BitBucket::Constants::META_LAST, BitBucket::Constants::META_NEXT, BitBucket::Constants::META_PREV, BitBucket::Constants::META_REL, BitBucket::Constants::PARAM_PAGE, BitBucket::Constants::PARAM_START_PAGE, BitBucket::Constants::QUERY_STR_SEP, BitBucket::Constants::RATELIMIT_LIMIT, BitBucket::Constants::RATELIMIT_REMAINING, BitBucket::Constants::SERVER, BitBucket::Constants::USER_AGENT

Instance Method Summary collapse

Instance Method Details

#caching?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/bitbucket_rest_api/connection.rb', line 32

def caching?
  !@connection.nil?
end

#clear_cacheObject



28
29
30
# File 'lib/bitbucket_rest_api/connection.rb', line 28

def clear_cache
  @connection = nil
end

#connection(api, options = {}) ⇒ Object

Returns a Fraday::Connection object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bitbucket_rest_api/connection.rb', line 48

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

#default_options(options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/bitbucket_rest_api/connection.rb', line 18

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

#stack(options = {}) ⇒ Object

Exposes middleware builder to facilitate custom stacks and easy addition of new extensions such as cache adapter.



39
40
41
42
43
44
# File 'lib/bitbucket_rest_api/connection.rb', line 39

def stack(options={})
  @stack ||= begin
    builder_class = defined?(Faraday::RackBuilder) ? Faraday::RackBuilder : Faraday::Builder
    builder_class.new(&BitBucket.default_middleware(options))
  end
end