Class: Twurl::CLI

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

Defined Under Namespace

Modules: AvailableOptions Classes: NoPathFound

Constant Summary collapse

SUPPORTED_COMMANDS =
%w(authorize accounts alias set)
DEFAULT_COMMAND =
'request'
PATH_PATTERN =
/^\/\w+/
PROTOCOL_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.



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

def output
  @output
end

Class Method Details

.dispatch(options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/twurl/cli.rb', line 24

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



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/twurl/cli.rb', line 43

def parse_options(args)
  arguments = args.dup

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

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

    o.banner = <<-BANNER
Usage: twurl authorize --consumer-key key --consumer-secret secret
 twurl [options] /1.1/statuses/home_timeline.json

Supported Commands: #{SUPPORTED_COMMANDS.sort.join(', ')}
    BANNER

    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
      headers
      host
      quiet
      disable_ssl
      request_method
      help
      version
      proxy
    end
  end

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

  if Twurl.options.command == DEFAULT_COMMAND and Twurl.options.path.nil?
    CLI.puts option_parser
    raise NoPathFound, "No path found"
  end

  Twurl.options.subcommands = arguments
  Twurl.options
end


109
110
111
112
# File 'lib/twurl/cli.rb', line 109

def print(*args, &block)
  output.print(*args, &block)
  output.flush if output.respond_to?(:flush)
end

.prompt_for(label) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/twurl/cli.rb', line 119

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

.puts(*args, &block) ⇒ Object



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

def puts(*args, &block)
  output.puts(*args, &block)
  output.flush if output.respond_to?(:flush)
end

.run(args) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/twurl/cli.rb', line 15

def run(args)
  begin
    options = parse_options(args)
  rescue NoPathFound => e
    exit
  end
  dispatch(options)
end