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 bearer_tokens alias set)
DEFAULT_COMMAND =
'request'
PATH_PATTERN =
/^\/\w+/
PROTOCOL_PATTERN =
/^\w+:\/\//
README =
File.dirname(__FILE__) + '/../../README.md'

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
42
43
# 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 'bearer_tokens'
                 AppOnlyTokenInformationController
               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



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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/twurl/cli.rb', line 45

def parse_options(args)
  Twurl.options         = Options.new
  Twurl.options.args    = args.dup
  Twurl.options.trace   = false
  Twurl.options.data    = {}
  Twurl.options.headers = {}
  Twurl.options.upload  = {}
  Twurl.options.upload['file'] = []

  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
      consumer_key
      consumer_secret
      access_token
      token_secret
    end

    o.section "Common options:" do
      trace
      data
      raw_data
      headers
      host
      quiet
      disable_ssl
      request_method
      help
      version
      proxy
      file
      filefield
      base64
      json_format
      timeout
      connection_timeout
      app_only
    end
  end

  begin
    arguments               = option_parser.parse!(args)
  rescue OptionParser::InvalidOption
    CLI.puts "ERROR: undefined option"
    exit
  rescue
    CLI.puts "ERROR: invalid argument"
    exit
  end
  Twurl.options.command     = extract_command!(arguments)
  Twurl.options.path        = extract_path!(arguments)
  Twurl.options.subcommands = arguments
  
  if Twurl.options.command == DEFAULT_COMMAND and Twurl.options.path.nil? and Twurl.options.args.empty?
    CLI.puts option_parser
    raise NoPathFound, "No path found"
  end

  Twurl.options
end


127
128
129
130
# File 'lib/twurl/cli.rb', line 127

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

.prompt_for(label) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/twurl/cli.rb', line 137

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



132
133
134
135
# File 'lib/twurl/cli.rb', line 132

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