Class: Twat::Subcommands::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Exceptions

#with_handled_exceptions

Constructor Details

#initialize(argv) ⇒ Base

Returns a new instance of Base.



21
22
23
# File 'lib/twat/subcommands/base.rb', line 21

def initialize(argv)
  @argv=argv
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

Instance Method Details

#argsObject



103
104
105
106
107
# File 'lib/twat/subcommands/base.rb', line 103

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



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

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



60
61
62
# File 'lib/twat/subcommands/base.rb', line 60

def beep
  print "\a"
end

#configObject



95
96
97
# File 'lib/twat/subcommands/base.rb', line 95

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

#deentitize(text) ⇒ Object



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

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

#enable_readline!Object



109
110
111
# File 'lib/twat/subcommands/base.rb', line 109

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

#format(twt, idx = nil) ⇒ Object

Format a tweet all pretty like



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

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



31
32
33
34
35
# File 'lib/twat/subcommands/base.rb', line 31

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

#needs_at_least(n) ⇒ Object



37
38
39
40
41
# File 'lib/twat/subcommands/base.rb', line 37

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

#pad(n) ⇒ Object



64
65
66
# File 'lib/twat/subcommands/base.rb', line 64

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

#readerObject



113
114
115
# File 'lib/twat/subcommands/base.rb', line 113

def reader
  @reader
end

#run!Object



25
26
27
28
29
# File 'lib/twat/subcommands/base.rb', line 25

def run!
  with_handled_exceptions(args) do
    run
  end
end

#usageObject



117
118
119
# File 'lib/twat/subcommands/base.rb', line 117

def usage
  puts self.class.usage
end

#usage_and_exit!Object



43
44
45
46
# File 'lib/twat/subcommands/base.rb', line 43

def usage_and_exit!
  usage
  exit
end