Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Replaces Net::HTTP with a subclass that routes all requests to a Rack application

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.textile
Octocat-spinner-32 Rakefile
Octocat-spinner-32 artifice.gemspec
README.textile

Artifice allows you to replace the Net::HTTP subsystem of Ruby
with an equivalent that routes all requests to a Rack application.

You can use Sinatra, raw Rack, or even Rails as your application,
allowing you to build up an equivalent to the remote service you
are mocking out using familiar and convenient tools to route
requests and build up responses.

Usage

First, require artifice.

require "artifice"

Next, activate artifice using a Rack endpoint.

Artifice.activate_with(rack_endpoint)

You’re done!

You can also pass a block to activate_with:

Artifice.activate_with(rack_endpoint) do
  # make some requests using Net::HTTP
end

This will replace Net::HTTP for the duration of the block only.

You can deactivate Artifice by calling deactivate

Artifice.deactivate

Artifice will restore the last endpoint after a block-style call, so the following will work as expected:

Artifice.activate_with(global_endpoint)

Artifice.activate_with(block_endpoint) do
  # make some requests using Net::HTTP
end

# make any requests after the block and they'll hit the global_endpoint

Furthermore, Artifice will allow you to nest block-style calls to activate_with like so:

Artifice.activate_with(outer_endpoint) do
  # make some requests that hit an endpoint
  Artifice.activate_with(inner_endpoint) do
    # make some requests that require a different endpoint
  end
  # make some other requests that will hit the outer block's endpoint
end
Something went wrong with that request. Please try again.