From 0986b4e64f46f23a60a6d1bbd40a85f6a05643c6 Mon Sep 17 00:00:00 2001 From: Bob Aman Date: Wed, 20 Oct 2010 23:49:15 +0000 Subject: [PATCH] Refactored CLI tool to be more maintainable. git-svn-id: https://google-api-ruby-client.googlecode.com/svn/trunk@89 c1d61fac-ed7f-fcc1-18f7-ff78120a04ef --- bin/google-api | 555 +++++++++++++++++++++------------------ lib/google/api_client.rb | 1 + 2 files changed, 298 insertions(+), 258 deletions(-) diff --git a/bin/google-api b/bin/google-api index 6ba4d3925..791f71825 100755 --- a/bin/google-api +++ b/bin/google-api @@ -10,96 +10,28 @@ OAUTH_SERVER_PORT = 12736 require 'rubygems' require 'optparse' +require 'httpadapter' +require 'webrick' require 'google/api_client/version' require 'google/api_client' ARGV.unshift('--help') if ARGV.empty? -command = 'execute' -options = {} -OptionParser.new do |opts| - opts.banner = - "Usage: google-api [options] -- \n" + - " or: google-api --oauth-login= [options]\n" + - " or: google-api --interactive= [options]\n" + - " or: google-api --fuzz [options]" +module Google + class APIClient + class CLI + # Used for oauth login + class OAuthVerifierServlet < WEBrick::HTTPServlet::AbstractServlet + attr_reader :verifier - opts.separator "" - - opts.on( - "--oauth-login ", String, "Authorize for the scope") do |s| - if command != 'execute' - STDERR.puts("Ambiguous command: #{command}") - exit(1) - end - command = 'oauth-login' - options[:scope] = s - end - opts.on( - "-s", "--service ", String, "Perform discovery on service") do |s| - options[:service_name] = s - end - opts.on( - "-i", "--interactive ", String, "Start interactive session") do |s| - if command != 'execute' - STDERR.puts("Ambiguous command: #{command}") - exit(1) - end - command = 'interactive' - options[:service_name] = s - end - opts.on( - "--service-version ", String, "Select service version") do |id| - options[:service_version] = id - end - opts.on( - "--content-type ", String, "Content-Type for request") do |f| - # Resolve content type shortcuts - case f - when 'json' - f = 'application/json' - when 'xml' - f = 'application/xml' - when 'atom' - f = 'application/atom+xml' - when 'rss' - f = 'application/rss+xml' - end - options[:content_type] = f - end - opts.on("--fuzz [rpcname]", String, "Fuzz an API or endpoint") do |rpcname| - if command != 'execute' - STDERR.puts("Ambiguous command: #{command}") - exit(1) - end - command = 'fuzz' - options[:fuzz] = rpcname - end - - opts.on_tail("-v", "--verbose", "Run verbosely") do |v| - options[:verbose] = v - end - opts.on_tail("-h", "--help", "Show this message") do - puts opts - exit - end - opts.on_tail("--version", "Show version") do - puts "google-api-client (#{Google::APIClient::VERSION::STRING})" - exit - end -end.parse! - -if command == 'oauth-login' # Guard to keep start-up time short - require 'webrick' - # Used for oauth login - class OAuthVerifierServlet < WEBrick::HTTPServlet::AbstractServlet - def do_GET(request, response) - $verifier ||= Addressable::URI.unencode_component( - request.request_uri.to_s[/\?.*oauth_verifier=([^&$]+)(&|$)/, 1] - ) - response.status = WEBrick::HTTPStatus::RC_ACCEPTED - # This javascript will auto-close the tab after the verifier is obtained. - response.body = <<-HTML + def do_GET(request, response) + $verifier ||= Addressable::URI.unencode_component( + request.request_uri.to_s[/\?.*oauth_verifier=([^&$]+)(&|$)/, 1] + ) + response.status = WEBrick::HTTPStatus::RC_ACCEPTED + # This javascript will auto-close the tab after the + # verifier is obtained. + response.body = <<-HTML