Skip to content

Commit

Permalink
Fixed error where repeated autolinking was corrupting tweet content
Browse files Browse the repository at this point in the history
  • Loading branch information
TALlama committed Aug 17, 2011
1 parent 098ae7a commit 585db53
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion alexandria.css
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ body {
}

.tweet.from-archive {
border-left: 2px solid red;
border-left: 2px solid rgba(0, 0, 255, .2);
}

.tweet:hover {
Expand Down
2 changes: 1 addition & 1 deletion alexandria.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ jQuery.extend(Date.prototype, {
permalink.append(tweet.created_at.toString());
metaDiv.append(permalink);

if (!('entities' in tweet)) tweetDiv.addClass('from-archive');
if (!('autolinked' in tweet)) tweetDiv.addClass('from-archive');

//TODO: add geo
//TODO: add coordinates
Expand Down
9 changes: 6 additions & 3 deletions lib/readers/api_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ def initialize(opts={})
end

def get_tweet(id_str)
return nil if @rate_limit_exceeded #don't piss off the folks at Twitter

mash = Twitter.status(id_str, :trim_user => true)
sleep api_delay
clean_tweet(mash)
rescue
rescue => e
@rate_limit_exceeded ||= e.is_a?(Twitter::BadRequest) and e.message =~ /rate limit exceeded/i
nil
end

def clean_tweet(mash)
tweet = Tweet.new(mash)
tweet.plain_text = tweet.text
tweet.text = auto_link(tweet.text) unless tweet.autolinked
tweet.plain_text ||= tweet.text unless tweet.autolinked
tweet.text = auto_link(tweet.plain_text) unless tweet.autolinked
tweet.autolinked = true
tweet.created_at = DateTime.parse(tweet.created_at).to_s
user_by_id_str(tweet.user.id_str)
Expand Down
27 changes: 27 additions & 0 deletions lib/readers/tweet_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,33 @@ def all_tweets(options={})
end
end

class TweetReaderDecorator
include TweetReader

attr_accessor :wrapped, :opts

def initialize(*args)
self.opts = args.pop if args.last.respond_to?(:each_pair)
self.wrapped = args.pop if args.last
end

def each_tweet(opts={})
wrapped.each_tweet(opts) do |t|
yield decorate(t)
end
end

def all_tweets(opts={})
wrapped.all_tweets(opts).collect do |t|
decorate(t)
end
end

def decorate(t)
t
end
end

# if you want to read from multiple successive sources, aggregate them
class TweetReaderAggregator
include TweetReader
Expand Down
2 changes: 1 addition & 1 deletion spec/library_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
@lib.opts[:sources] = [source]
@r = @lib.reader
@r.should_not == nil
@r.should be_an_instance_of(klass)
@r.readers.any? {|r| r.is_a? klass }
end
end

Expand Down
9 changes: 9 additions & 0 deletions spec/readers/api_reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,13 @@
/error getting page/i
)
end

it "auto links hashtags" do
mash = Hashie::Mash.new(JSON::parse(%{{"coordinates":null,"favorited":false,"created_at":"2009-09-21T18:33:06+00:00","truncated":false,"id_str":"4152265699","in_reply_to_user_id_str":null,"source_url":"http://bit.ly","source_name":"bit.ly","url":"http://twitter.com/TALlama/status/4152265699","contributors":null,"text":"#hcr Nikki White would have been far better off if only she had been a convicted bank robber. http://bit.ly/3eZanN","in_reply_to_status_id_str":null,"geo":null,"user_name":"TALlama","user":{"id_str":"10588782"},"in_reply_to_screen_name":null,"place":null}}))
text_before = mash.text
tweet = @reader.clean_tweet(mash)

tweet.plain_text.should == text_before
tweet.text.should == "<a href=\"http://twitter.com/search?q=%23hcr\" title=\"#hcr\" class=\"tweet-url hashtag\" rel=\"nofollow\">#hcr</a> Nikki White would have been far better off if only she had been a convicted bank robber. <a href=\"http://bit.ly/3eZanN\" rel=\"nofollow\">http://bit.ly/3eZanN</a>"
end
end

0 comments on commit 585db53

Please sign in to comment.