Class: Twtr::Console

Inherits:
Object
  • Object
show all
Includes:
Screen
Defined in:
lib/twtr/console.rb

Constant Summary collapse

CONFIG_FILE =
Twtr.is_win? ? File.expand_path("#{ENV['HOMEPATH']}/twtr/twtr.yml") \
: File.expand_path("~/.twtr/twtr.yml")
DEFAULT_DISPLAY_COUNT =
20
COUNT_WITH_UPDATE =
5
DEFAULT_DISPLAY_ENCODE =
Twtr.is_win? ? :sjis : :utf8
SUBCOMMANDS =
{
  :public_timeline => :public_timeline,
  :pt => :public_timeline,
  :friends_timeline => :friends_timeline,
  :ft => :friends_timeline,
  :user_timeline => :user_timeline,
  :ut => :user_timeline,
  :replies => :replies,
  :rp => :replies,
  :update => :update,
  :up => :update,
  :setting => :setting,
  :reset => :setting,
  :friends => :friends,
  :fds => :friends,
  :update_location => :update_location,
  :ul => :update_location,
  :rate_limit_status => :rate_limit_status,
  :rls => :rate_limit_status,
}

Instance Method Summary collapse

Methods included from Screen

included, #puts_with_enc

Instance Method Details

#edit_configObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/twtr/console.rb', line 96

def edit_config()
  puts ">> #{@configfile}"
  print "Edit twtr config? [y/N]:"; replay = gets
  exit if replay.to_s.chomp != 'y'

  config = {}

   = {}
  user = HighLine.new.ask("Twitter username: ")
  password = HighLine.new.ask("Twitter password: ") {|q| q.echo = '*' }
  ["user"] = user.to_s.chomp      
  ["password"] = password.to_s.chomp
  config["account"] =       

  proxy = {}
  print "Proxy host (option): "; proxy_host = gets
  print "Proxy port (option): "; proxy_port = gets
  proxy["host"] = proxy_host.chomp if proxy_host && !proxy_host.chomp.empty? 
  proxy["port"] = proxy_port.chomp if proxy_port && !proxy_port.chomp.empty?
  config["proxy"] = proxy unless proxy.empty? 
  
  Twtr::Config.save(@configfile, config)
end

#parse(args) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/twtr/console.rb', line 120

def parse(args)
  @params = {}
  args << '-h' if args.empty?
  args.options do |opt|

    opt.on('-v','--version','Show the version number and quit') do |val|
      puts("twtr #{Twtr::VERSION::STRING}")
    end
    
    opt.on('-h','--help','Show this help message and quit.') do |val|
      show_help(opt)
    end

    opt.on('-c','--count=number', 'Display number statuses.') do |val|
      @params.merge!({:count => val})
      @display_count = val.to_i
    end
    
    opt.on('-i','--id=userid','The ID or screen name of the user.') do |val|
      @params.merge!({:id => val})
    end

    opt.on('-m','--message=message','Post message.') do |val|
      @display_count ||= COUNT_WITH_UPDATE
      @params.merge!({:source => "twtrconsole", :status => val.toutf8})
    end

    opt.on('-l','--location=location','Post location.') do |val|
      @params.merge!({:source => "twtrconsole", :location => val.toutf8})
    end

    opt.on('-p', '--page=number',"Gets the 20 next statuses.") do |val|
      @params.merge!({:page => val.to_i})
    end
    
    opt.on('-C', '--config=path/to/configfile',"Use another config. (default: #{CONFIG_FILE})") do |val|
      @configfile = val
    end

    opt.parse!
  end

  return unless args[0]
  @subcommand = parse_subcommand(args.shift)
end

#parse_subcommand(cmd) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/twtr/console.rb', line 166

def parse_subcommand(cmd)
  cmd = cmd.to_sym
  if SUBCOMMANDS.has_key?(cmd)
    @subcommand = SUBCOMMANDS[cmd]
  else
    raise UnknownSubcommandException.new("Unkown Subcommand [#{cmd}]. Please type 'twtr -h'.")
  end
end

#run(args) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/twtr/console.rb', line 71

def run(args)
  begin
    parse(args)            
  rescue Exception => e
    puts e.message
  end

  if @subcommand
    @configfile ||= CONFIG_FILE
    (edit_config(); return) if @subcommand == :setting
    
    begin
      client = Twtr::Client.new(Twtr::Config.load(@configfile))
      @display_count ||= DEFAULT_DISPLAY_COUNT
      @source = client.send(@subcommand, @params)
      self.send("show_#{@subcommand}")
    rescue Twtr::ConfigFileNotFoundException => e
      puts e.message
      edit_config() 
    rescue Exception => e
      puts e.message
    end    
  end
end