module Artifice::Excon

Constants

EXCON_CONNECTION

Public Class Methods

activate_for(host, endpoint) { || ... } click to toggle source

Activate an endpoint for a specific host. The host has both scheme and port omitted.

activate_for('google.com', rack_endpoint)
# File lib/artifice/excon.rb, line 12
def self.activate_for(host, endpoint)
  # support both hosts of http://google.com or google.com
  Excon::Connection.endpoints[URI.parse(host).host || host] = endpoint

  # activate only after the first stub is added
  replace_connection(Artifice::Excon::Connection) \
    if Excon::Connection.endpoints.count == 1

  if block_given?
    begin
      yield
    ensure
      deactivate_for(host)
    end
  end
end
activate_with(endpoint, &block) click to toggle source

Activate a default endpoint to which all requests will be routed (unless a more specific host endpoint is active).

# File lib/artifice/excon.rb, line 40
def self.activate_with(endpoint, &block)
  activate_for(:default, endpoint, &block)
end
deactivate() click to toggle source

Deactivate all endpoints including the default and all host-specific endpoints as well.

# File lib/artifice/excon.rb, line 46
def self.deactivate
  Excon::Connection.endpoints.clear
  replace_connection(EXCON_CONNECTION)
end
deactivate_for(host) click to toggle source

Deactivate an endpoint for a specific host.

# File lib/artifice/excon.rb, line 30
def self.deactivate_for(host)
  Excon::Connection.endpoints.delete(host)

  # deactivate fully after the last stub is gone
  replace_connection(EXCON_CONNECTION) \
    if Excon::Connection.endpoints.count == 0
end

Private Class Methods

replace_connection(value) click to toggle source
# File lib/artifice/excon.rb, line 53
def self.replace_connection(value)
  ::Excon.class_eval do
    remove_const(:Connection)
    const_set(:Connection, value)
  end
end