diff --git a/README b/README index 163b715..fb58642 100644 --- a/README +++ b/README @@ -65,6 +65,12 @@ Then invoke: $ 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 Write calls to a log filename (can also be "stdout" or "stderr"): diff --git a/bin/restclient b/bin/restclient index 5a7fb70..0fbfb19 100755 --- a/bin/restclient +++ b/bin/restclient @@ -7,10 +7,18 @@ require "yaml" def usage(why = nil) 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) end +if %w(get put post delete).include? ARGV.first + @verb = ARGV.shift +else + @verb = nil +end + @url = ARGV.shift || 'http://localhost:4567' config = YAML.load(File.read(ENV['HOME'] + "/.restclient")) rescue {} @@ -22,7 +30,7 @@ else end 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 @r ||= RestClient::Resource.new(@url, @username, @password) @@ -30,6 +38,15 @@ end 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| eval <<-end_eval def #{m}(path, *args, &b)