Class: Flumtter::Client
- Inherits:
-
Object
show all
- Includes:
- Util
- Defined in:
- lib/flumtter/app/core/client.rb
Defined Under Namespace
Classes: NoSetAccount
Constant Summary
collapse
- @@events =
[]
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Util
#command_value_regexp, #dialog_for_index, #error, #error_handler, #id2obj, #if_tweet, #index_regexp, #index_with_dialog, #logger, #on_event, #parse_index, #parse_time, #sarastire, #sarastire_user, #screen_name_regexp
Constructor Details
#initialize(account) ⇒ Client
Returns a new instance of Client.
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/flumtter/app/core/client.rb', line 22
def initialize(account)
debug if Setting[:debug]
@queue = Queue.new
@mutex = Mutex.new
set(account)
Cli.run(self)
start
Keyboard.input(self)
rescue NoSetAccount
STDERR.puts "Error: Account Not Found".color
exit
end
|
Instance Attribute Details
#account ⇒ Object
Returns the value of attribute account.
8
9
10
|
# File 'lib/flumtter/app/core/client.rb', line 8
def account
@account
end
|
#ethread ⇒ Object
Returns the value of attribute ethread.
8
9
10
|
# File 'lib/flumtter/app/core/client.rb', line 8
def ethread
@ethread
end
|
#rest ⇒ Object
Returns the value of attribute rest.
8
9
10
|
# File 'lib/flumtter/app/core/client.rb', line 8
def rest
@rest
end
|
#sthread ⇒ Object
Returns the value of attribute sthread.
8
9
10
|
# File 'lib/flumtter/app/core/client.rb', line 8
def sthread
@sthread
end
|
Class Method Details
.on_event(*filter, &blk) ⇒ Object
11
12
13
|
# File 'lib/flumtter/app/core/client.rb', line 11
def self.on_event(*filter,&blk)
@@events << [filter, blk]
end
|
Instance Method Details
#callback(object, events = nil) ⇒ Object
15
16
17
18
19
20
|
# File 'lib/flumtter/app/core/client.rb', line 15
def callback(object, events=nil)
events ||= object.type(@account)
@@events.each do |filter, blk|
blk.call(object, self) if (filter - events).empty?
end
end
|
#execute ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/flumtter/app/core/client.rb', line 62
def execute
@ethread = Thread.new do
loop do
handler do
object = @queue.pop
begin
@mutex.lock
callback(object)
ensure
@mutex.unlock
end
end
end
end
end
|
#kill ⇒ Object
78
79
80
81
82
|
# File 'lib/flumtter/app/core/client.rb', line 78
def kill
@sthread.kill
@ethread.kill
@queue.clear
end
|
#pause ⇒ Object
84
85
86
|
# File 'lib/flumtter/app/core/client.rb', line 84
def pause
@mutex.lock
end
|
#resume ⇒ Object
88
89
90
|
# File 'lib/flumtter/app/core/client.rb', line 88
def resume
@mutex.unlock
end
|
#set(account) ⇒ Object
35
36
37
38
39
40
|
# File 'lib/flumtter/app/core/client.rb', line 35
def set(account)
raise NoSetAccount if account.nil?
@account = account
@rest = Twitter::REST::Client.new(@account.keys)
@stream = Twitter::Streaming::Client.new(@account.keys)
end
|
#start ⇒ Object
42
43
44
45
|
# File 'lib/flumtter/app/core/client.rb', line 42
def start
Flumtter.callback(:init, self)
stream unless Setting[:non_stream]
end
|
#stream ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/flumtter/app/core/client.rb', line 47
def stream
execute
@sthread = Thread.new do
puts "#{@account.screen_name}'s stream_start!"
"#{@account.screen_name} #{TITLE}".title
begin
@stream.user(Setting[:stream_option] || {}) do |object|
@queue.push object
end
rescue Twitter::Error::TooManyRequests => e
puts e.class.to_s.color
end
end
end
|