Class: OAuth::CLI
- Inherits:
-
Object
- Object
- OAuth::CLI
- Defined in:
- lib/oauth/cli.rb
Constant Summary collapse
- SUPPORTED_COMMANDS =
%w(sign)
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
Instance Method Summary collapse
- #execute(stdout, arguments = []) ⇒ Object
- #extract_command_and_parse_options(arguments) ⇒ Object
- #parse_options(arguments) ⇒ Object
- #prepare_parameters ⇒ Object
- #sufficient_options? ⇒ Boolean
- #usage ⇒ Object
- #valid_command? ⇒ Boolean
- #verbose? ⇒ Boolean
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
8 9 10 |
# File 'lib/oauth/cli.rb', line 8 def command @command end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/oauth/cli.rb', line 9 def @options end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
10 11 12 |
# File 'lib/oauth/cli.rb', line 10 def stdout @stdout end |
Class Method Details
.execute(stdout, arguments = []) ⇒ Object
12 13 14 |
# File 'lib/oauth/cli.rb', line 12 def self.execute(stdout, arguments = []) self.new.execute(stdout, arguments) end |
Instance Method Details
#execute(stdout, arguments = []) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/oauth/cli.rb', line 16 def execute(stdout, arguments = []) @stdout = stdout (arguments) if && valid_command? case command when "sign" request = OAuth::RequestProxy.proxy \ "method" => [:method], "uri" => [:uri], "parameters" => prepare_parameters # can't pass options unless they respond to :secret, so use this alternative signature = OAuth::Signature.sign \ request, :consumer_secret => [:oauth_consumer_secret], :token_secret => [:oauth_token_secret] do |request| # while we have access to the request being signed, display some internals if verbose? stdout.puts "Method: #{request.method}" stdout.puts "URI: #{request.uri}" stdout.puts "Normalized params: #{request.normalized_parameters}" stdout.puts "Signature base string: #{request.signature_base_string}" end end if verbose? stdout.puts "Signature: #{signature}" stdout.puts "Escaped signature: #{OAuth::Helper.escape(signature)}" else stdout.puts signature end end else usage end end |
#extract_command_and_parse_options(arguments) ⇒ Object
55 56 57 58 |
# File 'lib/oauth/cli.rb', line 55 def (arguments) @command = arguments[-1] (arguments[0..-1]) end |
#parse_options(arguments) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/oauth/cli.rb', line 60 def (arguments) @options = {} OptionParser.new do |opts| opts. = "Usage: #{$0} [options] <command>" # defaults [:oauth_signature_method] = "HMAC-SHA1" opts.on("--consumer-key KEY", "Specifies the consumer key to use.") do |v| [:oauth_consumer_key] = v end opts.on("--consumer-secret SECRET", "Specifies the consumer secret to use.") do |v| [:oauth_consumer_secret] = v end opts.on("--method METHOD", "Specifies the method (e.g. GET) to use when signing.") do |v| [:method] = v end opts.on("--parameters PARAMS", "Specifies the parameters to use when signing.") do |v| [:params] = v end opts.on("--signature-method METHOD", "Specifies the signature method to use; defaults to HMAC-SHA1.") do |v| [:oauth_signature_method] = v end opts.on("--secret SECRET", "Specifies the token secret to use.") do |v| [:oauth_token_secret] = v end opts.on("--token TOKEN", "Specifies the token to use.") do |v| [:oauth_token] = v end opts.on("--uri URI", "Specifies the URI to use when signing.") do |v| [:uri] = v end opts.on("-v", "--verbose", "Be verbose.") do [:verbose] = true end end.parse!(arguments) end |
#prepare_parameters ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/oauth/cli.rb', line 106 def prepare_parameters { "oauth_consumer_key" => [:oauth_consumer_key], "oauth_token" => [:oauth_token], "oauth_signature_method" => [:oauth_signature_method] }.merge(CGI.parse([:params])) end |
#sufficient_options? ⇒ Boolean
114 115 116 |
# File 'lib/oauth/cli.rb', line 114 def [:oauth_consumer_key] && [:oauth_consumer_secret] && [:method] && [:uri] end |
#usage ⇒ Object
118 119 120 |
# File 'lib/oauth/cli.rb', line 118 def usage stdout.puts "Should be generated by OptionParser" end |
#valid_command? ⇒ Boolean
122 123 124 |
# File 'lib/oauth/cli.rb', line 122 def valid_command? SUPPORTED_COMMANDS.include?(command) end |
#verbose? ⇒ Boolean
126 127 128 |
# File 'lib/oauth/cli.rb', line 126 def verbose? [:verbose] end |