Module: Flakey::Twitter
- Includes:
- Base
- Defined in:
- lib/flakey/twitter.rb
Constant Summary collapse
- BASE_URL =
"https://twitter.com"- SHARE_URL =
BASE_URL + "/intent/tweet"
- TWEET_BUTTON_CLASS =
Default HTML classes for the various buttons.
'twitter-share-button'- FOLLOW_BUTTON_CLASS =
'twitter-follow-button'
Instance Attribute Summary collapse
-
#output_buffer ⇒ Object
Needed to be able to pass a block down into link_to.
Instance Method Summary collapse
- #custom_tweet_button(options = {}, &block) ⇒ Object
-
#follow_button(options = {}) ⇒ Object
Generate a traditional follow button.
-
#follow_intent_link(options = {}, &block) ⇒ Object
Generate a link which will open a dialog for following the user with whe specified user_id or screen_name.
-
#tweet_button(options = {}) ⇒ Object
Generate a traditional tweet button.
- #tweet_url(options = {}) ⇒ Object
-
#twitter_handle(handle = nil) ⇒ Object
Get the default Twitter handle for this configuration.
-
#twitter_profile_url(handle = nil) ⇒ Object
Get the Twitter profile URL for a handle.
Methods included from Base
Instance Attribute Details
#output_buffer ⇒ Object
Needed to be able to pass a block down into link_to. See the custom_tweet_button method. INFO: stackoverflow.com/a/7562194/574190
20 21 22 |
# File 'lib/flakey/twitter.rb', line 20 def output_buffer @output_buffer end |
Instance Method Details
#custom_tweet_button(options = {}, &block) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/flakey/twitter.rb', line 168 def ( = {}, &block) url = tweet_url() defaults = { label: 'Tweet', class: 'custom-tweet-button', target: '_blank' } settings = defaults.merge() label = settings.delete(:label) # Delete these so we can pass the settings directly into link_to %w[url text hashtags related via label].each do |url_key| settings.delete(url_key.to_sym) end if block_given? link_to(url, settings, &block) else link_to label, url, settings end end |
#follow_button(options = {}) ⇒ Object
Generate a traditional follow button. This method needs the Twitter JavaScript to be loaded on the page in order to work correctly.
Takes a hash of options to use when generating the button.
handle-
The Twitter handle of the user to follow. Defaults to the
twitter_handleconfiguration. size-
The button size. Can be
nilor ‘large’. Configure globally withfollow_button_size. class_list-
HTML classes to apply to the button. Configure globally with
follow_button_class. show_count-
Show the follow count. Defaults to false. Configure globally with
follow_button_show_count.
Returns a HTML string.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/flakey/twitter.rb', line 105 def ( = {}) handle = [:handle] || twitter_handle label = [:label] || "Follow @#{handle}" size = [:size] || Flakey.configuration. class_list = [:class] || Flakey.configuration. show_count = [:show_count] || Flakey.configuration. class_list = class_list ? class_list.concat(' ' + FOLLOW_BUTTON_CLASS) : FOLLOW_BUTTON_CLASS link_to label, twitter_profile_url(handle), class: class_list, data: { :"show-count" => show_count, size: size } end |
#follow_intent_link(options = {}, &block) ⇒ Object
Generate a link which will open a dialog for following the user with whe specified user_id or screen_name.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/flakey/twitter.rb', line 124 def follow_intent_link( = {}, &block) user_id_to_follow = [:user_id] || Flakey.configuration.twitter_user_id screen_name_to_follow = [:screen_name] || Flakey.configuration.twitter_handle label = .has_key?(:label) ? .delete(:label) : "Follow Me" intent_url = 'https://twitter.com/intent/user?' if user_id_to_follow intent_url += "user_id=#{user_id_to_follow}" end intent_url += '&' if user_id_to_follow && screen_name_to_follow unless screen_name_to_follow == '' intent_url += "screen_name=#{screen_name_to_follow}" end if block_given? link_to(intent_url, .except(:user_id, :screen_name), &block) else link_to label, intent_url, .except(:user_id, :screen_name) end end |
#tweet_button(options = {}) ⇒ Object
Generate a traditional tweet button. This method needs the Twitter JavaScript to be loaded on the page to work correctly.
Takes a hash of options to use when generating the button.
url-
The URL to tweet. Default to the current request url.
text-
The textual body of the Tweet. Defaults to the current page title. This is a Twitter convention.
hashtags-
A list of hashtags to include in the tweet. Can be globally configured by setting
tweet_hashtags. via-
Tweet via an associated about. Defaults to the
tweet_viaconfiguration setting. related-
A related Twitter handle. Defaults to the
tweet_relatedconfiguration setting. class-
HTML classes to apply to the Tweet button. Defaults to the
tweet_button_classconfiguration setting which is “twitter-share-button”. size-
The size of the button. Valid options are nil (default) and </i>‘large’</i>.
count-
The position of the Tweet count. Valid options are horizonal (default), vertical and </i>none</i>.
Returns a HTML string.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/flakey/twitter.rb', line 67 def ( = {}) url = [:url] || default_url text = [:text] || '' = [:hashtags] || Flakey.configuration. via = [:via] || Flakey.configuration.tweet_via = [:related] || Flakey.configuration. size = [:size] || Flakey.configuration. class_list = [:class] || Flakey.configuration. count = [:count] || Flakey.configuration. data_attr = { via: via, related: , hashtags: , count: count } # Twitter will take the page title if we just leave it out. data_attr.merge!(text: sanitize(text)) unless text.nil? data_attr.merge!(size: size) unless size.nil? data_attr.merge!(url: url) unless url.nil? data_attr.merge!([:data]) unless [:data].nil? class_list = class_list ? class_list.concat(' ' + TWEET_BUTTON_CLASS) : TWEET_BUTTON_CLASS settings = { class: class_list, data: data_attr } settings.merge!(id: [:id]) if [:id].present? link_to "Tweet", SHARE_URL, settings end |
#tweet_url(options = {}) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/flakey/twitter.rb', line 149 def tweet_url( = {}) defaults = { url: default_url, related: Flakey.configuration., hashtags: Flakey.configuration., via: Flakey.configuration.tweet_via } settings = defaults.merge() url = "#{SHARE_URL}?url=#{CGI.escape settings[:url]}" %w[text hashtags related via].each do |url_key| if settings.has_key?(url_key.to_sym) && settings[url_key.to_sym] != '' url += "&#{url_key}=#{CGI.escape settings[url_key.to_sym]}" end end url end |
#twitter_handle(handle = nil) ⇒ Object
Get the default Twitter handle for this configuration. If a handle argument is supplied, it will be returned.
twitter_handle
# => The default handle.
twitter_handle('peter')
# => 'peter'
Returns a string.
32 33 34 |
# File 'lib/flakey/twitter.rb', line 32 def twitter_handle(handle = nil) handle || Flakey.configuration.twitter_handle end |
#twitter_profile_url(handle = nil) ⇒ Object
Get the Twitter profile URL for a handle. If no handle is specified then this method returns the profile URL of the default handle.
twitter_profile_url
# => "https://twitter.com/default
twitter_profile_url('peter')
# => "https://twitter.com/peter"
Returns a string.
47 48 49 50 |
# File 'lib/flakey/twitter.rb', line 47 def twitter_profile_url(handle = nil) handle = handle || twitter_handle BASE_URL + "/#{handle}" end |