Class: Twat::Subcommands::Base

Inherits:
Object
  • Object
show all
Includes:
Exceptions
Defined in:
lib/twat/subcommands/base.rb

Instance Method Summary collapse

Methods included from Exceptions

#with_handled_exceptions

Constructor Details

#initialize(argv) ⇒ Base

Returns a new instance of Base.



19
20
21
# File 'lib/twat/subcommands/base.rb', line 19

def initialize(argv)
  @argv=argv
end

Instance Method Details

#argsObject



101
102
103
104
105
# File 'lib/twat/subcommands/base.rb', line 101

def args
  # This is dangerous, but realistically I don't see how else to parse the
  # args before creating an instance of a subclass
  @args ||= $args
end

#auth!Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/twat/subcommands/base.rb', line 46

def auth!
  Twitter.configure do |twit|
    config..each do |key, value|
      twit.send("#{key}=", value)
    end
    config.endpoint.consumer_info.each do |key, value|
      twit.send("#{key}=", value)
    end
    # twit.endpoint = config.endpoint.url
  end
end

#beepObject



58
59
60
# File 'lib/twat/subcommands/base.rb', line 58

def beep
  print "\a"
end

#configObject



93
94
95
# File 'lib/twat/subcommands/base.rb', line 93

def config
  @config ||= ::Twat::Config.new
end

#deentitize(text) ⇒ Object



86
87
88
89
90
91
# File 'lib/twat/subcommands/base.rb', line 86

def deentitize(text)
  {"&lt;" => "<", "&gt;" => ">", "&amp;" => "&", "&quot;" => '"' }.each do |k,v|
    text.gsub!(k, v)
  end
  text
end

#enable_readline!Object



107
108
109
# File 'lib/twat/subcommands/base.rb', line 107

def enable_readline!
  @reader = ReadlineNG::Reader.new
end

#format(twt, idx = nil) ⇒ Object

Format a tweet all pretty like



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/twat/subcommands/base.rb', line 67

def format(twt, idx = nil)
  idx = pad(idx) if idx
  text = deentitize(twt.text)
  if config.colors?
    buf = idx ? "#{idx.cyan}:" : ""
    if twt.as_user == config..to_s
      buf += "#{twt.as_user.bold.blue}: #{text}"
    elsif text.mentions?(config.)
      buf += "#{twt.as_user.bold.red}: #{text}"
    else
      buf += "#{twt.as_user.bold.cyan}: #{text}"
    end
    buf.colorise!
  else
    buf = idx ? "#{idx}: " : ""
    buf += "#{twt.as_user}: #{text}"
  end
end

#needs_arguments(n) ⇒ Object



29
30
31
32
33
# File 'lib/twat/subcommands/base.rb', line 29

def needs_arguments(n)
  unless @argv.length == n
    usage_and_exit!
  end
end

#needs_at_least(n) ⇒ Object



35
36
37
38
39
# File 'lib/twat/subcommands/base.rb', line 35

def needs_at_least(n)
  unless @argv.length >= n
    usage_and_exit!
  end
end

#optsObject



97
98
99
# File 'lib/twat/subcommands/base.rb', line 97

def opts
  @opts ||= ::Twat::Options.new
end

#pad(n) ⇒ Object



62
63
64
# File 'lib/twat/subcommands/base.rb', line 62

def pad(n)
  "%02d" % n
end

#readerObject



111
112
113
# File 'lib/twat/subcommands/base.rb', line 111

def reader
  @reader
end

#run!Object



23
24
25
26
27
# File 'lib/twat/subcommands/base.rb', line 23

def run!
  with_handled_exceptions(args) do
    run
  end
end

#usageObject



115
116
117
# File 'lib/twat/subcommands/base.rb', line 115

def usage
  puts self.class.usage
end

#usage_and_exit!Object



41
42
43
44
# File 'lib/twat/subcommands/base.rb', line 41

def usage_and_exit!
  usage
  exit
end