While working on a sinatra app recently, I noticed that sinatra has no rails equivalent to rails console. So here’s tux, dressing your sinatra app in a console.
Install and Try On
$ gem install tux
In the directory of your sinatra app, try on tux:
$ tux
# If app's rack config file isn't config.ru, specify it with -c
$ tux -c app.ru
# ruby options like -I and -r are available to tux
$ tux -h
Usage: tux [COMMAND] [ARGS] [OPTIONS]
Options:
-f Suppress loading ~/.irbrc
-F Suppress loading ~/.riplrc
-d, --debug Set $DEBUG to true (same as `ruby -d')
-I PATH Add to front of $LOAD_PATH. Delimit multiple paths with ':'
-r, --require FILE Require file (same as `ruby -r')
-v, --version Print version
-h, --help Print help
-c, --config FILE Set rack config file i.e. config.ru
Interact with App Methods
Tux provides the app object to interact with your app’s methods. For example, to interact with helpers:
>> app.my_helper_method
...
Since app is an instance of Sintra::Base, it can interact with any built-in sinatra methods i.e. request and response specific helper methods:
# depends on request
>> app.uri '/'
=> "http://:/"
# depends on response
>> app.headers
=> {"Content-Type"=>"text/html"}
For the above to work, tux sets up default empty request and response objects. To use custom requests and responses:
Tux comes with commands to give you a good overview of your app:
# Displays routes defined by HTTP verb and order they were defined
>> routes
HEAD "/"
HEAD "/book/:id"
GET "/"
GET "/book/:id"
# Displays app settings configured via sinatra's #set method
>> settings
absolute_redirects true
add_charset [/^text\//, "application/javascript", "application/xml",
"application/xhtml+xml"]
app_file "./sample.rb"
bind "0.0.0.0"
default_encoding "utf-8"
dump_errors true
empty_path_info nil
environment :development
lock false
logging false
method_override false
port 4567
prefixed_redirects false
public "/my/path/public"
raise_errors false
reload_templates true
root "/my/path"
run false
running false
server ["thin", "mongrel", "webrick"]
session_secret "XXX"
sessions false
show_exceptions true
static true
views "/my/path/views"
Since tux is a ripl shell, tux is highly configurable. You can create tux commands in the format tux-COMMAND and enhance your shell by adding ripl plugins to ~/.riplrc. Read ripl’s readme for more.