IsolatedService provides an unobtrusive way of faking services in Rails which is especially useful in service oriented architecture environments. It generates a sinatra based application in your lib directory that acts as a stand in for your actual service. Here are a few benefits:
-
Increased response time because you are hitting a lightweight app on your own development machine
-
Don’t need to be on the internal network if you are using an internal service
-
Services are always available :)
Add the gem to you Gemfile
gem "isolated_service", :group => :development
Then do your usual bundle install
Now you can run the isolated_service generator to start building out your fake service
rails g isolated_service auth
This also generates (or appends) a Procfile with the following line. Adjust the port as necessary
auth_isolated_service: rackup --port 3900 lib/isolated_service/auth/auth.ru
Update your service’s yml file to point to your fake service. Also include a conditional that checks for the a ENV variable called ISOLATE. For example
base_url: <%= ENV["ISOLATE"] ? "http://localhost:3900" : "http://youractualservice" %>
Stub out your service calls in the generated file located at
lib/isolated_service/auth/auth.rb
Start your fake service
foreman start
Start your server with the ISOLATE=true environment variable
ISOLATE=true rails s
Don’t want to use your fake services? Don’t set the ISOLATE variable. Now your rails application will hit your fake sinatra based service!
How do I read my yaml file if there is ruby in there?
You will need to wrap your yaml file with ERB. For example:
YAML.load(ERB.new(File.read("config/auth_service.yml")).result)
Do I need to use foreman?
No. Foreman is just being used to run your rack app. You can rackup your fake service directly in your terminal.
How do I set an enviroment variable with pow?
Add the following line to the .powenv file in your root directory: export ISOLATE=true
Go here and make a new issue request: github.com/imranraja85/isolated_service/issues
Coded by Imran Raja github.com/imranraja85
Inspired by @chrishunt’s RubyConf talk “Service Oriented Architecture at Square”. You can view it here: www.youtube.com/watch?v=6mesJxUVZyI