Skip to content

Commit

Permalink
more readme, more twitter tags
Browse files Browse the repository at this point in the history
  • Loading branch information
will-r committed Jul 28, 2011
1 parent 0d00e36 commit 614ec4a
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 103 deletions.
15 changes: 0 additions & 15 deletions README

This file was deleted.

86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Twitter

An extension to Radiant that will automatically tweet the publication of new pages in selected parts of your site, and which provides a set of radius tags that allow you to feature twitter feeds of different kinds on your site.

## Installation

sudo gem install twitter
sudo gem install radiant-twitter-extension

## Configuration

You can present twitter searches and feeds without authenticating, but if you want to post automatically to twitter you need to provide login information. The extension adds a 'twitter' block to the main radiant configuration interface: enter your screen name and password. Future versions may integrate with Twitter as an application but for now all we need is the ability to tweet.

## Tweet on publication

To post a tweet every time you publish a blog entry, check the 'Notify Twitter of newly published child pages?' box on the parent blog page. The tweet will contain the title of the page and its url.

## Display a twitter feed

If radiant is configured to tweet for you, all you need is this radius tag:

<r:twitter:tweets [max="10"] />

If it's not configured, or you want to display another user:

<r:twitter:tweets user="screen_name" />

To display a hashtag, or any other search:

<r:twitter:tweets search="#radiant" />

To display tweets from someone's list:

<r:twitter:tweets user="screen_name" list="list_name" />

The default presentation of tweets is exactly as [specified by Twitter](https://dev.twitter.com/terms/display-guidelines) and if you include their widget script and the provided css it should all just work. If you want to present tweets differently, a range of more detailed radius tags is available. This is a slightly more compact format:

<r:twitter:tweets user="screen_name" />
<li class="tweet">
<r:tweet:avatar class="avatar" />
<r:tweet:user:screen_name />
<span class="block">
<r:tweet:text />
</span>
<span class="hidden">
<r:tweet:reply_link />
<r:tweet:retweet_link />
</span>
</li>
</r:twitter:tweets>
## Scripts and styles

The quick way to format tweets nicely is to include the supplied sass in your site stylesheet. If you're using radiant's built-in stylesheet manager and working in Sass, you can keep everything in one file (and selectively override it) by including this line near the top:

@import 'twitter.sass'

You can also link to `/stylesheets/twitter.css` in the usual way.

The links created by radius tags here are all compatible with twitter's widgeting. To enable basic intent-based popups, just include this line in the head or at the foot of your layout:

<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>

## Todo

* Smoother twitter integration
* Page field to enter tweet text before publication
* URL-shortener

## Copyright and license

Originally created by Sean Cribbs and now the work of many hands:

* Jim Gay
* Edmund Haselwanter
* Anna Billstrom
* William Ross

Currently maintained by Will at spanner.org. Issues and comments on github, please:

https://github.com/radiant/radiant-twitter-extension/issues

Released under the same terms as Rails and/or Radiant.



7 changes: 4 additions & 3 deletions app/views/admin/pages/_twitter.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
%th.label
Twitter
%td
= check_box 'page', 'notify_twitter_of_children'
%label{:for => "page_notify_twitter_of_children"} Notify Twitter of newly published child pages?
- fields_for @page do |pf|
= pf.check_box :notify_twitter_of_children
= pf.label :notify_twitter_of_children
- if @page.twitter_id
= link_to "[#{@page.twitter_id}]", "http://twitter.com/#{Radiant::Config['twitter.username']}/statuses/#{@page.twitter_id}"
= link_to "[#{@page.twitter_id}]", "http://twitter.com/#{Radiant::Config['twitter.username']}/status/#{@page.twitter_id}"
14 changes: 12 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
en:
activerecord:
attributes:
page:
notify_twitter_of_children: "Notify Twitter of newly published child pages?"
config:
twitter:
username: "Username (for tweets)"
password: "Password (for tweets)"
token: "Application token (for feeds)"
secret: "Application secret (for feeds)"
token: "Application token"
secret: "Application secret"
date:
formats:
twitter: "%m %B"
twitter_extension:
favorite: "Favorite"
reply: "Reply"
retweet: "Retweet"
twitter: "Twitter"

78 changes: 59 additions & 19 deletions lib/twitter_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TagError < StandardError; end
You can also specify a search in various ways.
* Supply a `max` attribute to change the number of tweets displayed
* Supply a `max` attribute to change the number of tweets displayed. Default is 10.
* Supply a `user` attribute to display tweets from a different username
* Supply a `list` attribute to display tweets from the named list (see also r:twitter:list for a shortcut)
* Supply a `search` attribute to show tweets containing that text (see also r:twitter:search for a shortcut)
Expand All @@ -42,7 +42,7 @@ class TagError < StandardError; end

tag 'twitter:tweets:each' do |tag|
tag.locals.tweets ||= fetch_and_cache_tweets(tag.attr.slice('user', 'max', 'search', 'list').symbolize_keys)
tag.render('_tweets_list')
tag.render('_tweets_list', tag.attr.dup, &tag.block)
end

desc %{
Expand All @@ -69,7 +69,7 @@ class TagError < StandardError; end

tag 'twitter:search:each' do |tag|
tag.locals.tweets ||= fetch_and_cache_tweets(:search => tag.attr['for'])
tag.render('_tweets_list')
tag.render('_tweets_list', tag.attr.dup, &tag.block)
end

desc %{
Expand All @@ -92,21 +92,21 @@ class TagError < StandardError; end

tag 'twitter:list:each' do |tag|
tag.locals.tweets ||= fetch_and_cache_tweets(:user => tag.attr['user'], :max => tag.attr['max'], :list => tag.attr['list'])
tag.render('_tweets_list')
tag.render('_tweets_list', tag.attr.dup, &tag.block)
end

desc %{
Returns the number of tweets.
}
tag 'tweets:length' do |tag|
tag.render('_tweets_length')
tag.render('_tweets_length', tag.attr.dup, &tag.block)
end

desc %{
Loops through the current list of tweets.
}
tag 'tweets:each' do |tag|
tag.render('_tweets_list')
tag.render('_tweets_list', tag.attr.dup, &tag.block)
end

# these are just for drying out: they can't be called directly.
Expand All @@ -116,7 +116,11 @@ class TagError < StandardError; end
out = ""
tag.locals.tweets.each do |tweet|
tag.locals.tweet = tweet
out << tag.double? ? tag.expand : tag.render('tweet:message')
if tag.double?
out << tag.expand
else
out << tag.render('tweet:message')
end
end
out
end
Expand All @@ -125,8 +129,6 @@ class TagError < StandardError; end
raise TagError, "_tweets_length utility tag called without a list of tweets to length" unless tag.locals.tweets
tag.locals.tweets.length
end



desc %{
Usage:
Expand Down Expand Up @@ -172,10 +174,10 @@ class TagError < StandardError; end
<span class="twitter_name">#{tag.render('tweet:user:name')}</span>
<span class="twitter_text">#{text}</span>
<span class="twitter_links">
<a class="twitter_permalink" href="http://twitter.com/#!/#{screen_name}/status/#{tweet.id_str}">#{date}</a>
<a class="twitter_reply" href="http://twitter.com/intent/tweet?in_reply_to=#{tweet.id_str}">reply</a>
<a class="twitter_retweet" href="http://twitter.com/intent/retweet?tweet_id=#{tweet.id_str}">retweet</a>
<a class="twitter_favorite" href="http://twitter.com/favorite?tweet_id?in_reply_to=#{tweet.id_str}">favorite</a>
#{tag.render('tweet:permalink')}
#{tag.render('tweet:reply_link')}
#{tag.render('tweet:retweet_link')}
#{tag.render('tweet:favorite_link')}
</span>
</span>
</p>
Expand All @@ -190,15 +192,19 @@ class TagError < StandardError; end
<pre><code><r:tweet:#{method.to_s}/></code></pre>
}
tag "tweet:#{method.to_s}" do |tag|
tag.locals.tweet.send(method) rescue nil

p "calling #{method} of #{tag.locals.tweet.inspect}"
p "respond_to?(:source) is #{tag.locals.tweet.respond_to?(:source).inspect} and source is #{tag.locals.tweet.source}"

tag.locals.tweet.send(method) if tag.locals.tweet.respond_to? method
end

desc %{
expands if the property has a value
<pre><code><r:tweet:if_#{method.to_s}/></code></pre>
}
tag "tweet:if_#{method.to_s}" do |tag|
value = tag.locals.tweet.send(method) rescue nil
value = tag.locals.tweet.send(method) if tag.locals.tweet.respond_to? method
tag.expand if !value.nil? && !value.empty?
end

Expand All @@ -207,7 +213,7 @@ class TagError < StandardError; end
<pre><code><r:tweet:unless_#{method.to_s}/></code></pre>
}
tag "tweet:unless_#{method.to_s}" do |tag|
value = tag.locals.tweet.send(method) rescue nil
value = tag.locals.tweet.send(method) if tag.locals.tweet.respond_to? method
tag.expand if value.nil? || value.empty?
end
end
Expand Down Expand Up @@ -287,6 +293,42 @@ class TagError < StandardError; end
time_ago_in_words tweet.created_at
end

desc %{
Renders a permalink to this tweet with its date as the default link text.
}
tag 'tweet:permalink' do |tag|
cssclass = tag.attr['class'] || 'twitter_permalink'
text = tag.double? ? tag.expand : I18n.l(tag.locals.tweet.created_at, :twitter)
%{<a class="#{cssclass}" href="http://twitter.com/#!/#{screen_name}/status/#{tweet.id_str}">#{text}</a>}
end

desc %{
Renders a 'Reply' link that can be left as it is or hooked up by the twitter javascript.
}
tag 'tweet:reply_link' do |tag|
cssclass = tag.attr['class'] || 'twitter_reply'
text = tag.double? ? tag.expand : I18n.t('twitter_extension.reply')
%{<a class="#{cssclass}" href="http://twitter.com/intent/tweet?in_reply_to=#{tag.locals.tweet.id_str}">#{text}</a>}
end

desc %{
Renders a 'Retweet' link that can be left as it is or hooked up by the twitter javascript.
}
tag 'tweet:retweet_link' do |tag|
cssclass = tag.attr['class'] || 'twitter_retweet'
text = tag.double? ? tag.expand : I18n.t('twitter_extension.retweet')
%{<a class="#{cssclass}" href="http://twitter.com/intent/retweet?tweet_id=#{tag.locals.tweet.id_str}">#{text}</a>}
end

desc %{
Renders a 'Favorite' link that can be left as it is or hooked up by the twitter javascript.
}
tag 'tweet:favorite_link' do |tag|
cssclass = tag.attr['class'] || 'twitter_favorite'
text = tag.double? ? tag.expand : I18n.t('twitter_extension.favorite')
%{<a class="#{cssclass}" href="http://twitter.com/intent/favorite?tweet_id=#{tag.locals.tweet.id_str}">#{text}</a>}
end

private

# Retained for compatibility
Expand All @@ -304,8 +346,7 @@ def twitter_status(max = 1)
# other options are passed through (to non-search calls) unchanged.
#
def fetch_and_cache_tweets(options = {})
Rails.logger.warn "! fetch_and_cache_tweets(#{options.inspect})"
max = options.delete(:max) || 5
max = options.delete(:max) || 10
user = options.delete(:username) || Radiant.config['twitter.username']
list = options.delete(:list) || Radiant.config['twitter.listname']
search = options.delete(:search)
Expand All @@ -318,7 +359,6 @@ def fetch_and_cache_tweets(options = {})
elsif list
twitter_client.list_timeline(user, list, options)
else
Rails.logger.warn "! calling twitter_client.user_timeline(#{user}, #{options.inspect})"
twitter_client.user_timeline(user, options)
end
end
Expand Down
31 changes: 31 additions & 0 deletions radiant-twitter-extension.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "radiant-twitter-extension"

Gem::Specification.new do |s|
s.name = "radiant-twitter-extension"
s.version = RadiantTwitterExtension::VERSION
s.platform = Gem::Platform::RUBY
s.authors = RadiantTwitterExtension::AUTHORS
s.email = RadiantTwitterExtension::EMAIL
s.homepage = RadiantTwitterExtension::URL
s.summary = RadiantTwitterExtension::SUMMARY
s.description = RadiantTwitterExtension::DESCRIPTION

s.add_dependency 'twitter', "~> 1.6.0"

ignores = if File.exist?('.gitignore')
File.read('.gitignore').split("\n").inject([]) {|a,p| a + Dir[p] }
else
[]
end
s.files = Dir['**/*'] - ignores
s.test_files = Dir['test/**/*','spec/**/*','features/**/*'] - ignores
# s.executables = Dir['bin/*'] - ignores
s.require_paths = ["lib"]

s.post_install_message = %{
Add this to your radiant project with:
config.gem 'radiant-twitter-extension', :version => '~>#{RadiantTwitterExtension::VERSION}'
}
end
Loading

0 comments on commit 614ec4a

Please sign in to comment.