class Twurl::CLI
Constants
- DEFAULT_COMMAND
- PATH_PATTERN
- PROTOCOL_PATTERN
- README
- SUPPORTED_COMMANDS
Attributes
output[RW]
Public Class Methods
dispatch(options)
click to toggle source
# File lib/twurl/cli.rb 24 def dispatch(options) 25 client = OAuthClient.load_from_options(options) 26 controller = case options.command 27 when 'authorize' 28 AuthorizationController 29 when 'accounts' 30 AccountInformationController 31 when 'bearer_tokens' 32 AppOnlyTokenInformationController 33 when 'alias' 34 AliasesController 35 when 'set' 36 ConfigurationController 37 when 'request' 38 RequestController 39 end 40 controller.dispatch(client, options) 41 rescue Twurl::Exception => exception 42 abort(exception.message) 43 end
parse_options(args)
click to toggle source
# File lib/twurl/cli.rb 45 def parse_options(args) 46 Twurl.options = Options.new 47 Twurl.options.args = args.dup 48 Twurl.options.trace = false 49 Twurl.options.data = {} 50 Twurl.options.headers = {} 51 Twurl.options.upload = {} 52 Twurl.options.upload['file'] = [] 53 54 option_parser = OptionParser.new do |o| 55 o.extend AvailableOptions 56 57 o.banner = <<-BANNER 58 Usage: twurl authorize --consumer-key key --consumer-secret secret 59 twurl [options] /1.1/statuses/home_timeline.json 60 61 Supported Commands: #{SUPPORTED_COMMANDS.sort.join(', ')} 62 BANNER 63 64 o.section "Getting started:" do 65 tutorial 66 end 67 68 o.section "Authorization options:" do 69 username 70 consumer_key 71 consumer_secret 72 access_token 73 token_secret 74 end 75 76 o.section "Common options:" do 77 trace 78 data 79 raw_data 80 headers 81 host 82 quiet 83 disable_ssl 84 request_method 85 help 86 version 87 proxy 88 file 89 filefield 90 base64 91 json_format 92 timeout 93 connection_timeout 94 app_only 95 end 96 end 97 98 begin 99 arguments = option_parser.parse!(args) 100 rescue OptionParser::InvalidOption 101 CLI.puts "ERROR: undefined option" 102 exit 103 rescue 104 CLI.puts "ERROR: invalid argument" 105 exit 106 end 107 Twurl.options.command = extract_command!(arguments) 108 Twurl.options.path = extract_path!(arguments) 109 Twurl.options.subcommands = arguments 110 111 if Twurl.options.command == DEFAULT_COMMAND and Twurl.options.path.nil? and Twurl.options.args.empty? 112 CLI.puts option_parser 113 raise NoPathFound, "No path found" 114 end 115 116 Twurl.options 117 end
print(*args, &block)
click to toggle source
# File lib/twurl/cli.rb 127 def print(*args, &block) 128 output.print(*args, &block) 129 output.flush if output.respond_to?(:flush) 130 end
prompt_for(label)
click to toggle source
# File lib/twurl/cli.rb 137 def prompt_for(label) 138 system "stty -echo" 139 CLI.print "#{label}: " 140 result = STDIN.gets.chomp 141 CLI.puts 142 result 143 rescue Interrupt 144 exit 145 ensure 146 system "stty echo" 147 end
puts(*args, &block)
click to toggle source
# File lib/twurl/cli.rb 132 def puts(*args, &block) 133 output.puts(*args, &block) 134 output.flush if output.respond_to?(:flush) 135 end
run(args)
click to toggle source
# File lib/twurl/cli.rb 15 def run(args) 16 begin 17 options = parse_options(args) 18 rescue NoPathFound => e 19 exit 20 end 21 dispatch(options) 22 end
Private Class Methods
escape_params(params)
click to toggle source
# File lib/twurl/cli.rb 173 def escape_params(params) 174 CGI::parse(params).map do |key, value| 175 "#{CGI.escape key}=#{CGI.escape value.first}" 176 end.join("&") 177 end
extract_command!(arguments)
click to toggle source
# File lib/twurl/cli.rb 150 def extract_command!(arguments) 151 if SUPPORTED_COMMANDS.include?(arguments.first) 152 arguments.shift 153 else 154 DEFAULT_COMMAND 155 end 156 end
extract_path!(arguments)
click to toggle source
# File lib/twurl/cli.rb 158 def extract_path!(arguments) 159 path = nil 160 arguments.each_with_index do |argument, index| 161 if argument[PATH_PATTERN] 162 path_with_params = arguments.slice!(index) 163 path, params = path_with_params.split("?", 2) 164 if params 165 path += "?" + escape_params(params) 166 end 167 break 168 end 169 end 170 path 171 end