Module: Artifice::Excon

Defined in:
lib/artifice/excon.rb

Defined Under Namespace

Classes: Connection, RackRequest

Constant Summary collapse

EXCON_CONNECTION =
::Excon::Connection

Class Method Summary collapse

Class Method Details

.activate_for(host, endpoint) ⇒ Object

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

activate_for('google.com', rack_endpoint)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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) ⇒ Object

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



40
41
42
# File 'lib/artifice/excon.rb', line 40

def self.activate_with(endpoint, &block)
  activate_for(:default, endpoint, &block)
end

.deactivateObject

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



46
47
48
49
# File 'lib/artifice/excon.rb', line 46

def self.deactivate
  Excon::Connection.endpoints.clear
  replace_connection(EXCON_CONNECTION)
end

.deactivate_for(host) ⇒ Object

Deactivate an endpoint for a specific host.



30
31
32
33
34
35
36
# 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