2008-07-07 22:49:10 -04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
2014-07-08 06:54:01 -04:00
|
|
|
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
|
2008-07-07 22:49:10 -04:00
|
|
|
|
2011-06-13 05:45:37 -04:00
|
|
|
require 'rubygems'
|
|
|
|
require 'restclient'
|
|
|
|
require 'yaml'
|
2008-07-16 19:15:26 -04:00
|
|
|
|
2008-07-16 18:50:54 -04:00
|
|
|
def usage(why = nil)
|
2010-11-04 15:57:46 -04:00
|
|
|
puts "failed for reason: #{why}" if why
|
|
|
|
puts "usage: restclient [get|put|post|delete] url|name [username] [password]"
|
|
|
|
puts " The verb is optional, if you leave it off you'll get an interactive shell."
|
|
|
|
puts " put and post both take the input body on stdin."
|
|
|
|
exit(1)
|
2008-07-07 22:49:10 -04:00
|
|
|
end
|
|
|
|
|
2010-11-04 16:02:00 -04:00
|
|
|
POSSIBLE_VERBS = ['get', 'put', 'post', 'delete']
|
|
|
|
|
|
|
|
if POSSIBLE_VERBS.include? ARGV.first
|
2010-11-04 15:57:46 -04:00
|
|
|
@verb = ARGV.shift
|
2008-09-22 21:01:09 -04:00
|
|
|
else
|
2010-11-04 15:57:46 -04:00
|
|
|
@verb = nil
|
2008-09-22 21:01:09 -04:00
|
|
|
end
|
|
|
|
|
2008-07-16 18:50:54 -04:00
|
|
|
@url = ARGV.shift || 'http://localhost:4567'
|
2008-07-07 22:49:10 -04:00
|
|
|
|
2008-07-16 19:15:26 -04:00
|
|
|
config = YAML.load(File.read(ENV['HOME'] + "/.restclient")) rescue {}
|
|
|
|
|
2014-07-08 05:03:41 -04:00
|
|
|
if (c = config[@url])
|
|
|
|
@url, @username, @password = [c['url'], c['username'], c['password']]
|
2008-07-16 19:15:26 -04:00
|
|
|
else
|
2014-07-08 05:03:41 -04:00
|
|
|
@url, @username, @password = [@url, * ARGV]
|
2008-07-16 19:15:26 -04:00
|
|
|
end
|
|
|
|
|
2008-07-16 18:50:54 -04:00
|
|
|
usage("invalid url '#{@url}") unless @url =~ /^https?/
|
2008-09-22 21:01:09 -04:00
|
|
|
usage("too few args") unless ARGV.size < 3
|
2008-07-07 22:49:10 -04:00
|
|
|
|
|
|
|
def r
|
2010-11-04 15:57:46 -04:00
|
|
|
@r ||= RestClient::Resource.new(@url, @username, @password)
|
2008-07-07 22:49:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
r # force rc to load
|
|
|
|
|
2008-09-22 21:01:09 -04:00
|
|
|
if @verb
|
2010-11-04 15:57:46 -04:00
|
|
|
begin
|
|
|
|
if %w( put post ).include? @verb
|
|
|
|
puts r.send(@verb, STDIN.read)
|
|
|
|
else
|
|
|
|
puts r.send(@verb)
|
|
|
|
end
|
|
|
|
exit 0
|
|
|
|
rescue RestClient::Exception => e
|
2014-04-02 04:49:56 -04:00
|
|
|
puts e.response.body if e.respond_to?(:response) && e.response
|
2010-11-04 15:57:46 -04:00
|
|
|
raise
|
|
|
|
end
|
2008-09-22 21:01:09 -04:00
|
|
|
end
|
|
|
|
|
2010-11-04 16:02:00 -04:00
|
|
|
POSSIBLE_VERBS.each do |m|
|
2014-11-27 22:15:53 -05:00
|
|
|
define_method(m.to_sym) do |path, *args, &b|
|
|
|
|
r[path].public_send(m.to_sym, *args, &b)
|
|
|
|
end
|
2008-07-07 22:49:10 -04:00
|
|
|
end
|
|
|
|
|
2010-11-04 15:57:46 -04:00
|
|
|
def method_missing(s, * args, & b)
|
2010-11-04 16:02:00 -04:00
|
|
|
if POSSIBLE_VERBS.include? s
|
|
|
|
begin
|
2010-11-15 05:58:51 -05:00
|
|
|
r.send(s, *args, & b)
|
2010-11-04 16:02:00 -04:00
|
|
|
rescue RestClient::RequestFailed => e
|
|
|
|
print STDERR, e.response.body
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
else
|
|
|
|
super
|
2010-11-04 15:57:46 -04:00
|
|
|
end
|
2008-07-07 22:49:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
require 'irb'
|
|
|
|
require 'irb/completion'
|
|
|
|
|
2014-07-08 05:25:13 -04:00
|
|
|
if File.exist? ".irbrc"
|
2010-11-04 15:57:46 -04:00
|
|
|
ENV['IRBRC'] = ".irbrc"
|
2008-07-07 22:49:10 -04:00
|
|
|
end
|
|
|
|
|
2014-07-08 05:03:41 -04:00
|
|
|
rcfile = File.expand_path("~/.restclientrc")
|
2014-07-08 05:25:13 -04:00
|
|
|
if File.exist?(rcfile)
|
2010-11-04 15:57:46 -04:00
|
|
|
load(rcfile)
|
2008-07-07 22:49:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
ARGV.clear
|
|
|
|
|
|
|
|
IRB.start
|
|
|
|
exit!
|