Class: OAuth::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth/cli.rb

Constant Summary collapse

SUPPORTED_COMMANDS =
%w(sign)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



8
9
10
# File 'lib/oauth/cli.rb', line 8

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/oauth/cli.rb', line 9

def options
  @options
end

#stdoutObject (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
  extract_command_and_parse_options(arguments)

  if sufficient_options? && valid_command?
    case command
    when "sign"
      request = OAuth::RequestProxy.proxy \
         "method"     => options[:method],
         "uri"        => options[:uri],
         "parameters" => prepare_parameters

      # can't pass options unless they respond to :secret, so use this alternative
      signature = OAuth::Signature.sign \
        request,
        :consumer_secret => options[:oauth_consumer_secret],
        :token_secret    => options[: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 extract_command_and_parse_options(arguments)
  @command = arguments[-1]
  parse_options(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 parse_options(arguments)
  @options = {}
  OptionParser.new do |opts|
    opts.banner = "Usage: #{$0} [options] <command>"

    # defaults
    options[:oauth_signature_method] = "HMAC-SHA1"

    opts.on("--consumer-key KEY", "Specifies the consumer key to use.") do |v|
      options[:oauth_consumer_key] = v
    end

    opts.on("--consumer-secret SECRET", "Specifies the consumer secret to use.") do |v|
      options[:oauth_consumer_secret] = v
    end

    opts.on("--method METHOD", "Specifies the method (e.g. GET) to use when signing.") do |v|
      options[:method] = v
    end

    opts.on("--parameters PARAMS", "Specifies the parameters to use when signing.") do |v|
      options[:params] = v
    end

    opts.on("--signature-method METHOD", "Specifies the signature method to use; defaults to HMAC-SHA1.") do |v|
      options[:oauth_signature_method] = v
    end

    opts.on("--secret SECRET", "Specifies the token secret to use.") do |v|
      options[:oauth_token_secret] = v
    end

    opts.on("--token TOKEN", "Specifies the token to use.") do |v|
      options[:oauth_token] = v
    end

    opts.on("--uri URI", "Specifies the URI to use when signing.") do |v|
      options[:uri] = v
    end

    opts.on("-v", "--verbose", "Be verbose.") do
      options[:verbose] = true
    end
  end.parse!(arguments)
end

#prepare_parametersObject



106
107
108
109
110
111
112
# File 'lib/oauth/cli.rb', line 106

def prepare_parameters
  {
    "oauth_consumer_key"     => options[:oauth_consumer_key],
    "oauth_token"            => options[:oauth_token],
    "oauth_signature_method" => options[:oauth_signature_method]
  }.merge(CGI.parse(options[:params]))
end

#sufficient_options?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/oauth/cli.rb', line 114

def sufficient_options?
  options[:oauth_consumer_key] && options[:oauth_consumer_secret] && options[:method] && options[:uri]
end

#usageObject



118
119
120
# File 'lib/oauth/cli.rb', line 118

def usage
  stdout.puts "Should be generated by OptionParser"
end

#valid_command?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/oauth/cli.rb', line 122

def valid_command?
  SUPPORTED_COMMANDS.include?(command)
end

#verbose?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/oauth/cli.rb', line 126

def verbose?
  options[:verbose]
end