Class: Twurl::CLI

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

Defined Under Namespace

Modules: AvailableOptions

Constant Summary collapse

SUPPORTED_COMMANDS =
%w(authorize accounts alias set)
DEFAULT_COMMAND =
'request'
PATH_PATTERN =
/^\/\w+/
README =
File.dirname(__FILE__) + '/../../README'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.outputObject

Returns the value of attribute output.



10
11
12
# File 'lib/twurl/cli.rb', line 10

def output
  @output
end

Class Method Details

.dispatch(options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/twurl/cli.rb', line 17

def dispatch(options)
  client     = OAuthClient.load_from_options(options)
  controller = case options.command
               when 'authorize'
                 AuthorizationController
               when 'accounts'
                 AccountInformationController
               when 'alias'
                 AliasesController
               when 'set'
                 ConfigurationController
               when 'request'
                 RequestController
               end
  controller.dispatch(client, options)
rescue Twurl::Exception => exception
  abort(exception.message)
end

.parse_options(args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/twurl/cli.rb', line 36

def parse_options(args)
  arguments = args.dup

  Twurl.options         = Options.new
  Twurl.options.trace   = false
  Twurl.options.data    = {}

  option_parser = OptionParser.new do |o|
    o.extend AvailableOptions

    o.banner = "Usage: twurl authorize -u username -p password --consumer-key HQsAGcVm5MQT3n6j7qVJw --consumer-secret asdfasd223sd2\n" +
               "       twurl [options] /statuses/home_timeline.xml\n"                                                                  +
               "\n"                                                                                                                    +
               "Supported Commands:\n#{SUPPORTED_COMMANDS.sort.join(', ')}"

    o.section "Getting started:" do
      tutorial
    end

    o.section "Authorization options:" do
      username
      password
      consumer_key
      consumer_secret
      access_token
      token_secret
    end

    o.section "Common options:" do
      trace
      data
      host
      quiet
      disable_ssl
      request_method
      help
    end
  end

  arguments                 = option_parser.parse!(args)
  Twurl.options.command     = extract_command!(arguments)
  Twurl.options.path        = extract_path!(arguments)
  Twurl.options.subcommands = arguments
  Twurl.options
end

.prompt_for(label) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/twurl/cli.rb', line 86

def prompt_for(label)
  system "stty -echo"
  print "#{label}: "
  result = STDIN.gets.chomp
  CLI.puts
  result
rescue Interrupt
  exit
ensure
  system "stty echo"
end

.puts(*args, &block) ⇒ Object



82
83
84
# File 'lib/twurl/cli.rb', line 82

def puts(*args, &block)
  output.puts(*args, &block)
end

.run(args) ⇒ Object



12
13
14
15
# File 'lib/twurl/cli.rb', line 12

def run(args)
  options = parse_options(args)
  dispatch(options)
end