Class: SocialLinker::Subject
- Inherits:
-
Object
- Object
- SocialLinker::Subject
- Defined in:
- lib/social_linker/subject.rb
Constant Summary collapse
- SHARE_TEMPLATES =
Constant defining how the different share-url’s look like and their parameters; the parameters can be set in the options directly, or will be derived from more generic options
{ email: { base: "mailto:emailaddress?", params: [:subject,:body,:cc,:bcc] }, pinterest: { base: "https://pinterest.com/pin/create/button/?", params: {url: :url, media: :media, description: :title} }, linkedin: { base: "https://www.linkedin.com/shareArticle?mini=true&", params: [:url, :title, :summary, :source] }, google: { base: "https://plus.google.com/share?", params: [:url] }, twitter: { base: "https://twitter.com/intent/tweet?", params: {text: :tweet_text, via: :via, url: :url, hashtags: :hashtags} }, twitter_native: { base: "twitter://post?", params: [:message] }, facebook: { base: "https://www.facebook.com/sharer/sharer.php?", params: [:u] }, facebook_native: { base: "fb://publish/profile/me?", params: [:text] }, whatsapp: { base: "whatsapp://send?", params: [:text] } }
Instance Method Summary collapse
-
#camelize_tag_when_needed(tag) ⇒ String
single world tags don’t need any processing, but tags consisting of different words do (before they can use in hashtags following convention).
- #canonical_url ⇒ Object
-
#hashtag_string(tags) ⇒ String
convert an array of strings to a Twitter-like hashtag-string.
- #hashtags ⇒ Object
-
#initialize(options = {}) ⇒ Subject
constructor
Initialize the SocialLinker::Subject.
-
#media ⇒ Object
default media accessor.
-
#method_missing(m, *args) ⇒ Object
Catches method missing and tries to resolve them in either an appropriate share link or option value.
-
#options ⇒ Object
Returns the given options, extended with the (derived) defaults.
-
#quote_string(string) ⇒ String
puts quotes around a string.
-
#share_link(platform) ⇒ Object
Generates a share link for each of the predefined platforms in the ‘SHARE_TEMPLATES` constant.
-
#strip_string(string, max_length = 100) ⇒ String
strips a string to the max length taking into account quoting.
-
#summary ⇒ Object
default summary accessor.
-
#tags ⇒ Object
default tags accessor.
-
#title ⇒ Object
default title accessor.
-
#url ⇒ Object
default url accessor.
- #url_encode(v) ⇒ Object
- #utm_parameters ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Subject
Initialize the SocialLinker::Subject
options accepts:
-
tags
-
url
-
title
-
image_url & image_type(image/jpeg, image/png)
-
… and more often medium specific attributes…
Note by default tracking parameters are added, turn this off by passing ‘utm_parameters: false`
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/social_linker/subject.rb', line 149 def initialize(={}) # basic option syncing = [:u] = [:url] unless [:u] [:media] = [:image_url] unless [:media] [:description] = [:summary] unless [:description] [:summary] = [:description] unless [:summary] [:title] = "#{ strip_string(@options[:summary], 120) }" unless [:title] [:description] = [:title] unless [:description] [:subject] = [:title] unless [:subject] [:via] = [:twitter_username] unless [:via] [:url] = [:media] unless [:url] [:text] = "#{@options[:title]} #{@options[:url]}" unless [:text] #facebook & whatsapp native [:canonical_url] = [:url] if [:url] and utm_parameters unless [:url].match /utm_source/ combine_with = [:url].match(/\?/) ? "&" : "?" [:url] = "#{@options[:url]}#{combine_with}utm_source=<%=share_source%>" end unless [:url].match /utm_medium/ combine_with = "&" [:url] = "#{@options[:url]}#{combine_with}utm_medium=share_link" end unless [:url].match /utm_campaign/ combine_with = "&" [:url] = "#{@options[:url]}#{combine_with}utm_campaign=social" end end if [:tags] [:tags].compact! [:hashtags] = [:tags][0..1].collect{|a| camelize_tag_when_needed(a) }.join(",") if [:tags] and ![:hashtags] [:hash_string] = [:tags] ? hashtag_string([:tags][0..1]) : "" end unless [:tweet_text] max_length = 140 - (([:hash_string] ? [:hash_string].length : 0) + 12 + 4) #hashstring + url length (shortened) + spaces [:tweet_text] = "#{quote_string(strip_string(@options[:title],max_length))}" end [:message] = [[:tweet_text],[:url],[:hash_string]].compact.join(" ") unless [:message] [:status] = [:message] unless [:status] unless [:body] [:body] = "" [:body] += "#{@options[:summary]}\n" if [:summary] [:body] += "\n#{@options[:url]}\n" if [:url] [:body] += "\n#{@options[:description]}\n" if [:summary] != [:description] and [:description] [:body] += "\n#{@options[:media]}\n" if [:media] != [:url] and [:media] [:body] += "\n\n#{hashtag_string(@options[:tags])}" if [:tags] [:body] = nil if [:body].strip == "" end [:domain] = [:url].split(/\//)[0..2].join("/") if [:url] and ![:domain] .each do |k,v| [k] = v.strip if v and v.is_a? String end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
Catches method missing and tries to resolve them in either an appropriate share link or option value
236 237 238 239 240 241 242 243 244 245 |
# File 'lib/social_linker/subject.rb', line 236 def method_missing(m,*args) share_link_matcher = m.to_s.match(/([a-z]*)_share_link/) if share_link_matcher return share_link(share_link_matcher[1].to_sym) elsif [m] return [m] else super end end |
Instance Method Details
#camelize_tag_when_needed(tag) ⇒ String
single world tags don’t need any processing, but tags consisting of different words do (before they can use in hashtags following convention)
66 67 68 69 |
# File 'lib/social_linker/subject.rb', line 66 def camelize_tag_when_needed(tag) tag = tag.to_s tag.match(/\s/) ? tag.split(/\s/).collect{|a| a.capitalize}.join("") : tag end |
#canonical_url ⇒ Object
82 83 84 |
# File 'lib/social_linker/subject.rb', line 82 def canonical_url [:canonical_url] end |
#hashtag_string(tags) ⇒ String
convert an array of strings to a Twitter-like hashtag-string
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/social_linker/subject.rb', line 51 def hashtag_string() if and .count > 0 = .collect{|a| camelize_tag_when_needed(a) } string = "##{tags.collect{|a| a.to_s.strip.gsub('#','')}.join(" #")}" if string and string.length > 60 puts "WARNING: string of tags longer than adviced lenght of 60 characters: #{string}" end string end end |
#hashtags ⇒ Object
110 111 112 |
# File 'lib/social_linker/subject.rb', line 110 def [:hashtags] end |
#media ⇒ Object
default media accessor
100 101 102 |
# File 'lib/social_linker/subject.rb', line 100 def media [:media] end |
#options ⇒ Object
Returns the given options, extended with the (derived) defaults
207 208 209 |
# File 'lib/social_linker/subject.rb', line 207 def end |
#quote_string(string) ⇒ String
puts quotes around a string
116 117 118 |
# File 'lib/social_linker/subject.rb', line 116 def quote_string(string) "“#{string}”" if string and string.to_s.strip != "" end |
#share_link(platform) ⇒ Object
Generates a share link for each of the predefined platforms in the ‘SHARE_TEMPLATES` constant
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/social_linker/subject.rb', line 214 def share_link(platform) = SHARE_TEMPLATES[platform] raise "No share template defined" unless url_params = {} [:params].each do |k,v| value_key = v||k #smartassery; v = nil for arrays value = [value_key] if value and value.to_s.strip != "" value = value.gsub('<%=share_source%>', platform.to_s) url_params[k] = value end end return [:base]+url_params.collect{|k,v| "#{k}=#{url_encode(v)}"}.join('&') end |
#strip_string(string, max_length = 100) ⇒ String
strips a string to the max length taking into account quoting
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/social_linker/subject.rb', line 124 def strip_string(string, max_length=100) if string and string.length > max_length elipsis = "…" if string[-1] == "”" elipsis = "#{elipsis}”" end max_char = max_length-1-elipsis.length string = string[0..max_char]+elipsis end string end |
#summary ⇒ Object
default summary accessor
94 95 96 |
# File 'lib/social_linker/subject.rb', line 94 def summary [:summary] end |
#tags ⇒ Object
default tags accessor
106 107 108 |
# File 'lib/social_linker/subject.rb', line 106 def [:tags] ? [:tags] : [] end |
#title ⇒ Object
default title accessor
88 89 90 |
# File 'lib/social_linker/subject.rb', line 88 def title [:title] end |
#url ⇒ Object
default url accessor
74 75 76 |
# File 'lib/social_linker/subject.rb', line 74 def url [:url] end |
#url_encode(v) ⇒ Object
231 232 233 |
# File 'lib/social_linker/subject.rb', line 231 def url_encode(v) ERB::Util.url_encode(v) end |
#utm_parameters ⇒ Object
78 79 80 |
# File 'lib/social_linker/subject.rb', line 78 def utm_parameters [nil, true].include?([:utm_parameters]) ? true : false end |