class Fog::Core::Connection
Fog::Core::Connection is a generic class to contain a HTTP link to an API.
It is intended to be subclassed by providers who can then add their own modifications such as authentication or response object.
Public Class Methods
Source
# File lib/fog/core/connection.rb, line 13 def add_user_agent(str) if /\S+\/[\d|.]+/.match(str) @@user_agents << str else raise "User Agent must be in <app name>/<app version> notation." end end
Source
# File lib/fog/core/connection.rb, line 46 def initialize(url, persistent = false, params = {}) @path_prefix = params.delete(:path_prefix) if @path_prefix && params[:path] raise ArgumentError, "optional arg 'path' is invalid when 'path_prefix' is provided" end params[:debug_response] = true unless params.key?(:debug_response) params[:headers] ||= {} params.merge!(persistent: params.fetch(:persistent, persistent)) params[:headers]["User-Agent"] ||= user_agent @excon = Excon.new(url, params) end
Prepares the connection and sets defaults for any future requests.
@param [String] url The destination URL @param persistent [Boolean] @param [Hash] params @option params [String] :body Default text to be sent over a socket. Only used if :body absent in Connection#request params @option params [Hash<Symbol, String>] :headers The default headers to supply in a request. Only used if params is not supplied to Connection#request @option params [String] :host The destination hostâs reachable DNS name or IP, in the form of a String @option params [String] :path Default path; appears after âscheme://host:port/â. Only used if params is not supplied to Connection#request @option params [String] :path_prefix Sticky version of the âpathâ arg. :XSpath_prefix => âfoo/barâ with a request with :path => âblechâ sends a request to path âfoo/bar/blechâ @option params [Fixnum] :port The port on which to connect, to the destination host @option params [Hash] :query Default query; appended to the âscheme://host:port/path/â in the form of â?key=valueâ. Will only be used if params is not supplied to Connection#request @option params [String] :scheme The protocol; âhttpsâ causes OpenSSL to be used @option params [String] :proxy Proxy server; e.g. âmyproxy.com:8888â @option params [Fixnum] :retry_limit Set how many times weâll retry a failed request. (Default 4) @option params [Class] :instrumentor Responds to instrument as in ActiveSupport::Notifications @option params [String] :instrumentor_name Name prefix for instrument events. Defaults to âexconâ
Source
# File lib/fog/core/connection.rb, line 21 def user_agents agents = @@user_agents.dup agents << "fog/#{Fog::VERSION}" if defined?(Fog::VERSION) agents << "fog-core/#{Fog::Core::VERSION}" agents.uniq.compact.join(" ") end
Public Instance Methods
Source
# File lib/fog/core/connection.rb, line 78 def request(params, &block) @excon.request(handle_path_prefix_for(params), &block) end
Makes a request using the connection using Excon
@param [Hash] params @option params [String] :body text to be sent over a socket @option params [Hash<Symbol, String>] :headers The default headers to supply in a request @option params [String] :host The destination hostâs reachable DNS name or IP, in the form of a String @option params [String] :path appears after âscheme://host:port/â @option params [Fixnum] :port The port on which to connect, to the destination host @option params [Hash] :query appended to the âscheme://host:port/path/â in the form of â?key=valueâ @option params [String] :scheme The protocol; âhttpsâ causes OpenSSL to be used @option params [Proc] :response_block
@return [Excon::Response]
@raise [Excon::Errors::StubNotFound] @raise [Excon::Errors::Timeout] @raise [Excon::Errors::SocketError]
Source
# File lib/fog/core/connection.rb, line 90 def reset @excon.reset end
Closes the connection
Protected Instance Methods
Make {#request} available even when it has been overidden by a subclass to allow backwards compatibility.
Private Instance Methods
Source
# File lib/fog/core/connection.rb, line 100 def handle_path_prefix_for(params) return params unless @path_prefix params[:path] = params[:path].sub(/^\//, "") params[:path] = "#{@path_prefix}/#{params[:path]}" params end
Source
# File lib/fog/core/connection.rb, line 96 def user_agent self.class.user_agents end