class SocialLinker::Subject

Constants

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

Public Class Methods

new(options={}) click to toggle source

Initialize the SocialLinker::Subject

options accepts:

  • tags

  • url

  • title

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

  • width and height for the images

  • 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`

@params [Hash] options as defined above

# File lib/social_linker/subject.rb, line 224
def initialize(options={})
  # basic option syncing
  @options = {}
  self.merge!(options)
end

Public Instance Methods

body() click to toggle source

Generates a large body of text (typical for email) @return String

# File lib/social_linker/subject.rb, line 280
def body
  return options[:body] if options[:body]
  rv = ""
  rv += "#{summary}\n" if summary
  rv += "\n#{share_url}\n" if share_url
  rv += "\n#{description}\n" if summary != description and description
  rv += "\n#{@options[:media]}\n" if options[:media] != 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) click to toggle source

single world tags don't need any processing, but tags consisting of different words do (before they can use in hashtags following convention)

@param [String] tag to might need conversion @return [String] fixed tag

# File lib/social_linker/subject.rb, line 65
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() click to toggle source
# File lib/social_linker/subject.rb, line 122
def canonical_url
  prefix_domain((@options[:canonical_url] || @options[:url]), @options[:domain])
end
description() click to toggle source
# File lib/social_linker/subject.rb, line 293
def description
  @options[:description] || @options[:summary]
end
filename_derived_image_type() click to toggle source
# File lib/social_linker/subject.rb, line 156
def filename_derived_image_type
  if media
    extension = media.to_s.split(".").last.downcase
    if extension == "jpg" or extension == "jpeg"
      "image/jpeg"
    elsif extension == "png"
      "image/png"
    elsif extension == "gif"
      "image/gif"
    end
  end
end
hashtag_string(tags) click to toggle source

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

@param [Array] tags to be converted to string @return [String] containing a Twitter-style tag-list

# File lib/social_linker/subject.rb, line 52
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(" #")}"
  else
    ""
  end
end
hashtags() click to toggle source
# File lib/social_linker/subject.rb, line 179
def hashtags
  @options[:hashtags]
end
image_type() click to toggle source
# File lib/social_linker/subject.rb, line 169
def image_type
  @options[:image_type] || filename_derived_image_type
end
image_url() click to toggle source
# File lib/social_linker/subject.rb, line 77
def image_url
  @options[:image_url]
end
media() click to toggle source

default media accessor @return String with media-url

# File lib/social_linker/subject.rb, line 152
def media
  @options[:media]
end
media_dimensions() click to toggle source
# File lib/social_linker/subject.rb, line 81
def media_dimensions
  return @media_dimensions if @media_dimensions
  if media
    @media_dimensions = @options[:media_dimensions]
    if @media_dimensions.is_a? Array
      @media_dimensions = {
        width: @media_dimensions[0],
        height: @media_dimensions[1]
      }
    end
    @media_dimensions ||= {
      width: @options[:media_width],
      height: @options[:media_height]
    }
  else
    @media_dimensions = {}
  end
end
media_height() click to toggle source
# File lib/social_linker/subject.rb, line 104
def media_height
  media_dimensions[:height].to_i if media_dimensions[:height]
end
media_width() click to toggle source
# File lib/social_linker/subject.rb, line 100
def media_width
  media_dimensions[:width].to_i if media_dimensions[:width]
end
merge!(options) click to toggle source

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`

@params [Hash, SocialLinker::Subject] options as defined above @return SocialLinker::Subject (self)

# File lib/social_linker/subject.rb, line 249
def merge!(options)
  options = options.options if options.is_a? SocialLinker::Subject
  options[:render_site_title_postfix] = true if options[:render_site_title_postfix].nil?
  options[:u] ||= options[:url] if options[:url]
  options[:media] ||= options[:image_url] if options[:image_url]
  options[:subject] ||= options[:title] if options[:title]
  options[:via] ||= options[:twitter_username] if options[:twitter_username]
  options[:text] = "#{options[:title]} #{options[:url]}" unless options[:text] #facebook & whatsapp native
  options[:domain] = options[:url].split(/\//)[0..2].join("/") if options[:url] and !options[:domain]
  options.select!{|k,v| !v.nil?}
  @options.merge!(options)

  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[: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
Also aliased as: update
method_missing(m,*args) click to toggle source

Catches method missing and tries to resolve them in either an appropriate share link or option value

Calls superclass method
# File lib/social_linker/subject.rb, line 372
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
options() click to toggle source

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

@return Hash with the options

# File lib/social_linker/subject.rb, line 342
def options
  @options
end
prefix_domain(path, domain) click to toggle source

It is assumed that paths are relative to the domainname if none is given @param [String] path to file (if it is already a full url, it will be passed along) @param [String] domain of the file @return String with full url

# File lib/social_linker/subject.rb, line 328
def prefix_domain path, domain
  if path and !path.include?("//")
    return [
      domain.gsub(/\/$/,''),
      path.gsub(/^\//,'')
    ].join("/")
  else
    return path
  end
end
quote_string(string) click to toggle source

puts quotes around a string @return [String] now with quotes.

# File lib/social_linker/subject.rb, line 185
def quote_string(string)
  "“#{string}”" if string and string.to_s.strip != ""
end
share_url() click to toggle source
# File lib/social_linker/subject.rb, line 126
def share_url
  url_to_share = prefix_domain((@options[:share_url] || @options[:url]), @options[:domain])
  if utm_parameters?
    utm_url_params = utm_parameters.collect{|k,v| "#{k}=#{v}" unless url_to_share.match(k.to_s)}.compact.join("&")
    combine_with = url_to_share.match(/\?/) ? "&" : "?"
    return "#{url_to_share}#{combine_with}#{utm_url_params}"
  else
    return url_to_share
  end
end
status()
strip_string(string, max_length=100) click to toggle source

strips a string to the max length taking into account quoting @param [String] string that is about to be shortened @param [Integer] max_length of the string to be shortened (default 100) @return [String] shortened to the max lenght

# File lib/social_linker/subject.rb, line 193
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) click to toggle source

default summary accessor @return String with summary

# File lib/social_linker/subject.rb, line 145
def summary(strip=false)
  summ = @options[:summary] || @options[:description]
  strip ? strip_string(summ, 300) : summ
end
tags() click to toggle source

default tags accessor @return Array<String> with tags

# File lib/social_linker/subject.rb, line 175
def tags
  @options[:tags] ? @options[:tags] : []
end
title() click to toggle source

default title accessor @return String with title

# File lib/social_linker/subject.rb, line 139
def title
  @options[:title] || "#{ strip_string(options[:summary], 120) }"
end
twitter_hash_tags() click to toggle source

Turns the first two tags in to tweetable hash tags Conform recommendation never to have more than 2 tags in a twitter message @return String with two tags as tags.

# File lib/social_linker/subject.rb, line 300
def twitter_hash_tags
  options[:tags] ? hashtag_string(options[:tags][0..1]) : ""
end
twitter_text() click to toggle source

Generatess the text to tweet (Twitter) @return String with text to tweet

# File lib/social_linker/subject.rb, line 306
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_hashags() click to toggle source

Generates a full twitter message includig url and hashtags @return String with full twitter message (typically for native app)

# File lib/social_linker/subject.rb, line 316
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
Also aliased as: status
update(options)
Alias for: merge!
url() click to toggle source

default url accessor

@return String with url

# File lib/social_linker/subject.rb, line 73
def url
  @options[:url] || image_url
end
url_encode(v) click to toggle source
# File lib/social_linker/subject.rb, line 367
def url_encode(v)
  ERB::Util.url_encode(v)
end
utm_parameters() click to toggle source
# File lib/social_linker/subject.rb, line 112
def utm_parameters
  if utm_parameters?
    {
      utm_source: "<%=share_source%>",
      utm_medium: "share_link",
      utm_campaign: "social"
    }
  end
end
utm_parameters?() click to toggle source
# File lib/social_linker/subject.rb, line 108
def utm_parameters?
  [nil, true].include?(@options[:utm_parameters]) ? true : false
end