require 'twitter'
require 'myun2/twitter_shell/consumer_key'

module Myun2
  class TwitterShell
    class Tail
      def initialize(profile, *params)
        Tail.home_timeline_streaming(profile) do |o|
          if o.is_a?(::Twitter::Tweet)
            puts "#{o.created_at} -> @#{o.user.screen_name}"
            puts "\e[38;5;#{o.user.id % 255}m<#{o.user.name}>\e[0m: #{o.text}"
          end
        end
      end

      def self.streaming_client(profile)
        @@client ||= ::Twitter::Streaming::Client.new(
          consumer_key:         Myun2::TwitterShell::ConsumerKey::KEY,
          consumer_secret:      Myun2::TwitterShell::ConsumerKey::SECRET,
          access_token:         profile[:access_token],
          access_token_secret:  profile[:access_token_secret],
        )
      end

      def self.home_timeline_streaming(profile, &block)
        streaming_client(profile).user({}, &block)
      end
    end
  end
end
