The pattern I use is to replace api object with one that fakes all methods when in development.
class Api
def query
# perform api query
end
end
class FakeApi
def query
{ fake: 'This is a fake return' }
end
end
# config/environments/production.rb
config.api = Api.new
# config/environments/test.rb
# config/environments/development.rb
config.api = FakeApi.new
# then
def api_call
Rails.configuration.api.query # no branching here! code is clean
end
Basically, you have two classes, Api
which does real work and FakeApi
that returns pre-baked faked responses. You then use Rails' environment configuration to set different apis in different environments. This way, your client code (that calls #query
) doesn't have to care about current environment.