Tagged With   gem:name=tux , gem:tags=sinatra , post:lang=ruby , post:type=tutorial

Tux - A Sinatra Console

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.

sinatra with a tux

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:

  >> app.request = Sinatra::Request.new({})
  => ...
  >> app.response = Sinatra::Response.new
  => ...

Last but not least, use app to interact with views:

  >> app.erb :my_template
  => 'template rendered'

  # also
  >> app.haml
  >> app.markdown
  ...

Interact with App Responses

Tux let’s you make requests using http verbs thanks to rack-test. Use them to interact with your app’s response objects:

  >> get '/'
  => #<\Rack::MockResponse:0x13d452c @headers={"Content-Type"=>"text/html;charset=utf-8",
  "Content-Length"=>"4"}, @errors="127.0.0.1 - - [05/Apr/2011 02:22:27] \"GET / \" 200 4
  0.0015\n", @status=200, @original_headers={"Content-Type"=>"text/html;charset=utf-8",
  "Content-Length"=>"4"}, @body="dude">

  >> _.headers
  => {"Content-Type"=>"text/html;charset=utf-8", "Content-Length"=>"4"}

  # last_response saves the response of your last request
  >> puts last_response.body
  dude

  >> post '/create', :user => 'cow', :password => 'abunga'
  ...

All http verb methods can take optional params and request headers as rack-test reveals. To see the full list of rack-test actions you can make:

  >> rack.actions
  => [:request, :get, :post, :put, :delete, :head, :follow_redirect!, :header, :set_cookie,
  :clear_cookies, :authorize, :basic_authorize, :digest_authorize, :last_response, :last_request]

Commands and Configuration

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.

Enjoyed this post? Tell others! hacker newsHacker News | twitterTwitter | DeliciousDelicious | redditReddit
blog comments powered by Disqus