class Flumtter::Client

Attributes

account[R]
ethread[R]
rest[R]
sthread[R]

Public Class Methods

new(account) click to toggle source
# 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
on_event(*filter,&blk) click to toggle source
# File lib/flumtter/app/core/client.rb, line 11
def self.on_event(*filter,&blk)
  @@events << [filter, blk]
end

Public Instance Methods

callback(object, events=nil) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/flumtter/app/core/client.rb, line 78
def kill
  @sthread.kill
  @ethread.kill
  @queue.clear
end
pause() click to toggle source
# File lib/flumtter/app/core/client.rb, line 84
def pause
  @mutex.lock
end
resume() click to toggle source
# File lib/flumtter/app/core/client.rb, line 88
def resume
  @mutex.unlock
end
set(account) click to toggle source
# 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() click to toggle source
# File lib/flumtter/app/core/client.rb, line 42
def start
  Flumtter.callback(:init, self)
  stream unless Setting[:non_stream]
end
stream() click to toggle source
# 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

Private Instance Methods

debug() click to toggle source
# File lib/flumtter/app/core/client.rb, line 102
def debug
  update = %i(update update_with_media retweet block unblock create_direct_message follow unfollow favorite unfavorite)
  Twitter::REST::Client.class_eval do
    update.each do |sym|
      define_method sym do |*args|
        puts "Debug: :#{sym} #{args}"
        sleep 1
      end
    end
  end
end
handler() { || ... } click to toggle source
# File lib/flumtter/app/core/client.rb, line 93
def handler
  yield
rescue => e
  error e
rescue Exception => e
  error e
  raise e
end