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

restclient shell accepts get/put/post/delete for curl-style access

This commit is contained in:
Adam Wiggins 2008-09-22 18:01:09 -07:00
parent 74e81c12b1
commit a6de091a94
2 changed files with 25 additions and 2 deletions

6
README
View file

@ -65,6 +65,12 @@ Then invoke:
$ restclient private_site $ restclient private_site
Use as a one-off, curl-style:
$ restclient get http://example.com/resource > output_body
$ restclient put http://example.com/resource < input_body
== Logging == Logging
Write calls to a log filename (can also be "stdout" or "stderr"): Write calls to a log filename (can also be "stdout" or "stderr"):

View file

@ -7,10 +7,18 @@ require "yaml"
def usage(why = nil) def usage(why = nil)
puts "failed for reason: #{why}" if why puts "failed for reason: #{why}" if why
puts "usage: restclient url|name [username] [password]" 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) exit(1)
end end
if %w(get put post delete).include? ARGV.first
@verb = ARGV.shift
else
@verb = nil
end
@url = ARGV.shift || 'http://localhost:4567' @url = ARGV.shift || 'http://localhost:4567'
config = YAML.load(File.read(ENV['HOME'] + "/.restclient")) rescue {} config = YAML.load(File.read(ENV['HOME'] + "/.restclient")) rescue {}
@ -22,7 +30,7 @@ else
end end
usage("invalid url '#{@url}") unless @url =~ /^https?/ usage("invalid url '#{@url}") unless @url =~ /^https?/
usage("to few args") unless ARGV.size < 3 usage("too few args") unless ARGV.size < 3
def r def r
@r ||= RestClient::Resource.new(@url, @username, @password) @r ||= RestClient::Resource.new(@url, @username, @password)
@ -30,6 +38,15 @@ end
r # force rc to load r # force rc to load
if @verb
if %w(put post).include? @verb
puts r.send(@verb, STDIN.read)
else
puts r.send(@verb)
end
exit 0
end
%w(get post put delete).each do |m| %w(get post put delete).each do |m|
eval <<-end_eval eval <<-end_eval
def #{m}(path, *args, &b) def #{m}(path, *args, &b)