Find yourself missing a rails console analogue in your other Ruby web applications? This lightweight gem provides a Rack::Console class that will load your Rack application's code and environment into an IRB or Pry session. Either use Rack::Console.new(options).start directly, or run the provided rack-console executable.
Add this line to your application's Gemfile:
gem "rack-console"And then execute:
$ bundle installOr install it system-wide:
$ gem install rack-consoleRack::Console ships with a rack-console executable that will load your application in an IRB shell (or
Pry if that's included in your Gemfile and you specify the --pry option). Assuming you have a config.ru file in the current directory, simply run:
$ bundle exec rack-console
Loading development environment (Rack::Console 2.0.0)
irb(main):001>
Rack::Console supports some of the same things that rails console provides, as well as some of the options used in rackup:
- An
appmethod that will return your underlying Rack application with rack-test methods mixed in. You can perform fake requests to your app (e.g.response = app.get('/')) - A
reload!method to discard new code or defined variables/constants - The
-coption (or--config) to specify a non-standardconfig.rufile - The
-eoption (or--environment) to specify the Rack environment to load - The
-roption (or--require) to require a file/library before Rack::Console loads - The
-Ioption (or--include) to specify paths (colon-separated) to add to$LOAD_PATHbefore Rack::Console loads - The
-Poption (or--[no-]pry) to specify whether to use Pry instead of IRB (Pry must be installed in your project)
Because Rack::Console is just a class, it's easy to provide a console subcommand to a CLI for your own Rack framework. For example, here's how you could hypothetically implement a console subcommand for a generic Rack CLI using Thor:
require "rack/console"
require "thor"
module Rack
class CLI < Thor
desc "console [ENVIRONMENT]", "Start a Rack console"
method_option :config, aliases: "-c", type: "string", desc: "Specify a Rackup file (default: config.ru)"
method_option :require, aliases: "-r", type: "string", desc: "Require a file/library before console boots"
method_option :include, aliases: "-I", type: "string", desc: "Add colon-separated paths to $LOAD_PATH"
def console
# Set a custom intro message:
# ENV["RACK_CONSOLE_INTRO"] = "Loading Rack::Console..."
#
# Or, to prevent an intro message from being printed at all:
# ENV["IGNORE_RACK_CONSOLE_INTRO"] = "true"
Rack::Console.new(options).start
end
end
end
Rack::CLI.start(ARGV)- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request