Class: Vertebrae::Connection

Inherits:
Object
  • Object
show all
Includes:
Authorization, Constants
Defined in:
lib/connection.rb

Constant Summary collapse

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

Constants included from Constants

Vertebrae::Constants::ACCEPT, Vertebrae::Constants::ACCEPT_CHARSET, Vertebrae::Constants::CACHE_CONTROL, Vertebrae::Constants::CONTENT_LENGTH, Vertebrae::Constants::CONTENT_TYPE, Vertebrae::Constants::DATE, Vertebrae::Constants::ETAG, Vertebrae::Constants::LOCATION, Vertebrae::Constants::RATELIMIT_LIMIT, Vertebrae::Constants::RATELIMIT_REMAINING, Vertebrae::Constants::SERVER, Vertebrae::Constants::USER_AGENT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Authorization

#authenticated?, #authentication, #basic_auth, #password?, #username?

Constructor Details

#initialize(options) ⇒ Connection



23
24
25
26
27
28
# File 'lib/connection.rb', line 23

def initialize(options)
  @options = options
  @configuration = Vertebrae::Configuration.new(options)
  @connection = nil
  @stack = nil
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



13
14
15
# File 'lib/connection.rb', line 13

def configuration
  @configuration
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/connection.rb', line 12

def options
  @options
end

Instance Method Details

#connectionObject

Returns a Fraday::Connection object



64
65
66
67
68
69
70
# File 'lib/connection.rb', line 64

def connection
  if @connection
    @connection
  else
    @connection ||= Faraday.new(configuration.faraday_options.merge(:builder => stack))
  end
end

#default_middlewareObject

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/connection.rb', line 33

def default_middleware
  Proc.new do |builder|
    builder.use Faraday::Request::Multipart
    builder.use Faraday::Request::UrlEncoded
    builder.use Vertebrae::Request::BasicAuth, configuration.authentication if configuration.authenticated?

    builder.use Faraday::Response::Logger if ENV['DEBUG']
    unless options[:raw]
      builder.use FaradayMiddleware::Mashify
      builder.use FaradayMiddleware::ParseJson
    end
    builder.use Vertebrae::Response::RaiseError
    builder.adapter configuration.adapter
  end
end

#stack(&block) ⇒ Object

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



52
53
54
55
56
57
58
59
60
# File 'lib/connection.rb', line 52

def stack(&block)
  @stack ||= begin
    if block_given?
      Faraday::Builder.new(&block)
    else
      Faraday::Builder.new(&default_middleware)
    end
  end
end