Permalink
Browse files

Merge branch 'master' of git://github.com/bcarlso/tweet-tail into int…

…egration
  • Loading branch information...
2 parents aabe6f6 + 3976c8f commit 927847fbba58e3edd7c506d072351cfdeab62d34 @drnic committed May 20, 2009
View
@@ -10,4 +10,6 @@ module TweetTail
require "open-uri"
-require "tweet-tail/tweet_poller"
+require "tweet-tail/tweet_poller"
+
+require "tweet-tail/ansi_tweet_formatter"
@@ -0,0 +1,43 @@
+module TweetTail::AnsiTweetFormatter
+ COLORS = { 'black' => 0,
+ 'red' => 1,
+ 'green' => 2,
+ 'yellow' => 3,
+ 'blue' => 4,
+ 'magenta' => 5,
+ 'cyan' => 6,
+ 'white' => 7 }
+
+ def self.extend_object(obj)
+ @@username_color, @@keyword_color, @@url_color = configure_colors
+ super
+ end
+
+ def self.configure_colors
+ color_list = (ENV['TWEETTAIL_COLORS'] || "green,cyan,cyan").split(',')
+ return COLORS[color_list[0]], COLORS[color_list[1]], COLORS[color_list[2]]
+ end
+
+ def format(tweet)
+ escaped_message = escape_replys_and_hashtags_in tweet['text']
+ escaped_message = escape_urls_in escaped_message
+ escaped_user = escape_username tweet['from_user']
+ "#{escaped_user}: #{escaped_message}\n"
+ end
+
+ def escape_replys_and_hashtags_in(text)
+ text.gsub(%r{[@#]\w+}, "\e[3#{@@keyword_color}m\\0\e[0m")
+ end
+
+ def escape_urls_in(text)
+ text.gsub(%r{https?://[\w./]+}, "\e[4;3#{@@url_color}m\\0\e[0m")
+ end
+
+ def escape_username(username)
+ "\e[3#{@@username_color}m#{username}\e[0m"
+ end
+
+ private :escape_replys_and_hashtags_in,
+ :escape_urls_in,
+ :escape_username
+end
@@ -27,6 +27,8 @@ def self.execute(stdout, arguments=[])
begin
app = TweetTail::TweetPoller.new(query)
+ app.extend(TweetTail::AnsiTweetFormatter) if stdout.tty?
+
app.refresh
stdout.puts app.render_latest_results
while(options[:polling])
@@ -1,4 +1,5 @@
require 'net/http'
+require 'tweet-tail/ansi_tweet_formatter'
class TweetTail::TweetPoller
attr_accessor :query, :latest_results, :refresh_url
@@ -20,12 +21,16 @@ def refresh
def render_latest_results
@latest_results.inject("") do |output, tweet|
- screen_name = tweet['from_user']
- message = tweet['text']
- output += "#{screen_name}: #{message}\n"
+ output += format(tweet)
end
end
+ def format(tweet)
+ screen_name = tweet['from_user']
+ message = tweet['text']
+ "#{screen_name}: #{message}\n"
+ end
+
protected
def initial_json_data
Net::HTTP.get(URI.parse("http://search.twitter.com/search.json?q=#{query}"))
@@ -0,0 +1,73 @@
+require File.dirname(__FILE__) + '/test_helper.rb'
+
+class TestTweetFormatter < Test::Unit::TestCase
+ def setup
+ @formatter = Object.new.extend(TweetTail::AnsiTweetFormatter)
+ end
+
+ def test_should_configure_colors_upon_module_inclusion
+ TweetTail::AnsiTweetFormatter.expects(:configure_colors)
+ Object.new.extend(TweetTail::AnsiTweetFormatter)
+ end
+
+ def test_configure_colors_should_use_default_colors_when_environment_variable_not_specified
+ ENV['TWEETTAIL_COLORS'] = nil
+
+ username_color, keyword_color, url_color = TweetTail::AnsiTweetFormatter.configure_colors
+
+ assert_equal 2, username_color
+ assert_equal 6, keyword_color
+ assert_equal 6, url_color
+ end
+
+ def test_configure_colors_should_set_colors_according_to_environment_variable
+ ENV['TWEETTAIL_COLORS'] = "blue,red,yellow"
+
+ username_color, keyword_color, url_color = TweetTail::AnsiTweetFormatter.configure_colors
+
+ assert_equal 4, username_color
+ assert_equal 1, keyword_color
+ assert_equal 3, url_color
+ end
+
+ def test_should_escape_username_on_basic_tweet
+ assert_equal "\e[32mbob\e[0m: Basic tweet\n", @formatter.format('from_user' => 'bob',
+ 'text' => 'Basic tweet')
+ end
+
+ def test_should_escape_links_when_at_the_beginning
+ assert_match %r{\e\[4;36mhttp://example.com\e\[0m}, @formatter.format('text' => 'http://example.com is my favorite site')
+ end
+
+ def test_should_escape_links_when_at_the_end
+ assert_match %r{\e\[4;36mhttp://example.com\e\[0m}, @formatter.format('text' => 'My favorite site is http://example.com')
+ end
+
+ def test_should_escape_links_when_https_is_used
+ assert_match %r{\e\[4;36mhttps://secure.example.com\e\[0m}, @formatter.format('text' => 'My favorite site is https://secure.example.com')
+ end
+
+ def test_should_escape_hashtags_when_at_the_beginning
+ assert_match %r{\e\[36m#rubyconf\e\[0m}, @formatter.format('text' => '#rubyconf bound')
+ end
+
+ def test_should_escape_hashtags_when_at_the_end
+ assert_match %r{\e\[36m#jaoo\e\[0m}, @formatter.format('text' => 'Hanging at #jaoo')
+ end
+
+ def test_should_escape_hashtags_when_followed_by_punctuation
+ assert_match %r{\e\[36m#jaoo\e\[0m}, @formatter.format('text' => 'Hanging at #jaoo.')
+ end
+
+ def test_should_escape_replies_when_at_the_beginning
+ assert_match %r{\e\[36m@reply\e\[0m}, @formatter.format('text' => '@reply at beginning')
+ end
+
+ def test_should_escape_replies_when_at_the_end
+ assert_match %r{\e\[36m@end\e\[0m}, @formatter.format('text' => 'reply at @end')
+ end
+
+ def test_should_escape_replies_when_followed_by_punctuation
+ assert_match %r{\e\[36m@sentence\e\[0m}, @formatter.format('text' => 'End of @sentence.')
+ end
+end

0 comments on commit 927847f

Please sign in to comment.