Permalink
Browse files

First commit

  • Loading branch information...
1 parent 6dd4117 commit a9e9b85231afb04b24c5dd899a729b3d518a21cd @jm committed Aug 12, 2010
Showing with 155 additions and 1 deletion.
  1. +50 −1 README.rdoc
  2. +55 −0 lib/tweet-button.rb
  3. +50 −0 tweet-button.gemspec
View
51 README.rdoc
@@ -1,6 +1,55 @@
= tweet-button
-Description goes here.
+Jeremy McAnally - Intridea
+
+A gem/plugin to generate those nifty new Twitter buttons.
+
+== Usage
+
+First, include +TweetButton+ into your application helper. After that, using it is as simple as adding a single method call to your views:
+
+ <%= tweet_button %>
+
+Bam. Done. You'll have a sweet lookin' Tweet button all up in your view.
+
+Of course, you can customize it. The method takes a few options (any default can be overridden universally; read on...):
+
+* +:url+ - The URL to share; the default is the current URL.
+* +:text+ - The text that will appear in the tweet; the default is "Check this out!"
+* +:via+ - The attribution. Defaults to "tweetbutton", but you should change that.
+* +:lang+ - Set the language for the tweet (no default).
+* +:related+ - Related Twitter accounts (no default).
+* +:count+ - The tweet count box position (values can be "none", "horizontal", or "vertical"; default is "vertical").
+
+So, if you wanted to tweet about Hacker News, attribute it to Peter Cooper, and add some custom text, all from a tweet button with a horizontal counter, you'd do this:
+
+ <%= tweet_button(:via => "peterc", :url => "http://news.ycombinator.com", :text => "AWESOME.")
+
+Simple enough, eh? Also, this method call will include the Twitter JavaScript into the page (it only does it once, even if you have multiple buttons on the page). To put this wherever you'd like (i.e., your header), then use the +twitter_widgets_js_tag+ method. If you call this method, it will place the tag wherever you call it from (and only place it there; subsequent calls do nothing).
+
+The gem also supports the custom Twitter share links. To generate one, use the +custom_tweet_button+ (aliased to +custom_tweet_link+ also) method:
+
+ <%= custom_tweet_button %>
+
+This will generate a link that will link to the share page with the same default options as the standard Tweet Button generator. You can customize your custom link with text as the first argument, the same options as +tweet_button+ (with the exception of the +count+ parameter, which will be ignored) as the second, and HTML options as a third argument. For example:
+
+ <%= custom_tweet_button('Tweet it!', {:via => "myself"}, {:class => "tweet-sharey-thing"})
+
+== Setting universal defaults
+
+You can set a new default for any option by setting +default_tweet_button_options+ in your application helper. For example:
+
+ module ApplicationHelper
+ include TweetButton
+
+ TweetButton.default_tweet_button_options = {:via => "myself"}
+ end
+
+Only the options you specify will be overridden; so if you only specify a new default +:via+ (which you should definitely do), then the other defaults will stay intact.
+
+== TODO
+
+Probably add the iframe capability.
== Note on Patches/Pull Requests
View
55 lib/tweet-button.rb
@@ -0,0 +1,55 @@
+module TweetButton
+ TWITTER_SHARE_URL = "http://twitter.com/share"
+
+ def tweet_button(options = {})
+ # Merge user specified overrides into defaults, then convert those to data-* attrs
+ params = options_to_data_params(default_tweet_button_options.merge(options))
+
+ html = ''.html_safe
+
+ unless @widgetized
+ html << tweet_widgets_js_tag
+ end
+
+ html << link_to("Tweet", TWITTER_SHARE_URL, params)
+ end
+
+ class <<self
+ attr_accessor :default_tweet_button_options
+ end
+
+ def default_tweet_button_options
+ {
+ :url => request.url,
+ :via => "tweetbutton",
+ :text => "",
+ :related => "",
+ :count => "vertical",
+ :lang => "en"
+ }.merge(TweetButton.default_tweet_button_options || {})
+ end
+
+ def tweet_widgets_js_tag
+ @widgetized = true
+ '<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>'.html_safe
+ end
+
+ def custom_tweet_button(text = 'Tweet', options = {}, html_options = {})
+ # This line is really long. And it makes me sad.
+ link_to(text, "#{TWITTER_SHARE_URL}?#{options_to_query_string(default_tweet_button_options.merge(options))}", html_options)
+ end
+
+ def options_to_data_params(opts)
+ params = {}
+ opts.each {|k, v| params["data-#{k}"] = v}
+
+ # Make sure the CSS class is there
+ params['class'] = 'twitter-share-button'
+ params
+ end
+
+ # I'm pretty sure this is in the stdlib or Rails somewhere. Too lazy to figure it out now.
+ def options_to_query_string(opts)
+ opts.map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v)}"}.join("&")
+ end
+end
View
50 tweet-button.gemspec
@@ -0,0 +1,50 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{tweet-button}
+ s.version = "0.1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Jeremy McAnally"]
+ s.date = %q{2010-08-12}
+ s.description = %q{Generate Tweet buttons for your Rails apps}
+ s.email = %q{jeremymcanally@gmail.com}
+ s.extra_rdoc_files = [
+ "LICENSE",
+ "README.rdoc"
+ ]
+ s.files = [
+ ".document",
+ ".gitignore",
+ "LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION",
+ "lib/tweet-button.rb",
+ "test/helper.rb",
+ "test/test_tweet-button.rb"
+ ]
+ s.homepage = %q{http://github.com/jm/tweet-button}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.6}
+ s.summary = %q{Generate new Twitter 'Tweet buttons'}
+ s.test_files = [
+ "test/helper.rb",
+ "test/test_tweet-button.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ else
+ end
+ else
+ end
+end
+

0 comments on commit a9e9b85

Please sign in to comment.