class OAuth::TTY::Command
Attributes
options[R]
Public Class Methods
new(stdout, stdin, stderr, arguments)
click to toggle source
# File lib/oauth/tty/command.rb 6 def initialize(stdout, stdin, stderr, arguments) 7 @stdout = stdout 8 @stdin = stdin 9 @stderr = stderr 10 11 @options = {} 12 option_parser.parse!(arguments) 13 end
Public Instance Methods
required_options()
click to toggle source
# File lib/oauth/tty/command.rb 25 def required_options 26 [] 27 end
run()
click to toggle source
# File lib/oauth/tty/command.rb 15 def run 16 missing = required_options - options.keys 17 if missing.empty? 18 _run 19 else 20 show_missing(missing) 21 puts option_parser.help 22 end 23 end
Protected Instance Methods
_option_parser_common(opts)
click to toggle source
# File lib/oauth/tty/command.rb 121 def _option_parser_common(opts) 122 ## Common Options 123 124 opts.on("-B", "--body", "Use the request body for OAuth parameters.") do 125 options[:scheme] = :body 126 end 127 128 opts.on("--consumer-key KEY", "Specifies the consumer key to use.") do |v| 129 options[:oauth_consumer_key] = v 130 end 131 132 opts.on("--consumer-secret SECRET", "Specifies the consumer secret to use.") do |v| 133 options[:oauth_consumer_secret] = v 134 end 135 136 opts.on("-H", "--header", "Use the 'Authorization' header for OAuth parameters (default).") do 137 options[:scheme] = :header 138 end 139 140 opts.on("-Q", "--query-string", "Use the query string for OAuth parameters.") do 141 options[:scheme] = :query_string 142 end 143 144 opts.on("-O", "--options FILE", "Read options from a file") do |v| 145 require "shellwords" 146 arguments = File.open(v).readlines.flat_map { |l| Shellwords.shellsplit(l.chomp) } 147 options2 = parse_options(arguments) 148 options.merge!(options2) 149 end 150 end
_option_parser_defaults()
click to toggle source
# File lib/oauth/tty/command.rb 110 def _option_parser_defaults 111 options[:oauth_nonce] = OAuth::Helper.generate_key 112 options[:oauth_signature_method] = "HMAC-SHA1" 113 options[:oauth_timestamp] = OAuth::Helper.generate_timestamp 114 options[:oauth_version] = "1.0" 115 options[:method] = :post 116 options[:params] = [] 117 options[:scheme] = :header 118 options[:version] = "1.0" 119 end
_option_parser_sign_and_query(opts)
click to toggle source
# File lib/oauth/tty/command.rb 152 def _option_parser_sign_and_query(opts) 153 opts.separator("\n options for signing and querying") 154 155 opts.on("--method METHOD", "Specifies the method (e.g. GET) to use when signing.") do |v| 156 options[:method] = v 157 end 158 159 opts.on("--nonce NONCE", "Specifies the nonce to use.") do |v| 160 options[:oauth_nonce] = v 161 end 162 163 opts.on("--parameters PARAMS", "Specifies the parameters to use when signing.") do |v| 164 options[:params] << v 165 end 166 167 opts.on("--signature-method METHOD", "Specifies the signature method to use; defaults to HMAC-SHA1.") do |v| 168 options[:oauth_signature_method] = v 169 end 170 171 opts.on("--token TOKEN", "Specifies the token to use.") do |v| 172 options[:oauth_token] = v 173 end 174 175 opts.on("--secret SECRET", "Specifies the token secret to use.") do |v| 176 options[:oauth_token_secret] = v 177 end 178 179 opts.on("--timestamp TIMESTAMP", "Specifies the timestamp to use.") do |v| 180 options[:oauth_timestamp] = v 181 end 182 183 opts.on("--realm REALM", "Specifies the realm to use.") do |v| 184 options[:realm] = v 185 end 186 187 opts.on("--uri URI", "Specifies the URI to use when signing.") do |v| 188 options[:uri] = v 189 end 190 191 opts.on("--version [VERSION]", "Specifies the OAuth version to use.") do |v| 192 options[:oauth_version] = v 193 end 194 195 opts.on("--no-version", "Omit oauth_version.") do 196 options[:oauth_version] = nil 197 end 198 199 opts.on("--xmpp", "Generate XMPP stanzas.") do 200 options[:xmpp] = true 201 options[:method] ||= "iq" 202 end 203 204 opts.on("-v", "--verbose", "Be verbose.") do 205 options[:verbose] = true 206 end 207 end
alert(string = nil)
click to toggle source
# File lib/oauth/tty/command.rb 50 def alert(string = nil) 51 @stderr.puts(string) 52 end
option_parser()
click to toggle source
# File lib/oauth/tty/command.rb 80 def option_parser 81 @option_parser ||= OptionParser.new do |opts| 82 opts.banner = "Usage: oauth <command> [ARGS]" 83 84 _option_parser_defaults 85 _option_parser_common(opts) 86 _option_parser_sign_and_query(opts) 87 _option_parser_authorization(opts) 88 end 89 end
parameters()
click to toggle source
# File lib/oauth/tty/command.rb 54 def parameters 55 @parameters ||= begin 56 escaped_pairs = options[:params].collect do |pair| 57 if pair.to_s.include?(":") 58 Hash[*pair.split(":", 2)].collect do |k, v| 59 [CGI.escape(k.strip), CGI.escape(v.strip)].join("=") 60 end 61 else 62 pair 63 end 64 end 65 66 querystring = escaped_pairs * "&" 67 cli_params = CGI.parse(querystring) 68 69 { 70 "oauth_consumer_key" => options[:oauth_consumer_key], 71 "oauth_nonce" => options[:oauth_nonce], 72 "oauth_timestamp" => options[:oauth_timestamp], 73 "oauth_token" => options[:oauth_token], 74 "oauth_signature_method" => options[:oauth_signature_method], 75 "oauth_version" => options[:oauth_version], 76 }.reject { |_k, v| v.nil? || v == "" }.merge(cli_params) 77 end 78 end
parse_options(arguments)
click to toggle source
Parse an array of CLI-like arguments into an options hash without mutating current state This is used by the -O/–options FILE feature to load args from a file and merge them
# File lib/oauth/tty/command.rb 93 def parse_options(arguments) 94 original_options = @options 95 begin 96 temp_options = {} 97 @options = temp_options 98 _option_parser_defaults 99 OptionParser.new do |opts| 100 _option_parser_common(opts) 101 _option_parser_sign_and_query(opts) 102 _option_parser_authorization(opts) 103 end.parse!(arguments) 104 temp_options 105 ensure 106 @options = original_options 107 end 108 end
puts(string = nil)
click to toggle source
# File lib/oauth/tty/command.rb 46 def puts(string = nil) 47 @stdout.puts(string) 48 end
show_missing(array)
click to toggle source
# File lib/oauth/tty/command.rb 33 def show_missing(array) 34 array = array.map { |s| "--#{s}" }.join(" ") 35 OAuth::TTY::CLI.puts_red("Options missing to OAuth CLI: #{array}") 36 end
verbose?()
click to toggle source
# File lib/oauth/tty/command.rb 42 def verbose? 43 options[:verbose] 44 end
xmpp?()
click to toggle source
# File lib/oauth/tty/command.rb 38 def xmpp? 39 options[:xmpp] 40 end