Class: SocialLinker::Subject

Inherits:
Object
  • Object
show all
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: :share_url, media: :media, description: :title}
  },
  linkedin: {
    base: "https://www.linkedin.com/shareArticle?mini=true&",
    params: {url: :share_url, title: :title, summary: :summary, source: :source}
  },
  google: {
    base: "https://plus.google.com/share?",
    params: {url: :share_url}
  },
  twitter: {
    base: "https://twitter.com/intent/tweet?",
    params: {text: :twitter_text, via: :via, url: :share_url, hashtags: :hashtags}
  },
  twitter_native: {
    base: "twitter://post?",
    params: {message: :twitter_text_with_url_and_hashags}
  },
  facebook: {
    base: "https://www.facebook.com/sharer/sharer.php?",
    params: {u: :share_url}
  },
  facebook_native: {
    base: "fb://publish/profile/me?",
    params: [:text]
  },
  whatsapp: {
    base: "whatsapp://send?",
    params: [:text]
  }

}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Subject

Initialize the SocialLinker::Subject

options accepts:

  • tags

  • url

  • title

  • image_url & image_type(image/jpeg, image/png)

  • description

  • facebook_app_id

  • twitter_username

  • language

  • site_title_postfix

  • … and more often medium specific attributes…

Note by default tracking parameters are added, turn this off by passing ‘utm_parameters: false`



154
155
156
157
158
# File 'lib/social_linker/subject.rb', line 154

def initialize(options={})
  # basic option syncing
  @options = {}
  self.merge!(options)
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



321
322
323
324
325
326
327
328
329
330
# File 'lib/social_linker/subject.rb', line 321

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 options[m]
    return options[m]
  else
    super
  end
end

Instance Method Details

#bodyObject

Generates a large body of text (typical for email)

Returns:

  • String



234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/social_linker/subject.rb', line 234

def body
  return options[:body] if options[:body]
  rv = ""
  rv += "#{@options[:summary]}\n" if options[:summary]
  rv += "\n#{@options[:share_url]}\n" if options[:share_url]
  rv += "\n#{@options[:description]}\n" if options[:summary] != options[:description] and options[:description]
  rv += "\n#{@options[:media]}\n" if options[:media] != options[:share_url] and options[:media]
  rv += "\n\n#{hashtag_string(@options[:tags])}" if options[:tags]
  rv.strip!
  rv = nil if rv == ""
  return rv
end

#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)

Parameters:

  • tag (String)

    to might need conversion

Returns:

  • (String)

    fixed tag



62
63
64
65
# File 'lib/social_linker/subject.rb', line 62

def camelize_tag_when_needed(tag)
  tag = tag.to_s
  tag.match(/\s/) ? tag.split(/\s/).collect{|a| a.capitalize}.join("") : tag
end

#canonical_urlObject



82
83
84
# File 'lib/social_linker/subject.rb', line 82

def canonical_url
  @options[:canonical_url]
end

#hashtag_string(tags) ⇒ String

convert an array of strings to a Twitter-like hashtag-string

Parameters:

  • tags (Array)

    to be converted to string

Returns:

  • (String)

    containing a Twitter-style tag-list



51
52
53
54
55
56
# File 'lib/social_linker/subject.rb', line 51

def hashtag_string(tags)
  if tags and tags.count > 0
    tags = tags.collect{|a| camelize_tag_when_needed(a) }
    "##{tags.collect{|a| a.to_s.strip.gsub('#','')}.join(" #")}"
  end
end

#hashtagsObject



110
111
112
# File 'lib/social_linker/subject.rb', line 110

def hashtags
  @options[:hashtags]
end

#image_urlObject



74
75
76
# File 'lib/social_linker/subject.rb', line 74

def image_url
  @options[:image_url]
end

#mediaObject

default media accessor

Returns:

  • String with media-url



100
101
102
# File 'lib/social_linker/subject.rb', line 100

def media
  @options[:media]
end

#merge!(options) ⇒ Object Also known as: update

Merges existing SocialLinker::Subject with a (potentially limited set of) new options

options accepts:

  • tags

  • url

  • title

  • image_url & image_type(image/jpeg, image/png)

  • description

  • facebook_app_id

  • twitter_username

  • render_site_title_postfix

  • … and more often medium specific attributes…

Note by default tracking parameters are added, turn this off by passing ‘utm_parameters: false`

Raises:

  • (ArgumentError)


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/social_linker/subject.rb', line 179

def merge!(options)
  options = options.options if options.is_a? SocialLinker::Subject
  @options.merge!(options)
  @options[:render_site_title_postfix] = true if @options[:render_site_title_postfix].nil?
  @options[:u] = @options[:url] unless @options[:u]
  @options[:media] = @options[:image_url] unless @options[:media]
  @options[:description] = @options[:summary] unless @options[:description]
  @options[:summary] = @options[:description] unless @options[:summary]
  @options[:title] = "#{ strip_string(@options[:summary], 120) }" unless @options[:title]
  @options[:description] = @options[:title] unless @options[:description]
  @options[:subject] = @options[:title] unless @options[:subject]
  @options[:via] = @options[:twitter_username] unless @options[:via]
  @options[:url] = @options[:media] unless @options[:url]
  raise ArgumentError, "#{url} is not a valid url" if @options[:url] and !@options[:url].include?('//')

  @options[:text] = "#{@options[:title]} #{@options[:url]}" unless @options[:text] #facebook & whatsapp native
  @options[:canonical_url] = @options[:url]
  @options[:share_url] = @options[:url]
  @options[:domain] = @options[:url].split(/\//)[0..2].join("/") if @options[:url] and !@options[:domain]

  if @options[:share_url] and utm_parameters
    unless @options[:share_url].match /utm_source/
      combine_with = @options[:share_url].match(/\?/) ? "&" : "?"
      @options[:share_url] = "#{@options[:share_url]}#{combine_with}utm_source=<%=share_source%>"
    end
    unless @options[:share_url].match /utm_medium/
      combine_with = "&"
      @options[:share_url] = "#{@options[:share_url]}#{combine_with}utm_medium=share_link"
    end
    unless @options[:share_url].match /utm_campaign/
      combine_with = "&"
      @options[:share_url] = "#{@options[:share_url]}#{combine_with}utm_campaign=social"
    end
  end
  if @options[:tags]
    @options[:tags].compact!
    @options[:hashtags] = @options[:tags][0..1].collect{|a| camelize_tag_when_needed(a) }.join(",") if @options[:tags] and !@options[:hashtags]
  end

  # make sure urls are absolute
  @options[:url] = prefix_domain(@options[:url],@options[:domain])
  @options[:share_url] = prefix_domain(@options[:share_url],@options[:domain])
  @options[:canonical_url] = prefix_domain(@options[:canonical_url],@options[:domain])
  @options[:image_url] = prefix_domain(@options[:image_url],@options[:domain])
  @options[:media] = prefix_domain(@options[:media],@options[:domain])

  @options.each do |k,v|
    @options[k] = v.strip if v and v.is_a? String
  end
  self
end

#optionsObject

Returns the given options, extended with the (derived) defaults

Returns:

  • Hash with the options



291
292
293
# File 'lib/social_linker/subject.rb', line 291

def options
  @options
end

#prefix_domain(path, domain) ⇒ Object

It is assumed that paths are relative to the domainname if none is given

Parameters:

  • path (String)

    to file (if it is already a full url, it will be passed along)

  • domain (String)

    of the file

Returns:

  • String with full url



278
279
280
281
282
283
284
285
286
# File 'lib/social_linker/subject.rb', line 278

def prefix_domain path, domain
  return_string = path
  if path and !path.include?("//")
    path.gsub!(/^\//,'')
    domain.gsub!(/\/$/,'')
    return_string = [domain,path].join("/")
  end
  return_string
end

#quote_string(string) ⇒ String

puts quotes around a string

Returns:

  • (String)

    now with quotes.



116
117
118
# File 'lib/social_linker/subject.rb', line 116

def quote_string(string)
  "“#{string}”" if string and string.to_s.strip != ""
end

Generates a share link for each of the predefined platforms in the ‘SHARE_TEMPLATES` constant

Parameters:

  • platform (Symbol)

    to generate the link for



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/social_linker/subject.rb', line 298

def share_link(platform)
  share_options = SHARE_TEMPLATES[platform]
  raise "No share template defined" unless share_options

  url_params = {}
  share_options[:params].each do |k,v|
    value_key = v||k #smartassery; v = nil for arrays
    value = options[value_key]
    value = self.send(value_key) if self.methods.include?(value_key)
    if value and value.to_s.strip != ""
      value = value.gsub('<%=share_source%>', platform.to_s)
      url_params[k] = value
    end
  end

  return share_options[: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

Parameters:

  • string (String)

    that is about to be shortened

  • max_length (Integer) (defaults to: 100)

    of the string to be shortened (default 100)

Returns:

  • (String)

    shortened to the max lenght



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(strip = false) ⇒ Object

default summary accessor

Returns:

  • String with summary



94
95
96
# File 'lib/social_linker/subject.rb', line 94

def summary(strip=false)
  strip ? strip_string(@options[:summary], 300) : @options[:summary]
end

#tagsObject

default tags accessor



106
107
108
# File 'lib/social_linker/subject.rb', line 106

def tags
  @options[:tags] ? @options[:tags] : []
end

#titleObject

default title accessor

Returns:

  • String with title



88
89
90
# File 'lib/social_linker/subject.rb', line 88

def title
  @options[:title]
end

#twitter_hash_tagsObject

Turns the first two tags in to tweetable hash tags Conform recommendation never to have more than 2 tags in a twitter message

Returns:

  • String with two tags as #tags.



250
251
252
# File 'lib/social_linker/subject.rb', line 250

def twitter_hash_tags
  options[:tags] ? hashtag_string(options[:tags][0..1]) : ""
end

#twitter_textObject

Generatess the text to tweet (Twitter)

Returns:

  • String with text to tweet



256
257
258
259
260
261
262
# File 'lib/social_linker/subject.rb', line 256

def twitter_text
  return options[:twitter_text] if options[:twitter_text]
  return options[:tweet_text] if options[:tweet_text]

  max_length = 140 - (twitter_hash_tags.length + 12 + 4) #hashstring + url length (shortened) + spaces
  "#{quote_string(strip_string(@options[:title],max_length))}"
end

#twitter_text_with_url_and_hashagsObject Also known as: status

Generates a full twitter message includig url and hashtags

Returns:

  • String with full twitter message (typically for native app)



266
267
268
269
270
271
# File 'lib/social_linker/subject.rb', line 266

def twitter_text_with_url_and_hashags
  return options[:twitter_text_with_url_and_hashags] if options[:twitter_text_with_url_and_hashags]
  return options[:message] if options[:message]
  return options[:status] if options[:status]
  [twitter_text,twitter_hash_tags,share_url].delete_if{|a| a.nil? or a.empty?}.join(" ")
end

#urlObject

default url accessor

Returns:

  • String with url



70
71
72
# File 'lib/social_linker/subject.rb', line 70

def url
  @options[:url]
end

#url_encode(v) ⇒ Object



316
317
318
# File 'lib/social_linker/subject.rb', line 316

def url_encode(v)
  ERB::Util.url_encode(v)
end

#utm_parametersObject



78
79
80
# File 'lib/social_linker/subject.rb', line 78

def utm_parameters
  [nil, true].include?(@options[:utm_parameters]) ? true : false
end