jugyo-termtter 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -21,7 +21,7 @@ Run:
21
21
 
22
22
  == INSTALL:
23
23
 
24
- sudo gem install jugyo-termtter
24
+ sudo gem install jugyo-termtter -s http://gems.github.com
25
25
 
26
26
  == LICENSE:
27
27
 
data/bin/termtter CHANGED
@@ -2,9 +2,16 @@
2
2
 
3
3
  $KCODE = 'u'
4
4
 
5
- $:.unshift(File.dirname(__FILE__) + "/../lib")
5
+ self_file =
6
+ if File.ftype(__FILE__) == 'link'
7
+ File.readlink(__FILE__)
8
+ else
9
+ __FILE__
10
+ end
11
+ $:.unshift(File.dirname(self_file) + "/../lib")
6
12
 
7
13
  require 'termtter'
14
+ require 'termtter/standard_commands'
8
15
  require 'termtter/stdout'
9
16
  require 'configatron'
10
17
 
data/lib/termtter.rb CHANGED
@@ -10,20 +10,31 @@ $:.unshift(File.dirname(__FILE__)) unless
10
10
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
11
11
 
12
12
  module Termtter
13
- VERSION = '0.2.3'
14
-
13
+ VERSION = '0.3.0'
14
+
15
+ class CommandNotFound < StandardError; end
16
+
15
17
  class Client
16
-
18
+
17
19
  @@hooks = []
20
+ @@commands = {}
18
21
 
19
22
  def self.add_hook(&hook)
20
23
  @@hooks << hook
21
24
  end
22
25
 
23
- def self.clear_hook
26
+ def self.clear_hooks
24
27
  @@hooks.clear
25
28
  end
26
-
29
+
30
+ def self.add_command(regex, &block)
31
+ @@commands[regex] = block
32
+ end
33
+
34
+ def self.clear_commands
35
+ @@commands.clear
36
+ end
37
+
27
38
  attr_reader :since_id
28
39
 
29
40
  def initialize
@@ -51,7 +62,7 @@ module Termtter
51
62
  statuses = get_timeline("http://twitter.com/statuses/friends_timeline.xml")
52
63
  call_hooks(statuses, :list_friends_timeline)
53
64
  end
54
-
65
+
55
66
  def update_friends_timeline
56
67
  uri = "http://twitter.com/statuses/friends_timeline.xml"
57
68
  if @since_id && !@since_id.empty?
@@ -86,7 +97,7 @@ module Termtter
86
97
  call_hooks(statuses, :search)
87
98
  return statuses
88
99
  end
89
-
100
+
90
101
  def show(id)
91
102
  statuses = get_timeline("http://twitter.com/statuses/show/#{id}.xml")
92
103
  call_hooks(statuses, :show)
@@ -132,12 +143,45 @@ module Termtter
132
143
  return statuses
133
144
  end
134
145
 
146
+ def call_commands(text)
147
+ return if text.empty?
148
+
149
+ command_found = false
150
+ @@commands.each do |key, command|
151
+ if key =~ text
152
+ command_found = true
153
+ begin
154
+ command.call(self, $~)
155
+ rescue => e
156
+ puts "Error: #{e}"
157
+ puts e.backtrace.join("\n")
158
+ end
159
+ end
160
+ end
161
+
162
+ raise CommandNotFound unless command_found
163
+ end
164
+
165
+ def pause
166
+ @pause = true
167
+ end
168
+
169
+ def resume
170
+ @pause = false
171
+ @update.run
172
+ end
173
+
174
+ def exit
175
+ @update.kill
176
+ @input.kill
177
+ end
178
+
135
179
  def run
136
- pause = false
180
+ @pause = false
137
181
 
138
- update = Thread.new do
139
- while true
140
- if pause
182
+ @update = Thread.new do
183
+ loop do
184
+ if @pause
141
185
  Thread.stop
142
186
  end
143
187
  update_friends_timeline()
@@ -145,61 +189,13 @@ module Termtter
145
189
  end
146
190
  end
147
191
 
148
- input = Thread.new do
192
+ @input = Thread.new do
149
193
  while buf = Readline.readline("", true)
150
194
  begin
151
- case buf
152
- when ''
153
- # do nothing
154
- when 'debug'
155
- if @debug
156
- update_friends_timeline()
157
- end
158
- when /^(post|p)\s+(.*)/, /^(update|u)\s+(.*)/
159
- unless $2.empty?
160
- update_status($2)
161
- puts "=> #{$2}"
162
- end
163
- when /^(list|l)\s*$/
164
- list_friends_timeline()
165
- when /^(list|l)\s+([^\s]+)/
166
- get_user_timeline($2)
167
- when /^(search|s)\s+(.*)/
168
- unless $2.empty?
169
- search($2)
170
- end
171
- when /^(replies|r)\s*$/
172
- replies()
173
- when /^show\s+([^\s]+)/
174
- show($1)
175
- when /^pause\s*$/
176
- pause = true
177
- when /^resume\s*$/
178
- pause = false
179
- update.run
180
- when /^exit\s*$/
181
- update.kill
182
- input.kill
183
- when /^help\s*$/
184
- puts <<-EOS
185
- exit Exit
186
- help Print this help message
187
- list,l List the posts in your friends timeline
188
- list USERNAME List the posts in the the given user's timeline
189
- pause Pause updating
190
- update,u TEXT Post a new message
191
- resume Resume updating
192
- replies,r List the most recent @replies for the authenticating user
193
- search,s TEXT Search for Twitter
194
- show ID Show a single status
195
- update TEXT Update friends timeline
196
- EOS
197
- else
198
- puts <<-EOS
199
- Unknown command "#{buf}"
200
- Enter "help" for instructions
201
- EOS
202
- end
195
+ call_commands(buf)
196
+ rescue CommandNotFound => e
197
+ puts "Unknown command \"#{buf}\""
198
+ puts 'Enter "help" for instructions'
203
199
  rescue => e
204
200
  puts "Error: #{e}"
205
201
  puts e.backtrace.join("\n")
@@ -208,13 +204,13 @@ Enter "help" for instructions
208
204
  end
209
205
 
210
206
  stty_save = `stty -g`.chomp
211
- trap("INT") { system "stty", stty_save; exit }
207
+ trap("INT") { system "stty", stty_save; self.exit }
212
208
 
213
- input.join
209
+ @input.join
214
210
  end
215
211
 
216
212
  end
217
-
213
+
218
214
  class Status
219
215
  %w(
220
216
  id text created_at truncated in_reply_to_status_id in_reply_to_user_id
@@ -1,6 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'configatron'
3
3
  require 'test/unit'
4
+ require 'kagemusha'
4
5
  require File.dirname(__FILE__) + '/../lib/termtter'
5
6
 
6
7
  class TestTermtter < Test::Unit::TestCase
@@ -8,7 +9,7 @@ class TestTermtter < Test::Unit::TestCase
8
9
  configatron.user_name = 'test'
9
10
  configatron.password = 'test'
10
11
  @termtter = Termtter::Client.new
11
-
12
+
12
13
  Termtter::Client.add_hook do |statuses, event|
13
14
  @statuses = statuses
14
15
  @event = event
@@ -16,11 +17,7 @@ class TestTermtter < Test::Unit::TestCase
16
17
  end
17
18
 
18
19
  def test_get_timeline
19
- def @termtter.open(*arg)
20
- return File.open(File.dirname(__FILE__) + '/../test/friends_timeline.xml')
21
- end
22
-
23
- statuses = @termtter.get_timeline('')
20
+ statuses = swap_open('friends_timeline.xml') { @termtter.get_timeline('') }
24
21
 
25
22
  assert_equal 3, statuses.size
26
23
  assert_equal '102', statuses[0].user_id
@@ -34,22 +31,14 @@ class TestTermtter < Test::Unit::TestCase
34
31
  assert_equal 'texttext 0', statuses[2].text
35
32
  assert_equal 'Thu Dec 25 22:10:57 +0900 2008', statuses[2].created_at.to_s
36
33
  end
37
-
38
- def test_get_timeline_with_update_since_id
39
- def @termtter.open(*arg)
40
- return File.open(File.dirname(__FILE__) + '/../test/friends_timeline.xml')
41
- end
42
34
 
43
- statuses = @termtter.get_timeline('', true)
35
+ def test_get_timeline_with_update_since_id
36
+ statuses = swap_open('friends_timeline.xml') { @termtter.get_timeline('', true) }
44
37
  assert_equal '10002', @termtter.since_id
45
38
  end
46
-
47
- def test_search
48
- def @termtter.open(*arg)
49
- return File.open(File.dirname(__FILE__) + '/../test/search.atom')
50
- end
51
39
 
52
- statuses = @termtter.search('')
40
+ def test_search
41
+ statuses = swap_open('search.atom') { @termtter.search('') }
53
42
  assert_equal 3, statuses.size
54
43
  assert_equal 'test2', statuses[0].user_screen_name
55
44
  assert_equal 'Test User 2', statuses[0].user_name
@@ -60,23 +49,47 @@ class TestTermtter < Test::Unit::TestCase
60
49
  assert_equal 'texttext 0', statuses[2].text
61
50
  assert_equal 'Thu Dec 25 22:42:36 +0900 2008', statuses[2].created_at.to_s
62
51
  end
63
-
52
+
64
53
  def test_add_hook
65
- def @termtter.open(*arg)
66
- return File.open(File.dirname(__FILE__) + '/../test/search.atom')
67
- end
68
54
  call_hook = false
69
55
  Termtter::Client.add_hook do |statuses, event|
70
56
  call_hook = true
71
57
  end
72
- @termtter.search('')
73
-
58
+ swap_open('search.atom'){ @termtter.search('') }
59
+
74
60
  assert_equal true, call_hook
75
-
76
- Termtter::Client.clear_hook()
61
+
62
+ Termtter::Client.clear_hooks()
77
63
  call_hook = false
78
64
  @termtter.search('')
79
-
65
+
80
66
  assert_equal false, call_hook
81
67
  end
68
+
69
+ def test_add_command
70
+ command_text = nil
71
+ matche_text = nil
72
+ Termtter::Client.add_command /foo\s+(.*)/ do |termtter, matche|
73
+ command_text = matche[0]
74
+ matche_text = matche[1]
75
+ end
76
+
77
+ @termtter.call_commands('foo xxxxxxxxxxxxxx')
78
+ assert_equal 'foo xxxxxxxxxxxxxx', command_text
79
+ assert_equal 'xxxxxxxxxxxxxx', matche_text
80
+
81
+ Termtter::Client.clear_commands()
82
+ assert_raise Termtter::CommandNotFound do
83
+ @termtter.call_commands('foo xxxxxxxxxxxxxx')
84
+ end
85
+ end
86
+
87
+ def swap_open(name)
88
+ Kagemusha.new(Termtter::Client).def(:open) {
89
+ File.open(File.dirname(__FILE__) + "/../test/#{name}")
90
+ }.swap do
91
+ yield
92
+ end
93
+ end
94
+ private :swap_open
82
95
  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: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jugyo
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-12-28 00:00:00 -08:00
12
+ date: 2009-01-03 00:00:00 -08:00
13
13
  default_executable: termtter
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency