Module: BitBucket::Connection

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

Constant Summary collapse

ALLOWED_OPTIONS =
[
    :headers,
    :url,
    :params,
    :request,
    :ssl
].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::HEADER_LAST, BitBucket::Constants::HEADER_LINK, BitBucket::Constants::HEADER_NEXT, 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_PER_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)


71
72
73
# File 'lib/bitbucket_rest_api/connection.rb', line 71

def caching?
  !@connection.nil?
end

#clear_cacheObject



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

def clear_cache
  @connection = nil
end

#connection(options = {}) ⇒ Object

Returns a Fraday::Connection object



90
91
92
93
94
95
96
97
98
# File 'lib/bitbucket_rest_api/connection.rb', line 90

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
59
60
61
# File 'lib/bitbucket_rest_api/connection.rb', line 40

def default_middleware(options={})
  Proc.new do |builder|
    #builder.use BitBucket::Request::Jsonize
    builder.use Faraday::Request::Multipart
    builder.use Faraday::Request::UrlEncoded

    if client_id? && client_secret?
      builder.use FaradayMiddleware::OAuth, {consumer_key: client_id, consumer_secret: client_secret, token: oauth_token, token_secret: oauth_secret}
    end

    builder.use BitBucket::Request::BasicAuth, authentication if basic_authed?
    builder.use FaradayMiddleware::EncodeJson

    builder.use Faraday::Response::Logger if ENV['DEBUG']
    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



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

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

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

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



78
79
80
81
82
83
84
85
86
# File 'lib/bitbucket_rest_api/connection.rb', line 78

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