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: :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

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(options={})
  # basic option syncing
  @options = options
  @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]
  @options[:text] = "#{@options[:title]} #{@options[:url]}" unless @options[:text] #facebook & whatsapp native
  @options[:canonical_url] = @options[:url]
  if @options[:url] and utm_parameters
    unless @options[:url].match /utm_source/
      combine_with = @options[:url].match(/\?/) ? "&" : "?"
      @options[:url] = "#{@options[:url]}#{combine_with}utm_source=<%=share_source%>"
    end
    unless @options[:url].match /utm_medium/
      combine_with = "&"
      @options[:url] = "#{@options[:url]}#{combine_with}utm_medium=share_link"
    end
    unless @options[:url].match /utm_campaign/
      combine_with = "&"
      @options[:url] = "#{@options[: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]
    @options[:hash_string] = @options[:tags] ? hashtag_string(@options[:tags][0..1]) : ""
  end
  unless @options[:tweet_text]
    max_length = 140 - ((@options[:hash_string] ? @options[:hash_string].length : 0) + 12 + 4) #hashstring + url length (shortened) + spaces
    @options[:tweet_text] = "#{quote_string(strip_string(@options[:title],max_length))}"
  end
  @options[:message] = [@options[:tweet_text],@options[:url],@options[:hash_string]].compact.join(" ") unless @options[:message]
  @options[:status] = @options[:message] unless @options[:status]
  unless @options[:body]
    @options[:body] = ""
    @options[:body] += "#{@options[:summary]}\n" if @options[:summary]
    @options[:body] += "\n#{@options[:url]}\n" if @options[:url]
    @options[:body] += "\n#{@options[:description]}\n" if @options[:summary] != @options[:description] and @options[:description]
    @options[:body] += "\n#{@options[:media]}\n" if @options[:media] != @options[:url] and @options[:media]
    @options[:body] += "\n\n#{hashtag_string(@options[:tags])}" if @options[:tags]
    @options[:body] = nil if @options[:body].strip == ""
  end
  @options[:domain] = @options[:url].split(/\//)[0..2].join("/") if @options[:url] and !@options[:domain]

  @options.each do |k,v|
    @options[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 options[m]
    return options[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)

Parameters:

  • tag (String)

    to might need conversion

Returns:

  • (String)

    fixed tag



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_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
57
58
59
60
# 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) }
    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

#hashtagsObject



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

def hashtags
  @options[:hashtags]
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

#optionsObject

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

Returns:

  • Hash with the options



207
208
209
# File 'lib/social_linker/subject.rb', line 207

def options
  @options
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



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_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]
    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

#summaryObject

default summary accessor

Returns:

  • String with summary



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

def summary
  @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

#urlObject

default url accessor

Returns:

  • String with url



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

def url
  @options[: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_parametersObject



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

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