earthquake 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -91,13 +91,12 @@ The 'm' is a MatchData.
91
91
  TODO
92
92
  ----
93
93
 
94
- * plugin system
95
94
  * filter
96
- * reconnect
97
- * show more events
98
- * show retweeted status id
95
+ * plugin system
99
96
  * keyword tracking
100
97
  * more intelligent completion
98
+ * spec
99
+ * typable id
101
100
 
102
101
  Copyright
103
102
  ----
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
data/earthquake.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{earthquake}
8
- s.version = "0.2.2"
8
+ s.version = "0.2.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["jugyo"]
@@ -11,6 +11,7 @@ module Earthquake
11
11
 
12
12
  command :restart do
13
13
  puts 'restarting...'
14
+ stop
14
15
  exec File.expand_path('../../..//bin/earthquake', __FILE__)
15
16
  end
16
17
 
@@ -95,5 +96,9 @@ module Earthquake
95
96
  command :report_spam do |m|
96
97
  twitter.report_spam(m[1])
97
98
  end
99
+
100
+ command :reconnect do
101
+ reconnect
102
+ end
98
103
  end
99
104
  end
@@ -54,58 +54,90 @@ module Earthquake
54
54
 
55
55
  def start(*argv)
56
56
  _init
57
+ restore_history
57
58
 
58
- Thread.start do
59
- while buf = Readline.readline("⚡ ", true)
60
- input(buf.strip)
59
+ EventMachine::run do
60
+ Thread.start do
61
+ while buf = Readline.readline("⚡ ", true)
62
+ Readline::HISTORY.pop if buf.empty?
63
+ input(buf.strip)
64
+ end
61
65
  end
62
- end
63
66
 
64
- Thread.start do
65
- loop do
66
- if Readline.line_buffer.nil? || Readline.line_buffer.empty?
67
- output
68
- sleep 1
69
- else
70
- sleep 2
67
+ Thread.start do
68
+ loop do
69
+ if Readline.line_buffer.nil? || Readline.line_buffer.empty?
70
+ output
71
+ sleep 1
72
+ else
73
+ sleep 2
74
+ end
71
75
  end
72
76
  end
77
+
78
+ reconnect
79
+
80
+ trap('TERM') { stop }
73
81
  end
82
+ end
83
+
84
+ def reconnect
85
+ item_queue.clear
86
+ start_stream(:host => 'userstream.twitter.com', :path => '/2/user.json', :ssl => true)
87
+ end
88
+
89
+ def start_stream(options)
90
+ stop_stream
74
91
 
75
- EventMachine::run {
76
- @stream = ::Twitter::JSONStream.connect(
77
- :ssl => true,
78
- :host => 'userstream.twitter.com',
79
- :path => '/2/user.json',
80
- :oauth => config.slice(:consumer_key, :consumer_secret).merge(:access_key => config[:token], :access_secret => config[:secret])
92
+ options = {
93
+ :oauth => config.slice(:consumer_key, :consumer_secret).merge(
94
+ :access_key => config[:token], :access_secret => config[:secret]
81
95
  )
96
+ }.merge(options)
82
97
 
83
- @stream.each_item do |item|
84
- item_queue << JSON.parse(item)
85
- end
98
+ @stream = ::Twitter::JSONStream.connect(options)
86
99
 
87
- @stream.on_error do |message|
88
- $stdout.print "error: #{message}\n"
89
- $stdout.flush
90
- end
100
+ @stream.each_item do |item|
101
+ item_queue << JSON.parse(item)
102
+ end
91
103
 
92
- @stream.on_reconnect do |timeout, retries|
93
- $stdout.print "reconnecting in: #{timeout} seconds\n"
94
- $stdout.flush
95
- end
104
+ @stream.on_error do |message|
105
+ notify "error: #{message}"
106
+ end
96
107
 
97
- @stream.on_max_reconnects do |timeout, retries|
98
- $stdout.print "Failed after #{retries} failed reconnects\n"
99
- $stdout.flush
100
- end
108
+ @stream.on_reconnect do |timeout, retries|
109
+ notify "reconnecting in: #{timeout} seconds"
110
+ end
101
111
 
102
- trap('TERM') { stop }
103
- }
112
+ @stream.on_max_reconnects do |timeout, retries|
113
+ notify "Failed after #{retries} failed reconnects"
114
+ end
115
+ end
116
+
117
+ def stop_stream
118
+ @stream.stop if @stream
104
119
  end
105
120
 
106
121
  def stop
107
- @stream.stop
108
- EventMachine.stop if EventMachine.reactor_running?
122
+ stop_stream
123
+ EventMachine.stop_event_loop
124
+ store_history
125
+ end
126
+
127
+ def store_history
128
+ history_size = config[:history_size] || 1000
129
+ File.open(File.join(config[:dir], 'history'), 'w') do |file|
130
+ lines = Readline::HISTORY.to_a[([Readline::HISTORY.size - history_size, 0].max)..-1]
131
+ puts lines
132
+ file.print(lines.join("\n"))
133
+ end
134
+ end
135
+
136
+ def restore_history
137
+ history_file = File.join(config[:dir], 'history')
138
+ if File.exists?(history_file)
139
+ File.read(history_file).split(/\n/).each { |line| Readline::HISTORY << line }
140
+ end
109
141
  end
110
142
 
111
143
  def notify(message)
@@ -59,7 +59,13 @@ module Earthquake
59
59
  output_hander do |item|
60
60
  next unless item["text"]
61
61
 
62
- misc = (item["in_reply_to_status_id"] ? " (reply to #{item["in_reply_to_status_id"]})" : "")
62
+ if item["in_reply_to_status_id"]
63
+ misc = " (reply to #{item["in_reply_to_status_id"]})"
64
+ elsif item["retweeted_status"]
65
+ misc = " (retweet of #{item["retweeted_status"]["id"]})"
66
+ else
67
+ misc = ""
68
+ end
63
69
  source = item["source"] =~ />(.*)</ ? $1 : 'web'
64
70
  user_color = color_of(item["user"]["screen_name"])
65
71
  text = item["text"].e.gsub(/[@#]([0-9A-Za-z_]+)/) do |i|
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: earthquake
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.2
5
+ version: 0.2.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - jugyo