Module: BitBucket::Connection

Included in:
API
Defined in:
lib/bitbucket_rest_api/connection.rb

Constant Summary collapse

ALLOWED_OPTIONS =
%i[
  headers
  url
  params
  request
  ssl
].freeze

Class Method Summary collapse

Class Method Details

.caching?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/bitbucket_rest_api/connection.rb', line 68

def caching?
  !@connection.nil?
end

.clear_cacheObject



64
65
66
# File 'lib/bitbucket_rest_api/connection.rb', line 64

def clear_cache
  @connection = nil
end

.connection(options = {}) ⇒ Object

Returns a Fraday::Connection object



87
88
89
90
91
92
93
94
95
# File 'lib/bitbucket_rest_api/connection.rb', line 87

def connection(options = {})
  conn_options = default_options(options)
  clear_cache unless options.empty?
  puts "OPTIONS:#{conn_options.inspect}" if ENV['DEBUG']

  @connection ||= Faraday.new(conn_options.merge(builder: stack(options))) do |faraday|
    faraday.response :logger if ENV['DEBUG']
  end
end

.default_middleware(options = {}) ⇒ Object

Default middleware stack that uses default adapter as specified at configuration stage.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bitbucket_rest_api/connection.rb', line 40

def default_middleware(options = {})
  proc do |builder|
    # builder.use BitBucket::Request::Jsonize
    builder.use Faraday::Request::Multipart
    builder.use Faraday::Request::UrlEncoded
    builder.use FaradayMiddleware::OAuth, consumer_key: client_id, consumer_secret: client_secret, token: oauth_token, token_secret: oauth_secret if client_id? && client_secret?
    builder.use BitBucket::Request::BasicAuth, authentication if basic_authed?
    builder.use FaradayMiddleware::EncodeJson

    builder.use Faraday::Response::Logger if ENV['DEBUG']
    builder.use BitBucket::Response::Helpers
    unless options[:raw]
      builder.use BitBucket::Response::Mashify
      builder.use BitBucket::Response::Jsonize
    end
    builder.use BitBucket::Response::RaiseError
    builder.adapter adapter
  end
end

.default_options(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/bitbucket_rest_api/connection.rb', line 27

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

.stack(options = {}, &block) ⇒ Object

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



75
76
77
78
79
80
81
82
83
# File 'lib/bitbucket_rest_api/connection.rb', line 75

def stack(options = {}, &block)
  @stack ||= begin
    if block_given?
      Faraday::RackBuilder.new(&block)
    else
      Faraday::RackBuilder.new(&default_middleware(options))
    end
  end
end