Sha256: e32ca1861c9d6c57a6f83d0c48bc3004d3d5b9a61da5eb3444dd1adefc7cf11f

Contents?: true

Size: 1.61 KB

Versions: 64

Compression:

Stored size: 1.61 KB

Contents

#!/usr/bin/env ruby

$:.unshift File.dirname(__FILE__) + "/../lib"
require 'restclient'

require "yaml"

def usage(why = nil)
	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)
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 {}

@url, @username, @password = if c = config[@url]
	[c['url'], c['username'], c['password']]
else
	[@url, *ARGV]
end

usage("invalid url '#{@url}") unless @url =~ /^https?/
usage("too few args") unless ARGV.size < 3

def r
	@r ||= RestClient::Resource.new(@url, @username, @password)
end

r # force rc to load

if @verb
	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
		raise
	end
end

%w(get post put delete).each do |m|
 eval <<-end_eval
def #{m}(path, *args, &b)
	r[path].#{m}(*args, &b)
end
 end_eval
end

def method_missing(s, *args, &b)
	super unless r.respond_to?(s)
	begin
		r.send(s, *args, &b)
	rescue RestClient::RequestFailed => e
		print STDERR, e.response.body
		raise e
	end
end

require 'irb'
require 'irb/completion'

if File.exists? ".irbrc"
	ENV['IRBRC'] = ".irbrc"
end

if File.exists?(rcfile = "~/.restclientrc")
	load(rcfile)
end

ARGV.clear

IRB.start
exit!

Version data entries

64 entries across 64 versions & 15 rubygems

Version Path
adamwiggins-rest-client-0.9.1 bin/restclient
adamwiggins-rest-client-0.9.2 bin/restclient
adamwiggins-rest-client-0.9 bin/restclient
adamwiggins-rest-client-1.0.1 bin/restclient
adamwiggins-rest-client-1.0.2 bin/restclient
adamwiggins-rest-client-1.0.3 bin/restclient
adamwiggins-rest-client-1.0.4 bin/restclient
adamwiggins-rest-client-1.0 bin/restclient
auser-poolparty-1.2.10 vendor/gems/rest-client/bin/restclient
auser-poolparty-1.2.3 vendor/gems/rest-client/bin/restclient
auser-poolparty-1.2.4 vendor/gems/rest-client/bin/restclient
auser-poolparty-1.2.7 vendor/gems/rest-client/bin/restclient
auser-poolparty-1.2.8 vendor/gems/rest-client/bin/restclient
auser-poolparty-1.2.9 vendor/gems/rest-client/bin/restclient
auser-rest-client-1.0.1 bin/restclient
auser-rest-client-1.0 bin/restclient
benschwarz-smoke-0.5.0 vendor/rest-client/bin/restclient
benschwarz-smoke-0.5.1 vendor/rest-client/bin/restclient
benschwarz-smoke-0.5.2 vendor/rest-client-1.0.1/bin/restclient
benschwarz-smoke-0.5.3 vendor/rest-client-1.0.1/bin/restclient