As the missing formatting tool for the Twitter gem, TweetPretty allows you to easily replace entities in tweets with properly encoded HTML to conform to Twitter's Display Requirements.
Add this line to your application's Gemfile:
gem 'tweet_pretty'
And then execute:
$ bundle
Or install it yourself as:
$ gem install tweet_pretty
If you're not working in an environment where gems are auto-loaded, then require
the gem to monkey patch the Twitter::Tweet
class:
require 'tweet_pretty'
Use the Twitter gem to pull a tweet:
> tweet = Twitter.status(379713438806597632)
Then use the to_html
method to convert its text
attribute into
a properly converted HTML snippet:
> tweet.text
=> "Pardon this test tweet. It's hard to find tweets containing at least one of each entity. #devtest @mherold https://t.co/CbYo8pjDDO"
> tweet.to_html
=> "Pardon this test tweet. It's hard to find tweets containing at least one of each entity. <a class="hashtag" href="http://twitter.com/search?q=#devtest">#devtest</a> <a class="user-mention" title="Michael Herold" href="http://twitter.com/mherold">@mherold</a> <a class="link" href="https://t.co/CbYo8pjDDO">twitter.com/mherold</a>"
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
- The original algorithm used to do the text replacements was based on some Javascript code in a gist by @wadey. This little snippet inspired me to encapsulate the functionality for use with the Twitter Ruby gem. This algorithm has since been replaced by a more Ruby-ish approach.
- The configuration class is based off of that from Geocoder, which is a pretty awesome library for geocoding in Ruby.
This library aims to support the same set of Ruby implementations as the twitter gem, which it is extending. Currently, those are:
- Ruby 1.8.7
- Ruby 1.9.2
- Ruby 1.9.3
- Ruby 2.0.0
This library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Thus, you should specify a gem dependency using the Pessimistic Version Constraint with two digits of precision:
# In Gemfile
gem 'tweet_pretty', '~> 1.0'
# In .gemspec
spec.add_dependency 'tweet_pretty', '~> 1.0'
Copyright (c) 2013 Michael Herold. Open sourced under the terms of the MIT license.