Module: TweetButton

Defined in:
lib/tweet-button.rb

Constant Summary collapse

TWITTER_SHARE_URL =
"http://twitter.com/share"

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.default_tweet_button_optionsObject

Returns the value of attribute default_tweet_button_options.



18
19
20
# File 'lib/tweet-button.rb', line 18

def default_tweet_button_options
  @default_tweet_button_options
end

Instance Method Details

#custom_tweet_button(text = 'Tweet', options = {}, html_options = {}) ⇒ Object



37
38
39
40
# File 'lib/tweet-button.rb', line 37

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

#default_tweet_button_optionsObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/tweet-button.rb', line 21

def default_tweet_button_options
  {
    :url => request.url, 
    :via => "tweetbutton", 
    :text => "", 
    :related => "", 
    :count => "vertical", 
    :lang => "en"
  }.merge(TweetButton.default_tweet_button_options || {})
end

#options_to_data_params(opts) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/tweet-button.rb', line 42

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

#options_to_query_string(opts) ⇒ Object

I’m pretty sure this is in the stdlib or Rails somewhere. Too lazy to figure it out now.



52
53
54
# File 'lib/tweet-button.rb', line 52

def options_to_query_string(opts)
  opts.map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v)}"}.join("&")
end

#tweet_button(options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/tweet-button.rb', line 4

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

#tweet_widgets_js_tagObject



32
33
34
35
# File 'lib/tweet-button.rb', line 32

def tweet_widgets_js_tag
  @widgetized = true
  '<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>'.html_safe
end