Module: Tweetwine::CLI

Defined in:
lib/tweetwine/cli.rb

Defined Under Namespace

Classes: Command, FollowersCommand, FriendsCommand, HelpCommand, HomeCommand, MentionsCommand, SearchCommand, UpdateCommand, UserCommand, VersionCommand

Constant Summary collapse

DEFAULT_COMMAND =
:home
DEFAULT_CONFIG =
{
  :colors       => :false,
  :config_file  => "#{(ENV['HOME'] || ENV['USERPROFILE'])}/.tweetwine",
  :env_lookouts => [:http_proxy],
  :excludes     => [:command],
  :num_tweets   => 20,
  :page         => 1,
  :show_reverse => false,
  :shorten_urls => {:disable => true},
  :username     => ENV['USER']
}.freeze
EXEC_NAME =
'tweetwine'

Class Method Summary collapse

Class Method Details

.commandsObject



49
50
51
52
53
54
# File 'lib/tweetwine/cli.rb', line 49

def commands
  @commands ||= {
    :primaries    => {},
    :secondaries  => {}
  }
end

.configObject



25
26
27
# File 'lib/tweetwine/cli.rb', line 25

def config
  @config ||= read_config
end

.find_command(name) ⇒ Object



61
62
63
64
# File 'lib/tweetwine/cli.rb', line 61

def find_command(name)
  name = name.to_sym
  commands[:primaries][name] || commands[:secondaries][name]
end

.global_option_parserObject



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
# File 'lib/tweetwine/cli.rb', line 66

def global_option_parser
  @global_option_parser ||= OptionParser.new do |parser, options|
    parser.on '-c', '--colors',                     'Enable ANSI colors for output.' do
      options[:colors] = true
    end
    parser.on '-f', '--config <file>',    String,   "Configuration file (default #{DEFAULT_CONFIG[:config_file]})." do |arg|
      options[:config_file] = arg
    end
    parser.on '-h', '--help',                       'Show this help and exit.' do
      options[:command] = :help
    end
    parser.on       '--http-proxy <url>', String,   'Enable HTTP(S) proxy.' do |arg|
      options[:http_proxy] = arg
    end
    parser.on       '--no-colors',                  'Disable ANSI colors for output.' do
      options[:colors] = false
    end
    parser.on       '--no-http-proxy',              'Disable HTTP(S) proxy.' do
      options[:http_proxy] = nil
    end
    parser.on       '--no-url-shorten',             'Disable URL shortening.' do
      options[:shorten_urls] ||= {}
      options[:shorten_urls][:disable] = true
    end
    parser.on '-n', '--num <n>',          Integer,  "Number of tweets per page (default #{DEFAULT_CONFIG[:num_tweets]})." do |arg|
      options[:num_tweets] = arg
    end
    parser.on '-p', '--page <p>',         Integer,  "Page number for tweets (default #{DEFAULT_CONFIG[:page]})." do |arg|
      options[:page] = arg
    end
    parser.on '-r', '--reverse',                    "Show tweets in reverse order (default #{DEFAULT_CONFIG[:show_reverse]})." do
      options[:show_reverse] = true
    end
    parser.on '-u', '--username <user>',  String,   "User to authenticate (default '#{DEFAULT_CONFIG[:username]}')." do |arg|
      options[:username] = arg
    end
    parser.on '-v', '--version',                    "Show version and exit." do
      options[:command] = :version
    end
  end
end

.httpObject



29
30
31
# File 'lib/tweetwine/cli.rb', line 29

def http
  @http ||= Http::Client.new(config)
end

.oauthObject



33
34
35
# File 'lib/tweetwine/cli.rb', line 33

def oauth
  @oauth ||= OAuth.new(config[:oauth_access])
end

.register_command(cmd_class, names) ⇒ Object



56
57
58
59
# File 'lib/tweetwine/cli.rb', line 56

def register_command(cmd_class, names)
  commands[:primaries][names.first.to_sym] = cmd_class
  names[1..-1].each { |name| commands[:secondaries][name.to_sym] = cmd_class }
end

.start(args = ARGV, overriding_default_conf = nil) ⇒ Object



20
21
22
23
# File 'lib/tweetwine/cli.rb', line 20

def start(args = ARGV, overriding_default_conf = nil)
  init(args, overriding_default_conf)
  run(args)
end

.twitterObject



37
38
39
# File 'lib/tweetwine/cli.rb', line 37

def twitter
  @twitter ||= Twitter.new(config)
end

.uiObject



41
42
43
# File 'lib/tweetwine/cli.rb', line 41

def ui
  @ui ||= UI.new(config)
end

.url_shortenerObject



45
46
47
# File 'lib/tweetwine/cli.rb', line 45

def url_shortener
  @url_shorterer ||= UrlShortener.new(config[:shorten_urls])
end