class Rabbiter::Client

Constants

CONSUMER_KEY
CONSUMER_SECRET

Public Class Methods

new(logger) click to toggle source
# File lib/rabbiter.rb, line 37
def initialize(logger)
  @logger = logger
  @oauth_parameters = nil
  @config_file_path = Pathname.new("~/.rabbit/twitter-oauth.yaml")
  @config_file_path = @config_file_path.expand_path
  @listeners = []
  @connection = nil
end

Public Instance Methods

close() click to toggle source
# File lib/rabbiter.rb, line 62
def close
  return if @connection.nil?
  @connection.close
  @connection = nil
end
register_listener(&block) click to toggle source
# File lib/rabbiter.rb, line 46
def register_listener(&block)
  @listeners << block
end
setup() click to toggle source
# File lib/rabbiter.rb, line 50
def setup
  return unless @oauth_parameters.nil?
  setup_access_token unless @config_file_path.exist?
  oauth_access_parameters = YAML.load(@config_file_path.read)
  @oauth_parameters = {
    :consumer_key => CONSUMER_KEY,
    :consumer_secret => CONSUMER_SECRET,
    :access_key => oauth_access_parameters[:access_token],
    :access_secret => oauth_access_parameters[:access_secret],
  }
end
start(*filters, &block) click to toggle source
# File lib/rabbiter.rb, line 68
def start(*filters, &block)
  register_listener(&block) if block_given?
  setup if @oauth_parameters.nil?
  return if @oauth_parameters.nil?

  stream_options = {
    :oauth => @oauth_parameters,
    :user_agent => "Rabbiter #{Rabbiter::VERSION}",
    :method => "POST",
    :filters => filters,
  }
  @stream = ::Twitter::JSONStream.allocate
  @stream.send(:initialize, stream_options)
  @stream.send(:reset_state)
  @connection = GLibConnection.new(@logger, @stream)

  @stream.each_item do |item|
    status = JSON.parse(item)
    @listeners.each do |listener|
      listener.call(status)
    end
  end

  @stream.on_error do |message|
    @logger.error("[twitter] #{message}")
  end

  @stream.on_reconnect do |timeout, n_retries|
    format = _("%{timeout} seconds (%{n_retries})")
    message = format % {:timeout => timeout, :n_retries => retries}
    @logger.info("[twitter][reconnect] #{message}")
  end

  @stream.on_max_reconnects do |timeout, n_retries|
    format = _("Failed after %{n_retries} failed reconnects")
    message = format % {:n_retries => retries}
    @logger.info("[twitter][max-reconnects] #{message}")
  end

  @connection.connect
end

Private Instance Methods

setup_access_token() click to toggle source
# File lib/rabbiter.rb, line 111
def setup_access_token
  FileUtils.mkdir_p(@config_file_path.dirname)
  oauth_options = {
    :consumer_key => CONSUMER_KEY,
    :consumer_secret => CONSUMER_SECRET,
    :proxy => ENV["http_proxy"],
  }
  client = TwitterOAuth::Client.new(oauth_options)
  request_token = client.request_token
  authorize_url = request_token.authorize_url
  puts( _("1) Access this URL: %{url}") % {:url => authorize_url})
  show_uri(authorize_url)
  print(_("2) Enter the PIN: "))
  pin = $stdin.gets.strip
  access_token = request_token.get_access_token(:oauth_verifier => pin)
  oauth_parameters = {
    :access_token => access_token.token,
    :access_secret => access_token.secret,
  }
  @config_file_path.open("w") do |config_file|
    config_file.chmod(0600)
    config_file.puts(YAML.dump(oauth_parameters))
  end
end
show_uri(uri) click to toggle source
# File lib/rabbiter.rb, line 136
def show_uri(uri)
  return unless Gtk.respond_to?(:show_uri)
  begin
    Gtk.show_uri(uri)
  rescue GLib::Error
    @logger.warning("[twitter][show-uri] #{$!}")
  end
end