| 1 |
#!/usr/bin/env ruby |
| 2 |
|
| 3 |
# httpclient shell command. |
| 4 |
# |
| 5 |
# Usage: 1) % httpclient get https://www.google.co.jp/ q=ruby |
| 6 |
# Usage: 2) % httpclient |
| 7 |
# |
| 8 |
# For 1) it issues a GET request to the given URI and shows the wiredump and |
| 9 |
# the parsed result. For 2) it invokes irb shell with the binding that has a |
| 10 |
# HTTPClient as 'self'. You can call HTTPClient instance methods like; |
| 11 |
# > get "https://www.google.co.jp/", :q => :ruby |
| 12 |
require 'httpclient' |
| 13 |
|
| 14 |
method = ARGV.shift |
| 15 |
if method == 'version' |
| 16 |
puts HTTPClient::VERSION |
| 17 |
exit |
| 18 |
end |
| 19 |
|
| 20 |
url = ARGV.shift |
| 21 |
if method && url |
| 22 |
client = HTTPClient.new |
| 23 |
client.strict_response_size_check = true |
| 24 |
if method == 'download' |
| 25 |
print client.get_content(url) |
| 26 |
else |
| 27 |
client.debug_dev = STDERR |
| 28 |
$DEBUG = true |
| 29 |
require 'pp' |
| 30 |
pp client.send(method, url, *ARGV) |
| 31 |
end |
| 32 |
exit |
| 33 |
end |
| 34 |
|
| 35 |
require 'irb' |
| 36 |
require 'irb/completion' |
| 37 |
|
| 38 |
class Runner |
| 39 |
def initialize |
| 40 |
@httpclient = HTTPClient.new |
| 41 |
@httpclient.strict_response_size_check = true |
| 42 |
end |
| 43 |
|
| 44 |
def method_missing(msg, *a, &b) |
| 45 |
debug, $DEBUG = $DEBUG, true |
| 46 |
begin |
| 47 |
@httpclient.send(msg, *a, &b) |
| 48 |
ensure |
| 49 |
$DEBUG = debug |
| 50 |
end |
| 51 |
end |
| 52 |
|
| 53 |
def run |
| 54 |
IRB.setup(nil) |
| 55 |
ws = IRB::WorkSpace.new(binding) |
| 56 |
irb = IRB::Irb.new(ws) |
| 57 |
IRB.conf[:MAIN_CONTEXT] = irb.context |
| 58 |
|
| 59 |
trap("SIGINT") do |
| 60 |
irb.signal_handle |
| 61 |
end |
| 62 |
|
| 63 |
begin |
| 64 |
catch(:IRB_EXIT) do |
| 65 |
irb.eval_input |
| 66 |
end |
| 67 |
ensure |
| 68 |
IRB.irb_at_exit |
| 69 |
end |
| 70 |
end |
| 71 |
|
| 72 |
def to_s |
| 73 |
'HTTPClient' |
| 74 |
end |
| 75 |
end |
| 76 |
|
| 77 |
Runner.new.run |