jugyo-termtter 1.0.7 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/lib/termtter/api.rb CHANGED
@@ -13,28 +13,43 @@ config.set_default(:enable_ssl, false)
13
13
  module Termtter
14
14
  module API
15
15
  class << self
16
- attr_reader :connection, :twitter, :twitter_old
16
+ attr_reader :connection, :twitter
17
17
  def setup
18
18
  @connection = Connection.new
19
- @twitter = Rubytter.new(
20
- config.user_name,
21
- config.password,
22
- {
23
- :app_name => Termtter::APP_NAME,
24
- :host => config.host,
25
- :header => {
26
- 'User-Agent' => 'Termtter http://github.com/jugyo/termtter',
27
- 'X-Twitter-Client' => 'Termtter',
28
- 'X-Twitter-Client-URL' => 'http://github.com/jugyo/termtter',
29
- 'X-Twitter-Client-Version' => Termtter::VERSION
30
- },
31
- :enable_ssl => config.enable_ssl,
32
- :proxy_host => config.proxy.host,
33
- :proxy_port => config.proxy.port,
34
- :proxy_user_name => config.proxy.user_name,
35
- :proxy_password => config.proxy.password
36
- }
37
- )
19
+ @twitter = create_twitter(config.user_name, config.password)
20
+ end
21
+
22
+ def restore_user
23
+ @twitter = create_twitter(config.user_name, config.password)
24
+ end
25
+
26
+ def switch_user(username = nil)
27
+ highline = create_highline
28
+ username = highline.ask('your twitter username: ') if username.nil? || username.empty?
29
+ password = highline.ask('your twitter password: ') { |q| q.echo = false }
30
+ @twitter = create_twitter(username, password)
31
+ end
32
+
33
+ def create_twitter(user_name, password)
34
+ Rubytter.new(
35
+ user_name,
36
+ password,
37
+ {
38
+ :app_name => config.app_name.empty? ? Termtter::APP_NAME : config.app_name,
39
+ :host => config.host,
40
+ :header => {
41
+ 'User-Agent' => 'Termtter http://github.com/jugyo/termtter',
42
+ 'X-Twitter-Client' => 'Termtter',
43
+ 'X-Twitter-Client-URL' => 'http://github.com/jugyo/termtter',
44
+ 'X-Twitter-Client-Version' => Termtter::VERSION
45
+ },
46
+ :enable_ssl => config.enable_ssl,
47
+ :proxy_host => config.proxy.host,
48
+ :proxy_port => config.proxy.port,
49
+ :proxy_user_name => config.proxy.user_name,
50
+ :proxy_password => config.proxy.password
51
+ }
52
+ )
38
53
  end
39
54
  end
40
55
  end
@@ -1,6 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require 'fileutils'
3
3
  require 'logger'
4
+ require 'termcolor'
4
5
 
5
6
  module Termtter
6
7
 
@@ -148,7 +149,7 @@ module Termtter
148
149
 
149
150
  @task_manager.invoke_and_wait do
150
151
  begin
151
- call_hooks("pre_exec_#{command.name.to_s}", command_str, modified_arg)
152
+ call_hooks("pre_exec_#{command.name.to_s}", command, modified_arg)
152
153
  # exec command
153
154
  result = command.call(command_str, modified_arg, text)
154
155
  if result
@@ -216,7 +217,7 @@ module Termtter
216
217
  end
217
218
 
218
219
  def post_config_load()
219
- if config.system.devel
220
+ if config.devel
220
221
  plugin 'devel'
221
222
  end
222
223
  end
@@ -309,7 +310,30 @@ module Termtter
309
310
  end
310
311
 
311
312
  def setup_logger
312
- @logger = config.logger || Logger.new(STDOUT)
313
+ @logger = config.logger || default_logger
314
+ end
315
+
316
+ def default_logger
317
+ logger = Logger.new(STDOUT)
318
+ logger.formatter = lambda { |severity, time, progname, message|
319
+ color =
320
+ case severity
321
+ when /^DEBUG/
322
+ 'blue'
323
+ when /^INFO/
324
+ 'cyan'
325
+ when /^WARN/
326
+ 'magenta'
327
+ when /^ERROR/
328
+ 'red'
329
+ when /^FATAL/
330
+ 'on_red'
331
+ else
332
+ 'white'
333
+ end
334
+ TermColor.parse("<#{color}>" + TermColor.escape("[#{severity}] #{message}\n") + "</#{color}>")
335
+ }
336
+ logger
313
337
  end
314
338
 
315
339
  def run
@@ -328,6 +352,12 @@ module Termtter
328
352
  end
329
353
 
330
354
  def handle_error(e)
355
+ if logger
356
+ logger.error("#{e.class.to_s}: #{e.message}")
357
+ logger.error(e.backtrace.join("\n")) if config.devel
358
+ else
359
+ raise e
360
+ end
331
361
  get_hooks(:on_error).each {|hook| hook.call(e) }
332
362
  rescue Exception => e
333
363
  puts "Error: #{e}"
@@ -36,9 +36,9 @@ module Termtter
36
36
  end
37
37
 
38
38
  def complement(input)
39
- command_str, command_arg = Command.split_command_line(input)
40
- if command_arg
39
+ if match?(input) && input =~ /^[^\s]+\s/
41
40
  if completion_proc
41
+ command_str, command_arg = Command.split_command_line(input)
42
42
  [completion_proc.call(command_str, command_arg || '')].flatten.compact
43
43
  else
44
44
  []
@@ -48,7 +48,7 @@ module Termtter
48
48
  end
49
49
  end
50
50
 
51
- def call(cmd, arg, original_text)
51
+ def call(cmd = nil, arg = nil, original_text = nil)
52
52
  arg = case arg
53
53
  when nil
54
54
  ''
@@ -78,4 +78,3 @@ module Termtter
78
78
  end
79
79
  end
80
80
  end
81
-
@@ -47,6 +47,10 @@ module Termtter
47
47
  @store[name] == :undefined ? @store[name] = Termtter::Config.new : @store[name]
48
48
  end
49
49
 
50
+ def __values__
51
+ @store.dup
52
+ end
53
+
50
54
  __instance = self.new
51
55
  (class << self; self end).
52
56
  __send__(:define_method, :instance) { __instance }
@@ -2,6 +2,7 @@
2
2
 
3
3
  module Termtter
4
4
  module ConfigSetup
5
+ module_function
5
6
  def run
6
7
  ui = create_highline
7
8
  username = ui.ask('your twitter username: ')
@@ -33,21 +34,5 @@ module Termtter
33
34
  puts "generated: ~/.termtter/config"
34
35
  puts "enjoy!"
35
36
  end
36
-
37
- module_function :run
38
-
39
- def self.create_highline
40
- HighLine.track_eof = false
41
- if $stdin.respond_to?(:getbyte) # for ruby1.9
42
- require 'delegate'
43
- stdin_for_highline = SimpleDelegator.new($stdin)
44
- def stdin_for_highline.getc
45
- getbyte
46
- end
47
- else
48
- stdin_for_highline = $stdin
49
- end
50
- HighLine.new(stdin_for_highline)
51
- end
52
37
  end
53
38
  end
@@ -144,3 +144,18 @@ unless Symbol.instance_methods.include?('to_proc')
144
144
  end
145
145
  end
146
146
  end
147
+
148
+ require 'highline'
149
+ def create_highline
150
+ HighLine.track_eof = false
151
+ if $stdin.respond_to?(:getbyte) # for ruby1.9
152
+ require 'delegate'
153
+ stdin_for_highline = SimpleDelegator.new($stdin)
154
+ def stdin_for_highline.getc
155
+ getbyte
156
+ end
157
+ else
158
+ stdin_for_highline = $stdin
159
+ end
160
+ HighLine.new(stdin_for_highline)
161
+ end
@@ -53,7 +53,7 @@ module Termtter
53
53
  synchronize do
54
54
  begin
55
55
  yield
56
- rescue => e
56
+ rescue Exception => e
57
57
  Termtter::Client.handle_error(e)
58
58
  end
59
59
  end
@@ -1,4 +1,4 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  module Termtter
3
- VERSION = '1.0.7'
3
+ VERSION = '1.0.8'
4
4
  end
@@ -21,4 +21,3 @@ module Termtter
21
21
  end
22
22
  end
23
23
  end
24
-
@@ -7,6 +7,7 @@ module Termtter
7
7
  describe Client do
8
8
 
9
9
  it 'should take command' do
10
+ Client.setup_logger
10
11
  command = Command.new(:name => :test)
11
12
  Client.register_command(command)
12
13
  Client.get_command(:test).should == command
@@ -207,28 +208,9 @@ module Termtter
207
208
  end
208
209
 
209
210
  it 'should handle error' do
210
- $stderr, old = StringIO.new, $stderr
211
+ logger = Client.instance_eval{@logger}
212
+ logger.should_receive(:error).with("StandardError: error")
211
213
  Client.handle_error StandardError.new('error')
212
- $stderr.string.should == "[ERROR] Something wrong: error\n"
213
- $stderr = old
214
- end
215
-
216
- it 'should handle Rubytte::APIError' do
217
- $stderr, old = StringIO.new, $stderr
218
- class Rubytter::APIError < StandardError; end
219
- [
220
- ['401', "[ERROR] Unauthorized: maybe you tried to show protected user status\n"],
221
- ['403', "[ERROR] Access denied: maybe that user is protected\n"],
222
- ['404', "[ERROR] Not found: maybe there is no such user\n"],
223
- ].each do |code, message|
224
- res = mock('res', :code => code)
225
- excep = Rubytter::APIError.new('error')
226
- excep.should_receive(:response).and_return(res)
227
- Client.handle_error excep
228
- $stderr.string.should == message
229
- $stderr.string = ''
230
- end
231
- $stderr = old
232
214
  end
233
215
 
234
216
  it 'should cancel command by hook' do
@@ -244,5 +226,30 @@ module Termtter
244
226
  command.should_not_receive(:call)
245
227
  Client.call_commands('test')
246
228
  end
229
+
230
+ it 'should get default help' do
231
+ $stdout, old_stdout = StringIO.new, $stdout # FIXME That suspends any debug informations!
232
+ help_command = Client.get_command(:help)
233
+ help_command.should_not be_nil
234
+ help_command.call
235
+ $stdout.string.should_not == '' # 何がか出力されていること
236
+ $stdout = old_stdout
237
+ end
238
+
239
+ it 'should get an added help' do
240
+ Client.register_command(
241
+ :name => :foo,
242
+ :help => [
243
+ ['foo USER', 'foo to USER'],
244
+ ['foo list', 'list foo'],
245
+ ]
246
+ )
247
+ $stdout, old_stdout = StringIO.new, $stdout # FIXME That suspends any debug informations!
248
+ help_command = Client.get_command(:help)
249
+ help_command.should_not be_nil
250
+ help_command.call
251
+ $stdout.string.should match(/foo USER/)
252
+ $stdout.string.should match(/foo list/)
253
+ end
247
254
  end
248
255
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jugyo-termtter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - jugyo
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-03-21 00:00:00 -07:00
13
+ date: 2009-04-08 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -68,14 +68,18 @@ files:
68
68
  - lib/plugins/april_fool.rb
69
69
  - lib/plugins/bomb.rb
70
70
  - lib/plugins/clear.rb
71
+ - lib/plugins/command_plus.rb
71
72
  - lib/plugins/confirm.rb
72
73
  - lib/plugins/cool.rb
74
+ - lib/plugins/countter.rb
73
75
  - lib/plugins/devel.rb
76
+ - lib/plugins/direct_messages.rb
74
77
  - lib/plugins/en2ja.rb
75
78
  - lib/plugins/english.rb
76
79
  - lib/plugins/erb.rb
80
+ - lib/plugins/exec.rb
81
+ - lib/plugins/exec_and_update.rb
77
82
  - lib/plugins/expand-tinyurl.rb
78
- - lib/plugins/favorite.rb
79
83
  - lib/plugins/fib.rb
80
84
  - lib/plugins/fib_filter.rb
81
85
  - lib/plugins/filter.rb
@@ -83,10 +87,13 @@ files:
83
87
  - lib/plugins/grass.rb
84
88
  - lib/plugins/group.rb
85
89
  - lib/plugins/growl.rb
90
+ - lib/plugins/growl2.rb
86
91
  - lib/plugins/hatebu.rb
92
+ - lib/plugins/hatebu_and_update.rb
87
93
  - lib/plugins/history.rb
88
94
  - lib/plugins/ignore.rb
89
95
  - lib/plugins/keyword.rb
96
+ - lib/plugins/l2.rb
90
97
  - lib/plugins/log.rb
91
98
  - lib/plugins/me.rb
92
99
  - lib/plugins/modify_arg_hook_sample.rb
@@ -94,10 +101,11 @@ files:
94
101
  - lib/plugins/multi_reply.rb
95
102
  - lib/plugins/notify-send.rb
96
103
  - lib/plugins/notify-send2.rb
104
+ - lib/plugins/notify-send3.rb
105
+ - lib/plugins/open_url.rb
97
106
  - lib/plugins/otsune.rb
98
107
  - lib/plugins/outputz.rb
99
108
  - lib/plugins/pause.rb
100
- - lib/plugins/plugin.rb
101
109
  - lib/plugins/post_exec_hook_sample.rb
102
110
  - lib/plugins/pre_exec_hook_sample.rb
103
111
  - lib/plugins/primes.rb
@@ -106,6 +114,7 @@ files:
106
114
  - lib/plugins/reblog.rb
107
115
  - lib/plugins/reload.rb
108
116
  - lib/plugins/reply.rb
117
+ - lib/plugins/retweet.rb
109
118
  - lib/plugins/reverse.rb
110
119
  - lib/plugins/say.rb
111
120
  - lib/plugins/scrape.rb
@@ -120,9 +129,12 @@ files:
120
129
  - lib/plugins/storage/status.rb
121
130
  - lib/plugins/storage/status_mook.rb
122
131
  - lib/plugins/storage.rb
132
+ - lib/plugins/switch_user.rb
123
133
  - lib/plugins/system_status.rb
124
134
  - lib/plugins/timer.rb
135
+ - lib/plugins/tinyurl.rb
125
136
  - lib/plugins/translation.rb
137
+ - lib/plugins/typable_id.rb
126
138
  - lib/plugins/update_editor.rb
127
139
  - lib/plugins/uri-open.rb
128
140
  - lib/plugins/wassr_post.rb
@@ -144,11 +156,9 @@ files:
144
156
  - lib/termtter.rb
145
157
  - spec/plugins/cool_spec.rb
146
158
  - spec/plugins/english_spec.rb
147
- - spec/plugins/favorite_spec.rb
148
159
  - spec/plugins/fib_spec.rb
149
160
  - spec/plugins/filter_spec.rb
150
161
  - spec/plugins/pause_spec.rb
151
- - spec/plugins/plugin_spec.rb
152
162
  - spec/plugins/primes_spec.rb
153
163
  - spec/plugins/shell_spec.rb
154
164
  - spec/plugins/sl_spec.rb
@@ -1,63 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- def create_favorite(id)
4
- r = Termtter::API.twitter.favorite id
5
- puts "Favorited status ##{r.id} on user @#{r.user.screen_name} #{r.text}"
6
- end
7
-
8
- module Termtter::Client
9
- register_command(
10
- :name => :favorite, :aliases => [:fav],
11
- :exec_proc => lambda {|arg|
12
- id = 0
13
- case arg
14
- when /^\d+/
15
- id = arg.to_i
16
- when /^@([A-Za-z0-9_]+)/
17
- user = $1
18
- statuses = Termtter::API.twitter.user_timeline(user)
19
- return if statuses.empty?
20
- id = statuses[0].id
21
- when /^\/(.*)$/
22
- word = $1
23
- raise "Not implemented yet."
24
- else
25
- return
26
- end
27
-
28
- create_favorite id
29
- },
30
- :completion_proc => lambda {|cmd, arg|
31
- case arg
32
- when /@(.*)/
33
- find_user_candidates $1, "#{cmd} @%s"
34
- when /(\d+)/
35
- find_status_ids(arg).map{|id| "#{cmd} #{$1}"}
36
- else
37
- %w(favorite).grep(/^#{Regexp.quote arg}/)
38
- end
39
- },
40
- :help => ['favorite,fav (ID|@USER|/WORD)', 'Favorite a status']
41
- )
42
-
43
- # TBD: Implement this when database support comes.
44
- #
45
- # if public_storage[:log]
46
- # add_help 'favorite,fav /WORD', 'Favorite a status by searching'
47
- #
48
- # add_command %r'^(?:favorite|fav)\s+/(.+)$' do |m, t|
49
- # pat = Regexp.new(m[1])
50
- # statuses = public_storage[:log].select {|s| pat =~ s.text }
51
- # if statuses.size == 1
52
- # status = statuses.first
53
- # res = t.favorite(status.id)
54
- # if res.code == '200'
55
- # puts %Q(Favorited "#{status.user.screen_name}: #{status.text}")
56
- # else
57
- # puts "Failed: #{res}"
58
- # end
59
- # else
60
- # puts "#{pat} does not match single status"
61
- # end
62
- # end
63
- end
@@ -1,53 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- module Termtter::Client
4
-
5
- public_storage[:plugins] = (Dir["#{File.dirname(__FILE__)}/*.rb"] + Dir["#{Termtter::CONF_DIR}/plugins/*.rb"]).map do |f|
6
- f.match(%r|([^/]+).rb$|)[1]
7
- end
8
-
9
- register_command(
10
- :name => :plugin, :aliases => [],
11
- :exec_proc => lambda {|arg|
12
- begin
13
- result = plugin arg.strip
14
- rescue LoadError
15
- ensure
16
- puts "=> #{result.inspect}"
17
- end
18
- },
19
- :completion_proc => lambda {|cmd, args|
20
- find_user_candidates args, "#{cmd} %s"
21
- unless args.empty?
22
- find_plugin_candidates args, "#{cmd} %s"
23
- else
24
- public_storage[:plugins].sort
25
- end
26
- },
27
- :help => ['plugin FILE', 'Load a plugin']
28
- )
29
-
30
- register_command(
31
- :name => :plugins, :aliases => [],
32
- :exec_proc => lambda {|arg|
33
- puts public_storage[:plugins].sort.join("\n")
34
- },
35
- :help => ['plugins', 'Show list of plugins']
36
- )
37
-
38
- def self.find_plugin_candidates(a, b)
39
- public_storage[:plugins].
40
- grep(/^#{Regexp.quote a}/i).
41
- map {|u| b % u }
42
- end
43
- end
44
-
45
- # plugin.rb
46
- # a dynamic plugin loader
47
- # example
48
- # > u <%= not erbed %>
49
- # => <%= not erbed %>
50
- # > plugin erb
51
- # => true
52
- # > u <%= 1 + 2 %>
53
- # => 3
@@ -1,10 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- require File.dirname(__FILE__) + '/../spec_helper'
4
-
5
- describe Termtter::Client, 'when the plugin favorite is loaded' do
6
- it 'should add command favorite' do
7
- Termtter::Client.should_receive(:register_command).once
8
- plugin 'favorite'
9
- end
10
- end
@@ -1,25 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- require File.dirname(__FILE__) + '/../spec_helper'
4
-
5
- module Termtter
6
- describe Client, 'when the plugin plugin is loaded' do
7
- it 'should add command plugin and plugins' do
8
- Termtter::Client.should_receive(:register_command).twice
9
- plugin 'plugin'
10
- end
11
-
12
- it 'should set public_storage[:plugins]' do
13
- plugin 'plugin'
14
- Client::public_storage[:plugins].should_not be_empty
15
- end
16
-
17
- describe 'after the plugin plugin is loaded' do
18
- before { plugin 'plugin' }
19
-
20
- it 'should load the given plugin in the command plugin'
21
- # hmm... How can I write it...?
22
- end
23
- end
24
- end
25
-