lib/twurl/cli.rb in twurl-0.9.6 vs lib/twurl/cli.rb in twurl-0.9.7
- old
+ new
@@ -4,21 +4,19 @@
DEFAULT_COMMAND = 'request'
PATH_PATTERN = /^\/\w+/
PROTOCOL_PATTERN = /^\w+:\/\//
README = File.dirname(__FILE__) + '/../../README.md'
@output ||= STDOUT
- class NoPathFound < Exception
- end
class << self
attr_accessor :output
def run(args)
begin
options = parse_options(args)
- rescue NoPathFound => e
- exit
+ rescue Twurl::Exception => exception
+ abort(exception.message)
end
dispatch(options)
end
def dispatch(options)
@@ -78,11 +76,10 @@
data
raw_data
headers
host
quiet
- disable_ssl
request_method
help
version
proxy
file
@@ -96,23 +93,23 @@
end
begin
arguments = option_parser.parse!(args)
rescue OptionParser::InvalidOption
- CLI.puts "ERROR: undefined option"
- exit
+ raise Exception "ERROR: undefined option"
+ rescue Twurl::Exception
+ raise
rescue
- CLI.puts "ERROR: invalid argument"
- exit
+ raise Exception "ERROR: invalid argument"
end
Twurl.options.command = extract_command!(arguments)
Twurl.options.path = extract_path!(arguments)
Twurl.options.subcommands = arguments
if Twurl.options.command == DEFAULT_COMMAND and Twurl.options.path.nil? and Twurl.options.args.empty?
CLI.puts option_parser
- raise NoPathFound, "No path found"
+ raise Exception, "No path found"
end
Twurl.options
end
@@ -170,11 +167,11 @@
path
end
def escape_params(params)
CGI::parse(params).map do |key, value|
- "#{CGI.escape key}=#{CGI.escape value.first}"
+ "#{CGI.escape(key)}=#{CGI.escape(value.first)}"
end.join("&")
end
end
module AvailableOptions
@@ -232,26 +229,30 @@
end
end
def data
on('-d', '--data [data]', 'Sends the specified data in a POST request to the HTTP server.') do |data|
- if options.args.count { |item| /content-type: (.*)/i.match(item) } > 0
- options.data[data] = nil
+ if options.args.count { |item| /^content-type:\s+application\/json/i.match(item) } > 0
+ options.json_data = true
+ options.data = data
else
- data.split('&').each do |pair|
- key, value = pair.split('=', 2)
- options.data[key] = value
+ CGI.parse(data).each_pair do |key, value|
+ options.data[key] = value.first
end
end
end
end
def raw_data
on('-r', '--raw-data [data]', 'Sends the specified data as it is in a POST request to the HTTP server.') do |data|
- CGI::parse(data).each_pair do |key, value|
- options.data[key] = value.first
+ if options.raw_data
+ raise Exception, "ERROR: can't specify '-r' option more than once"
+ elsif options.args.include?('-d') || options.args.include?('--data')
+ raise Exception, "ERROR: can't use '-r' and '-d' options together"
end
+ options.raw_data = true
+ options.data = data
end
end
def headers
on('-A', '--header [header]', 'Adds the specified header to the request to the HTTP server.') do |header|
@@ -275,16 +276,10 @@
on('-q', '--quiet', 'Suppress all output (default: output is printed to STDOUT)') do |quiet|
options.output = StringIO.new
end
end
- def disable_ssl
- on('-U', '--no-ssl', 'Disable SSL (default: SSL is enabled)') do |use_ssl|
- options.protocol = 'http'
- end
- end
-
def request_method
on('-X', '--request-method [method]', 'Request method (default: GET)') do |request_method|
options.request_method = request_method.downcase
end
end
@@ -296,11 +291,11 @@
end
end
def version
on_tail("-v", "--version", "Show version") do
- CLI.puts Version
+ CLI.puts "twurl version: #{Version}\nplatform: #{RUBY_ENGINE} #{RUBY_VERSION} (#{RUBY_PLATFORM})"
exit
end
end
def proxy
@@ -370,13 +365,9 @@
end
end
def base_url
"#{protocol}://#{host}"
- end
-
- def ssl?
- protocol == 'https'
end
def debug_output_io
super || STDERR
end