#!/usr/bin/env ruby # The command line Restfully client require 'restfully' require 'optparse' require 'logger' require 'pp' require 'ripl' require 'ripl/multi_line' require 'ripl/short_errors' # Behaviour of pp in IRB is different on ruby1.9: # * pp(object) returns object#inspect. # * we prefer the behaviour of ruby1.8 where pp returns nil. alias :old_pp :pp def pp(*args) old_pp(*args); nil end logger = Logger.new(STDERR) logger.level = Logger::WARN OPTIONS = {"logger" => logger, "shell" => false} Ripl.config[:play_quiet] = true Ripl.config[:play_input] = false option_parser = OptionParser.new do |opts| opts.banner = < :ruby } module Ripl::Exit def before_loop; super; exit; end end Ripl::Shell.include Ripl::Exit end Ripl.config[:play] = ARGV[0].dup if ARGV[0] end # Declare original Restfully::Session SESSION = Restfully::Session.new(OPTIONS) module Restfully class Session alias :old_initialize :initialize # Overwrite Restfully::Session.new calls that might occur in replayed # scripts, so that they preferably take the configuration given with the # restfully command-line tool. def initialize(options = {}, &block) options[:overridden] = true old_opts = Configuration.new(options).expand new_opts = old_opts.merge(SESSION.config) old_initialize(new_opts, &block) end end end # Ensure to close the log file, if any, when exiting. at_exit{ @log_file && @log_file.close } ARGV.clear Ripl.start :binding => SESSION.sandbox.instance_eval{ binding }