├── CHANGELOG.txt ├── LICENSE.txt ├── README.md ├── Rakefile ├── bin └── racksh ├── lib └── racksh │ ├── init.rb │ ├── irb.rb │ ├── session.rb │ └── version.rb └── racksh.gemspec /CHANGELOG.txt: -------------------------------------------------------------------------------- 1 | === 0.9.8 / 2010-09-03 2 | 3 | * added option to load racksh into existing irb session 4 | 5 | === 0.9.7 / ? 6 | === 0.9.6 / ? 7 | 8 | === 0.9.5 / 2010-01-31 9 | 10 | * added application reloading with "reload!" 11 | 12 | === 0.9.4 / 2009-11-19 13 | 14 | * added loading irb/completion 15 | * added support for session setup, loaded from .rackshrc in user's home directory and app's directory 16 | * prevented STDOUT & STDERR to be reopened 17 | * added showing simple prompt (>>) like Rails console 18 | * added printing startup info in colors 19 | 20 | === 0.9.3 / 2009-11-17 21 | 22 | * exposed Rack::Test::Methods via $rack variable 23 | 24 | === 0.9.2 / 2009-11-15 25 | 26 | * irb require uses absolute path (for users without rubygems required in their .irbrc) 27 | 28 | === 0.9.1 / 2009-11-15 29 | 30 | * added showing name of loaded rack env 31 | 32 | === 0.9 / 2009-11-15 33 | 34 | * initial release 35 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009 Marcin Kulik 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # racksh 2 | 3 | ## About 4 | 5 | **racksh** (Rack::Shell) is a console for Rack based ruby web applications. 6 | 7 | It's like _script/console_ in Rails or _merb -i_ in Merb, but for any app built on Rack. You can use it to load application 8 | environment for Rails, Merb, Sinatra, Camping, Ramaze or your own framework provided there is _config.ru_ file in app's root 9 | directory. 10 | 11 | It's purpose is to allow developer to introspect his application and/or make some initial setup. You can for example run 12 | _DataMapper.auto_migrate!_ or make a request to _/users/666_ and check response details. It's mainly aimed at apps that don't 13 | have console-like component (ie. app built with Sinatra) but all frameworks can benefit from interactive Rack stack and request 14 | introspection. 15 | 16 | ## How it works? 17 | 18 | It loads whole application environment like Rack web server, but instead of running the app it starts _irb_ session. 19 | Additionally it exposes _$rack_ variable which allows you to make simulated HTTP requests to your app. 20 | 21 | ## Installation 22 | 23 | gem install racksh 24 | 25 | ## Usage 26 | 27 | ### Starting racksh 28 | 29 | To start racksh session run following inside rack application directory (containing config.ru file): 30 | 31 | % racksh 32 | Rack::Shell v1.0.1 started in development environment. 33 | >> 34 | 35 | Specifying location of config.ru: 36 | 37 | % CONFIG_RU=~/projects/foobar/config.ru racksh 38 | 39 | Executing ruby code inside application environment and printing results: 40 | 41 | % racksh Order.all 42 | % racksh "Order.first :created_at => Date.today" 43 | 44 | Specifying Rack environment (default is development): 45 | 46 | % RACK_ENV=production racksh 47 | Rack::Shell v1.0.1 started in production environment. 48 | >> 49 | 50 | ### Making simulated HTTP requests to your app 51 | 52 | % racksh 53 | Rack::Shell v1.0.1 started in development environment. 54 | >> $rack.get "/" 55 | => #"text/html", "Content-Length"=>"1812"}, @status=200, ... 56 | 57 | _$rack_ variable contains following methods (thanks to [rack-test](http://github.com/brynary/rack-test) gem): 58 | 59 | # make GET request 60 | $rack.get uri, params, env 61 | 62 | # make POST request 63 | $rack.post uri, params, env 64 | 65 | # make PUT request 66 | $rack.put uri, params, env 67 | 68 | # make DELETE request 69 | $rack.delete uri, params, env 70 | 71 | # make HEAD request 72 | $rack.head uri, params, env 73 | 74 | # make custom request 75 | $rack.request uri, params, env 76 | 77 | # set HTTP header 78 | $rack.header name, value 79 | 80 | # set credentials for Basic Authorization 81 | $rack.basic_authorize username, password 82 | 83 | # set credentials for Digest Authorization 84 | $rack.digest_authorize username, password 85 | 86 | # follow redirect from previous request 87 | $rack.follow_redirect! 88 | 89 | # last request object 90 | $rack.last_request 91 | 92 | # last response object 93 | $rack.last_response 94 | 95 | # access your Rack app 96 | $rack.app 97 | 98 | # name of environment 99 | $rack.env 100 | 101 | Check [test.rb from brynary's rack-test](http://github.com/brynary/rack-test/blob/master/lib/rack/test.rb) for implementation of 102 | above methods. 103 | 104 | Examples: 105 | 106 | $rack.get "/", {}, { 'REMOTE_ADDR' => '123.45.67.89' } 107 | $rack.header "User-Agent", "Firefox" 108 | $rack.post "/users", :user => { :name => "Jola", :email => "jola@misi.ak" } 109 | 110 | ### Configuration files 111 | 112 | Rack::Shell supports configuration file _.rackshrc_ which is loaded from two places during startup: user's home dir and 113 | application directory (in this order). You can put any ruby code in it, but it's purpose is to setup your session, ie. setting 114 | headers which will be used for all $rack.get/post/... requests. 115 | 116 | For example to set user agent to Firefox and re-migrate db if loaded environment is _test_ put following in _.rackshrc_: 117 | 118 | $rack.header "User-Agent", "Firefox" 119 | DataMapper.auto_migrate! if $rack.env == "test" 120 | 121 | You can also make requests: 122 | 123 | $rack.put "/signin", :login => "jola", :password => "misiacz" 124 | 125 | This will ensure you are always logged in when you start _racksh_. 126 | 127 | ### Reloading 128 | 129 | If you've made some changes to your app and you want to reload it type: 130 | 131 | reload! 132 | 133 | It will reload (actually restart) whole Rack application in new process. 134 | 135 | ### Loading racksh into existing irb session 136 | 137 | If you already opened irb and you want racksh functionality just run following: 138 | 139 | require 'racksh/irb' 140 | 141 | It will initialize racksh and load rack app. From now on you can use _$rack_. 142 | 143 | ## Bugs & feature requests 144 | 145 | Please report bugs and/or feature requests on the github issue tracker for the project located [here](http://github.com/ku1ik/racksh/issues). 146 | 147 | ## Authors 148 | 149 | * Marcin Kulik - [ku1ik.com](https://ku1ik.com/) 150 | 151 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'rake' 3 | require 'rubygems/package_task' 4 | 5 | task :default => [:repackage] 6 | 7 | spec = eval(File.read('racksh.gemspec')) 8 | 9 | Gem::PackageTask.new(spec) do |pkg| 10 | pkg.need_tar = true 11 | end 12 | -------------------------------------------------------------------------------- /bin/racksh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require File.expand_path(File.join(File.dirname(__FILE__), "..", "lib", "racksh", "init.rb")) 4 | 5 | # prevent STDOUT & STDERR to be reopened (apps do this to be able to log under Passenger) 6 | def STDOUT.reopen(*args); end 7 | def STDERR.reopen(*args); end 8 | 9 | if ARGV.empty? 10 | begin 11 | require "pry" 12 | Interpreter = Pry 13 | rescue LoadError 14 | require "irb" 15 | require "irb/completion" 16 | Interpreter = IRB 17 | end 18 | 19 | Rack::Shell.init 20 | ARGV.concat(['--prompt', 'simple']) unless ARGV.include?('--prompt') 21 | Interpreter.start 22 | else 23 | Rack::Shell.init 24 | p eval(ARGV.join(" ")) 25 | end 26 | -------------------------------------------------------------------------------- /lib/racksh/init.rb: -------------------------------------------------------------------------------- 1 | require 'rack' 2 | 3 | ENV['RACK_ENV'] ||= 'development' 4 | ENV['CONFIG_RU'] ||= 'config.ru' 5 | 6 | dir = File.expand_path(File.dirname(__FILE__)) 7 | %w(session version init).each { |f| require File.join(dir, f) } 8 | 9 | def reload! 10 | puts "Rack::Shell reloading..." 11 | ENV['RACKSH_SKIP_INTRO'] = "1" 12 | exec $0 13 | end 14 | 15 | module Rack 16 | module Shell 17 | File = ::File 18 | 19 | def self.init 20 | config_ru = ENV['CONFIG_RU'] 21 | 22 | # build Rack app 23 | rack_app = Array(Rack::Builder.parse_file(config_ru)).first 24 | $rack = Rack::Shell::Session.new(rack_app) 25 | 26 | # run ~/.rackshrc 27 | rcfile = File.expand_path("~/.rackshrc") 28 | eval(File.read(rcfile)) if File.exist?(rcfile) 29 | 30 | # run local .rackshrc (from app dir) 31 | rcfile = File.expand_path(File.join(File.dirname(config_ru), ".rackshrc")) 32 | eval(File.read(rcfile)) if File.exist?(rcfile) 33 | 34 | # print startup info 35 | unless ENV['RACKSH_SKIP_INTRO'] 36 | if STDOUT.tty? && ENV['TERM'] != 'dumb' # we have color terminal, let's pimp our info! 37 | env_color = ($rack.env == 'production' ? "\e[31m\e[1m" : "\e[36m\e[1m") 38 | puts "\e[32m\e[1mRack\e[0m\e[33m\e[1m::\e[0m\e[32m\e[1mShell\e[0m v#{VERSION} started in #{env_color}#{$rack.env}\e[0m environment." 39 | else 40 | puts "Rack::Shell v#{VERSION} started in #{$rack.env} environment." 41 | end 42 | @reloaded = true 43 | end 44 | rescue Errno::ENOENT => e 45 | if e.message =~ /config\.ru$/ 46 | puts "Rack::Shell couldn't find #{config_ru}" 47 | exit(1) 48 | else 49 | raise e 50 | end 51 | end 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /lib/racksh/irb.rb: -------------------------------------------------------------------------------- 1 | require 'racksh/init' 2 | Rack::Shell.init 3 | -------------------------------------------------------------------------------- /lib/racksh/session.rb: -------------------------------------------------------------------------------- 1 | require 'rack/test' 2 | 3 | module Rack 4 | module Shell 5 | class Session 6 | include Rack::Test::Methods 7 | attr_reader :app 8 | 9 | def initialize(app) 10 | @app = app 11 | end 12 | 13 | def env 14 | ENV['RACK_ENV'] 15 | end 16 | end 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /lib/racksh/version.rb: -------------------------------------------------------------------------------- 1 | module Rack 2 | module Shell 3 | VERSION = '1.0.1'.freeze 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /racksh.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | require File.join(File.dirname(__FILE__), "lib", "racksh", "version.rb") 3 | require 'date' 4 | 5 | Gem::Specification.new do |s| 6 | s.name = %q{racksh} 7 | s.version = "" + Rack::Shell::VERSION 8 | s.platform = Gem::Platform::RUBY 9 | s.date = Date.today.to_s 10 | s.authors = ["Marcin Kulik"] 11 | s.email = %q{marcin.kulik@gmail.com} 12 | s.license = 'MIT' 13 | s.has_rdoc = false 14 | s.homepage = %q{http://github.com/ku1ik/racksh} 15 | s.summary = %q{Console for any Rack based ruby web app} 16 | s.executables = ["racksh"] 17 | s.files = Dir["bin/*"] + Dir["lib/**/*.rb"] + ["README.md", "CHANGELOG.txt"] 18 | s.add_dependency 'rack', '>= 1.0' 19 | s.add_dependency 'rack-test', '>= 0.5' 20 | end 21 | --------------------------------------------------------------------------------