A version of Wycat's Artifice for use with Excon.
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib/artifice
test
.gitignore
Gemfile
Gemfile.lock
README.md
Rakefile
artifice-excon.gemspec

README.md

artifice-excon

Artifice allows you to replace remote HTTP/HTTPS calls with a stub that routes all requests to a Rack application. This version integrates with Excon instead of the Net:HTTP subsystem that Wycat's original gem provides.

Remote service stubs can be constructed using Sinatra, raw Rack, or even Rails, allowing you to leverage familiar and convenient tools.

Usage

Bundle artifice-excon in your Gemfile:

gem 'artifice-excon'

Use Bundle.require or require it manually:

require 'artifice/excon'

Next, activate artifice-excon by passing in a Rack endpoint:

Artifice::Excon.activate_with(rack_endpoint)

# an extremely basic example
app = proc do |env|
  [200, {}, "Hello, world!"]
end
Artifice::Excon.activate_with(app)

And finally, deactivate it again:

Artifice::Excon.deactivate

Alternatively, pass a block to activate_with to keep stub requests for the duration of the block only:

Artifice.activate_with(rack_endpoint) do
  Excon.get("https://google.com")
end

Host-based Activation

A feature present in artifice-excon that isn't in the original is host-based endpoint activation, so that a particular rack app can be activated for a particular host. This is useful in case you have an app that potentially needs to speak to multiple services with different APIs.

Artifice::Excon.activate_for('google.com', google_endpoint)

# a catch-all is still allowed! but a registered host-specific endpoint will
# always take preference
Artifice::Excon.activate_with(rack_endpoint)

Once again, deactivate for a particular host or everything:

Artifice.deactivate_for('google.com')

# deactivates all registered endpoints including host-specific and global
Artifice.deactivate

Development

Run tests using:

rake test