1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00
rest-client--rest-client/bin/restclient

93 lines
1.8 KiB
Text
Raw Permalink Normal View History

2008-07-07 22:49:10 -04:00
#!/usr/bin/env ruby
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
2008-07-07 22:49:10 -04:00
require 'rubygems'
require 'restclient'
require 'yaml'
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
else
2010-11-04 15:57:46 -04:00
@verb = nil
end
@url = ARGV.shift || 'http://localhost:4567'
2008-07-07 22:49:10 -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']]
else
2014-07-08 05:03:41 -04:00
@url, @username, @password = [@url, * ARGV]
end
usage("invalid url '#{@url}") unless @url =~ /^https?/
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
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
puts e.response.body if e.respond_to?(:response) && e.response
2010-11-04 15:57:46 -04:00
raise
end
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'
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")
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!