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)


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.new 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? and 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