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

55 lines
929 B
Ruby
Executable file

#!/usr/bin/env ruby
$:.unshift File.dirname(__FILE__) + "/../lib"
require 'rest_client'
def usage(why = nil)
puts "failed for reason: #{why}" if why
puts "usage: restclient url [username] [password]"
exit(1)
end
@url = ARGV.shift || 'http://localhost:4567'
usage("invalid url '#{@url}") unless @url =~ /^https?/
usage("to few args") unless ARGV.size < 3
def r
@r ||= RestClient::Resource.new(@url, *ARGV)
end
r # force rc to load
%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
puts 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!