social_linker 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 288dc63f2019995af9cc826b611f6833f38b4e2a
4
- data.tar.gz: 929cb7b9986b851dc5474e51231b24a718c22075
3
+ metadata.gz: 130065e466b759dc45ad255c2d7edb4732157e96
4
+ data.tar.gz: d38b6a8f53b355fe42d21d6b66ac16c8ab1eb3a3
5
5
  SHA512:
6
- metadata.gz: 8e0a9bd53da2b2b39ee8f23fd916919eadaab9d470def0b807278f27335f639693c0efee9d52ebbe893bdce9895b3a1f678923c48f6f5f54553596540083e914
7
- data.tar.gz: 0d45542309ab9e6a830dfdd7b8f6653801a01c9b0fb608215daee1e8ba64bd259f29958772ea536efda50a8f9ce5c8eb5eb509a64da7346f052e7282dfadb594
6
+ metadata.gz: d1d021035b356fd120e6699a4c811f282dbfbb28b81780d8ff120eb39bbdde6abc964eac090cee2b3c81df3ff240a169bb01cd7517a4cd344d8781a72f10c590
7
+ data.tar.gz: 37ec6ed35bb5904cc0d567c0cf4b64584daab334237cc1230a155cfd46b653e9c7dade19b53a862740aa94694d4f2aa9c2f658d3ebdfd7681f76b9a9bebc0f6d
data/README.md CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.org/murb/social_linker.svg?branch=master)](https://travis-ci.org/murb/social_linker)
4
4
 
5
+ **SocialLinker solves two problems involving social networks (and search engines):
6
+ proper meta-headers and proper share links.**
7
+
5
8
  SocialLinker is able to generate the most common share links for you, without depending on JavaScript.
6
9
  You should use generated links, instead of the share buttons provided by the platforms themselves, to
7
10
  protect your user's privacy, and this gem makes it easy for you to do so.
@@ -28,6 +31,8 @@ Or install it yourself as:
28
31
 
29
32
  ## Usage
30
33
 
34
+ ### Basics
35
+
31
36
  Initialize the subject with enough material to generate links from, such as the page's url, maybe the image url (mainly for Pinterest type-shares), a description, tags etc.
32
37
 
33
38
  For example, initialze
@@ -46,21 +51,7 @@ Which will deliver you the following url:
46
51
 
47
52
  mailto:emailaddress?subject=Example%20website&body=Example.com%20is%20the%20typical%20URL%20you%20would%20want%20to%20use%20in%20explanations%20anyway.%0A%0Ahttp%3A%2F%2Fexample.com%2F
48
53
 
49
- Currently support is available for the following ways of sharing:
50
-
51
- :email
52
- :facebook
53
- :facebook_native
54
- :twitter
55
- :twitter_native
56
- :pinterest
57
- :google
58
- :linkedin
59
- :whatsapp
60
-
61
- Or to save you the copy-paste:
62
-
63
- [TestMailLink](mailto:emailaddress?subject=Example%20website&body=Example.com%20is%20the%20typical%20URL%20you%20would%20want%20to%20use%20in%20explanations%20anyway.%0A%0Ahttp%3A%2F%2Fexample.com%2F)
54
+ ### Setting up the subject
64
55
 
65
56
  The supported options are:
66
57
 
@@ -71,9 +62,16 @@ The supported options are:
71
62
  * title
72
63
  * tags
73
64
 
74
- I've tried to map them as good as possible to the different share tools. Sometimes by combining several values. You may also pass along link-specific parameters such as `:hashtags`, so no 2-tag long string is generated from the list of tags.
65
+ And of a more site-global nature:
66
+
67
+ * twitter_username
68
+ * facebook_app_id
69
+ * language
70
+ * site_title_postfix (which can be hidden by setting `render_site_title_postfix` to false)
75
71
 
76
- To conclude: a very complete instantiation:
72
+ I've tried to map them as good as possible to the different share tools. Sometimes by combining several values. You may also pass along link-specific parameters such as `:hashtags`, so you can control the 2-tag long string that is generated from the list of tags by default.
73
+
74
+ For example:
77
75
 
78
76
  @subject = SocialLinker::Subject.new(
79
77
  title: "title",
@@ -82,26 +80,54 @@ To conclude: a very complete instantiation:
82
80
  image_type: 'image/jpeg',
83
81
  summary: "short summary",
84
82
  tags: ["key1", "key2", "key3"],
85
- twitter_username: 'murb'
86
83
  )
87
84
 
88
- ## UTM Campaign parameters
85
+ You can also merge details later on. Let's say you want to set the global values:
89
86
 
90
- By default [utm campaign parameters](https://support.google.com/analytics/answer/1033863?hl=en) are added when they are not present. You can turn this off by passing the option: `utm_parameters: false`.
87
+ @subject = SocialLinker::Subject.new(
88
+ site_title_postfix: ":murb:",
89
+ twitter_username: 'murb',
90
+ facebook_app_id: '123123123',
91
+ tags: ['murb', 'ruby']
92
+ )
91
93
 
92
- ## Rails helpers
94
+ You might to set these defaults in a before action in your (Ruby on Rails-speak)
95
+ ApplicationController. Later on you can merge details into this subject:
93
96
 
94
- When using Ruby on Rails a few helpers have been created.
97
+ @subject.merge!({
98
+ title: "title",
99
+ url: "https://murb.nl/blog",
100
+ image_url: "https://murb.nl/image.jpg",
101
+ image_type: 'image/jpeg',
102
+ summary: "short summary",
103
+ tags: ["key1", "key2", "key3"]
104
+ })
95
105
 
96
- ### OpenGraph, Twitter, and HTML meta-data:
106
+ ### Creating share links
97
107
 
98
- Just set the following, which should give you a reasonable default.
99
108
 
100
- header_meta_tags(@subject, {
101
- site_title_postfix: "your sitename" # optional
102
- })
109
+ Currently support is available for the following ways of sharing:
110
+
111
+ :email
112
+ :facebook
113
+ :facebook_native
114
+ :twitter
115
+ :twitter_native
116
+ :pinterest
117
+ :google
118
+ :linkedin
119
+ :whatsapp
103
120
 
104
- ### Link helper with SVG icons
121
+ Or to save you the copy-paste:
122
+
123
+ [TestMailLink](mailto:emailaddress?subject=Example%20website&body=Example.com%20is%20the%20typical%20URL%20you%20would%20want%20to%20use%20in%20explanations%20anyway.%0A%0Ahttp%3A%2F%2Fexample.com%2F)
124
+
125
+
126
+ #### UTM Campaign parameters
127
+
128
+ By default [utm campaign parameters](https://support.google.com/analytics/answer/1033863?hl=en) are added when they are not present. You can turn this off by passing the option: `utm_parameters: false`.
129
+
130
+ #### Link helper with SVG icons (Rails)
105
131
 
106
132
  Use the following to create a sharelink to Facebook
107
133
 
@@ -126,7 +152,26 @@ the following line to the head of your application.ccs file:
126
152
 
127
153
  *= require social_linker/icons
128
154
 
129
- #### Reuse the SVG icons elsewhere
155
+
156
+ ### Meta-Headers
157
+
158
+ When using Ruby on Rails a few helpers have been created.
159
+
160
+
161
+ Just set the following, which should give you a reasonable default.
162
+
163
+ header_meta_tags(@subject, {
164
+ site_title_postfix: "your sitename" # optional
165
+ })
166
+
167
+ Alternatively you can also set the `site_title_post` in the `Subject` directly,
168
+ as suggested in an earlier section:
169
+
170
+ header_meta_tags(@subject)
171
+
172
+ ## Advanced
173
+
174
+ ### Reuse the SVG icons elsewhere
130
175
 
131
176
  When integrating social icons into your site, you might also want to include login options
132
177
  for these social networks, or access the icons for other reasons. Below is 'standard'
@@ -136,6 +181,11 @@ example code of how to access image-assets in the icon
136
181
  <use xlink:href="<%=image_path('social_linker/icons.svg')%>#icon-facebook"></use>
137
182
  </svg>
138
183
 
184
+ *Note:* If you just want the SVG icon standalone, you can also use the `social_link_to_image`
185
+ view helper:
186
+
187
+ social_link_to_image :facebook
188
+
139
189
  Included layers:
140
190
 
141
191
  * icon-email
@@ -152,7 +202,9 @@ Included layers:
152
202
  * icon-whatsapp
153
203
  * icon-tumblr
154
204
 
155
- #### Tip: SVG4Everyone
205
+ ## Problem solving
206
+
207
+ ### SVG Icons not showing up in older browsers
156
208
 
157
209
  When using SVG and serving your pages to older browsers, make sure you use something
158
210
  like SVG4Everyone. Include in your gemfile:
@@ -165,6 +217,7 @@ and include the following line to the head of your `application.js` file:
165
217
 
166
218
  //= require svg4everybody
167
219
 
220
+
168
221
  ## TODO
169
222
 
170
223
  * Idea: maybe improve share helpers with [javascript timeout workarounds](http://stackoverflow.com/questions/7231085/how-to-fall-back-to-marketplace-when-android-custom-url-scheme-not-handled) for native alternatives (although Twitter and facebook work well)
@@ -6,7 +6,13 @@ module ViewHelpers
6
6
  def meta_tag(name, content)
7
7
  content = erb_sanitized(content)
8
8
  name = erb_sanitized(name)
9
- name_or_property_attribute = (name.start_with?("og:") or name.start_with?("fb:")) ? "property" : "name"
9
+ name_or_property_attribute = if (name.start_with?("og:") or name.start_with?("fb:"))
10
+ "property"
11
+ elsif ['Content-Language'].include? name
12
+ "http-equiv"
13
+ else
14
+ "name"
15
+ end
10
16
  "<meta #{name_or_property_attribute}=\"#{name}\" content=\"#{content}\" />" if content and content.to_s.strip != ""
11
17
  end
12
18
 
@@ -21,18 +27,29 @@ module ViewHelpers
21
27
  # header_meta_tags renders the most important metatags based on the SocialLinker::Subject
22
28
  #
23
29
  # @param [SocialLinker::Subject] the SocialLinker::Subject initialized as complete as possible
24
- # @param [Hash] options with site-defaults for `:site_title_postfix`, (e.g. article title - {site title postfix here}), `:domain` (the main url),
30
+ # @param [Hash] options with site-defaults for `:site_title_postfix`, (e.g. article title - {site title postfix here}), `:domain` (the main url). These options are overridden by the subject if set by the subject.
25
31
  # @return String of tags (possibly marked as sanitized when available)
26
32
  def header_meta_tags subject, options={}
33
+ options = options.merge(subject.options) if subject
34
+
27
35
  site_title_postfix = options[:site_title_postfix]
36
+ site_name = options[:site_name] || site_title_postfix
37
+ site_title_postfix = nil if options[:render_site_title_postfix] == false
38
+ language = options[:language]
39
+ domain = options[:domain]
40
+
28
41
  header_html = []
29
- if subject
30
- domain = options[:domain] || subject.options[:domain]
31
42
 
43
+ header_html << meta_tag("twitter:site", options[:twitter_username])
44
+ header_html << meta_tag("twitter:creator", options[:twitter_username])
45
+ header_html << meta_tag("twitter:domain", domain)
46
+ header_html << meta_tag("Content-Language", language)
47
+ header_html << meta_tag("dc.language", language)
48
+ header_html << meta_tag("og:locale", language)
49
+ header_html << meta_tag("fb:app_id", options[:facebook_app_id])
50
+
51
+ if subject
32
52
  header_html << meta_tag("twitter:card", subject.media ? :summary_large_image : :summary)
33
- header_html << meta_tag("twitter:site", subject.options[:twitter_username])
34
- header_html << meta_tag("twitter:creator", subject.options[:twitter_username])
35
- header_html << meta_tag("twitter:domain", domain)
36
53
 
37
54
  if subject.url
38
55
  header_html << meta_tag("og:url", subject.canonical_url)
@@ -40,12 +57,11 @@ module ViewHelpers
40
57
  end
41
58
 
42
59
  header_html << meta_tag("keywords", subject.tags.join(" "))
43
- header_html << meta_tag("description", subject.summary(true))
60
+ header_html << meta_tag("description", subject.summary(false))
44
61
 
45
62
  header_html << meta_tag("twitter:description", subject.summary(true))
46
- header_html << meta_tag("og:description", subject.summary(true))
63
+ header_html << meta_tag("og:description", subject.summary(false))
47
64
 
48
- header_html << meta_tag("fb:app_id", subject.options[:facebook_app_id])
49
65
 
50
66
  if subject.media
51
67
  header_html << meta_tag("twitter:image:src", subject.media)
@@ -60,7 +76,7 @@ module ViewHelpers
60
76
  header_html << "<title>#{erb_sanitized(site_title)}</title>"
61
77
  header_html << meta_tag("twitter:title", title)
62
78
  header_html << meta_tag("og:title", title)
63
- header_html << meta_tag("og:site_name", site_title_postfix)
79
+ header_html << meta_tag("og:site_name", site_name)
64
80
 
65
81
  header_html.compact!
66
82
  header_html = header_html.join("\n") if header_html
@@ -76,25 +92,45 @@ module ViewHelpers
76
92
  # * social_icons_image_path (defaults to the default SocialLinker iconset)
77
93
  # * title (the title attribute, defaults to the network's name capitalized)
78
94
 
79
- def social_link_to_image(network, image_path)
95
+ def social_link_to_image(network, image_path=nil)
96
+ if image_path == nil
97
+ image_path = image_path('social_linker/icons.svg') if self.methods.include?(:image_path)
98
+ end
99
+
80
100
  if network and image_path
81
- "<svg class=\"icon icon-#{network} icon-default-style\"><use xlink:href=\"#{image_path}#icon-#{network}\"></use></svg>"
101
+ html = "<svg class=\"icon icon-#{network} icon-default-style\"><title>#{network.capitalize}</title><use xlink:href=\"#{image_path}#icon-#{network}\"></use></svg>"
102
+ html = html.html_safe if html.methods.include?(:html_safe)
103
+ html
82
104
  end
83
105
  end
84
106
 
107
+ # Generates the <a href> code for the subject and network
108
+ # By default it will use the #social_link_to_image - function, refer to
109
+ # that function if you don't see the icons rendered.
110
+ #
111
+ # Options:
112
+ # @param [SocialLinker::Subject] the SocialLinker::Subject initialized as complete as possible
113
+ # @param [Symbol] network key (e.g. twitter, facebook, see README and/or SocialLinker::Subject::SHARE_TEMPLATES )
114
+ # params [Hash] options:
115
+ # * :social_icons_image_path (defaults to the default SocialLinker iconset)
116
+ # * :title (the title attribute, defaults to the network's name capitalized)
117
+ # * :target_blank (boolean whether it should open in a new window)
118
+ # * :class (array or string of classes to add to the a-href element)
119
+ # @return String of html (possibly marked as sanitized when available)
120
+
85
121
  def social_link_to subject, network, options = {}
86
122
  raise ArgumentError, "subject can't be nil" unless subject
87
123
  raise ArgumentError, "network can't be nil" unless network
88
124
  options_with_defaults = {
89
125
  social_icons_image_path: 'social_linker/icons.svg',
90
126
  title: network.to_s.capitalize,
91
- target_blank: true
127
+ target_blank: true,
92
128
  }.merge(options)
93
129
 
94
130
  link_content = network
95
131
 
96
132
  if block_given?
97
- link_content = yield
133
+ link_content = capture{ yield }
98
134
  else
99
135
  social_icons_image_path = options_with_defaults[:social_icons_image_path]
100
136
  social_icons_image_path = image_path(social_icons_image_path) if self.methods.include?(:image_path)
@@ -103,8 +139,9 @@ module ViewHelpers
103
139
  end
104
140
 
105
141
  title = options_with_defaults[:title]
142
+ html_class = [options_with_defaults[:class], network].flatten.compact.join(" ")
106
143
  targetblank = options_with_defaults[:target_blank] ? " target=\"_blank\"" : ""
107
- html = "<a href=\"#{erb_sanitized(subject.share_link(network))}\"#{targetblank} class=\"#{network}\" title=\"#{title}\">#{link_content}</a>"
144
+ html = "<a href=\"#{erb_sanitized(subject.share_link(network))}\"#{targetblank} class=\"#{html_class}\" title=\"#{title}\">#{link_content}</a>"
108
145
  html = html.html_safe if html.methods.include?(:html_safe)
109
146
  html
110
147
  end
@@ -11,27 +11,27 @@ module SocialLinker
11
11
  },
12
12
  pinterest: {
13
13
  base: "https://pinterest.com/pin/create/button/?",
14
- params: {url: :url, media: :media, description: :title}
14
+ params: {url: :share_url, media: :media, description: :title}
15
15
  },
16
16
  linkedin: {
17
17
  base: "https://www.linkedin.com/shareArticle?mini=true&",
18
- params: [:url, :title, :summary, :source]
18
+ params: {url: :share_url, title: :title, summary: :summary, source: :source}
19
19
  },
20
20
  google: {
21
21
  base: "https://plus.google.com/share?",
22
- params: [:url]
22
+ params: {url: :share_url}
23
23
  },
24
24
  twitter: {
25
25
  base: "https://twitter.com/intent/tweet?",
26
- params: {text: :tweet_text, via: :via, url: :url, hashtags: :hashtags}
26
+ params: {text: :twitter_text, via: :via, url: :share_url, hashtags: :hashtags}
27
27
  },
28
28
  twitter_native: {
29
29
  base: "twitter://post?",
30
- params: [:message]
30
+ params: {message: :twitter_text_with_url_and_hashags}
31
31
  },
32
32
  facebook: {
33
33
  base: "https://www.facebook.com/sharer/sharer.php?",
34
- params: [:u]
34
+ params: {u: :share_url}
35
35
  },
36
36
  facebook_native: {
37
37
  base: "fb://publish/profile/me?",
@@ -51,11 +51,7 @@ module SocialLinker
51
51
  def hashtag_string(tags)
52
52
  if tags and tags.count > 0
53
53
  tags = tags.collect{|a| camelize_tag_when_needed(a) }
54
- string = "##{tags.collect{|a| a.to_s.strip.gsub('#','')}.join(" #")}"
55
- if string and string.length > 60
56
- puts "WARNING: string of tags longer than adviced lenght of 60 characters: #{string}"
57
- end
58
- string
54
+ "##{tags.collect{|a| a.to_s.strip.gsub('#','')}.join(" #")}"
59
55
  end
60
56
  end
61
57
 
@@ -147,6 +143,8 @@ module SocialLinker
147
143
  # * description
148
144
  # * facebook_app_id
149
145
  # * twitter_username
146
+ # * language
147
+ # * site_title_postfix
150
148
  # * ... and more often medium specific attributes...
151
149
  #
152
150
  # Note by default tracking parameters are added, turn this off by passing
@@ -155,13 +153,38 @@ module SocialLinker
155
153
  # @params [Hash] options as defined above
156
154
  def initialize(options={})
157
155
  # basic option syncing
158
- @options = options
159
- @options[:facebook_app_id] = @options[:facebook_app_id]
160
- @options[:u] = @options[:url] unless options[:u]
161
- @options[:media] = @options[:image_url] unless options[:media]
162
- @options[:description] = @options[:summary] unless options[:description]
163
- @options[:summary] = @options[:description] unless options[:summary]
164
- @options[:title] = "#{ strip_string(@options[:summary], 120) }" unless options[:title]
156
+ @options = {}
157
+ self.merge!(options)
158
+ end
159
+
160
+ # Merges existing SocialLinker::Subject with a (potentially limited set of)
161
+ # new options
162
+ #
163
+ # options accepts:
164
+ # * tags
165
+ # * url
166
+ # * title
167
+ # * image_url & image_type(image/jpeg, image/png)
168
+ # * description
169
+ # * facebook_app_id
170
+ # * twitter_username
171
+ # * render_site_title_postfix
172
+ # * ... and more often medium specific attributes...
173
+ #
174
+ # Note by default tracking parameters are added, turn this off by passing
175
+ # `utm_parameters: false`
176
+ #
177
+ # @params [Hash, SocialLinker::Subject] options as defined above
178
+ # @return SocialLinker::Subject (self)
179
+ def merge!(options)
180
+ options = options.options if options.is_a? SocialLinker::Subject
181
+ @options.merge!(options)
182
+ @options[:render_site_title_postfix] = true if @options[:render_site_title_postfix].nil?
183
+ @options[:u] = @options[:url] unless @options[:u]
184
+ @options[:media] = @options[:image_url] unless @options[:media]
185
+ @options[:description] = @options[:summary] unless @options[:description]
186
+ @options[:summary] = @options[:description] unless @options[:summary]
187
+ @options[:title] = "#{ strip_string(@options[:summary], 120) }" unless @options[:title]
165
188
  @options[:description] = @options[:title] unless @options[:description]
166
189
  @options[:subject] = @options[:title] unless @options[:subject]
167
190
  @options[:via] = @options[:twitter_username] unless @options[:via]
@@ -170,53 +193,83 @@ module SocialLinker
170
193
 
171
194
  @options[:text] = "#{@options[:title]} #{@options[:url]}" unless @options[:text] #facebook & whatsapp native
172
195
  @options[:canonical_url] = @options[:url]
196
+ @options[:share_url] = @options[:url]
173
197
  @options[:domain] = @options[:url].split(/\//)[0..2].join("/") if @options[:url] and !@options[:domain]
174
198
 
175
- if @options[:url] and utm_parameters
176
- unless @options[:url].match /utm_source/
177
- combine_with = @options[:url].match(/\?/) ? "&" : "?"
178
- @options[:url] = "#{@options[:url]}#{combine_with}utm_source=<%=share_source%>"
199
+ if @options[:share_url] and utm_parameters
200
+ unless @options[:share_url].match /utm_source/
201
+ combine_with = @options[:share_url].match(/\?/) ? "&" : "?"
202
+ @options[:share_url] = "#{@options[:share_url]}#{combine_with}utm_source=<%=share_source%>"
179
203
  end
180
- unless @options[:url].match /utm_medium/
204
+ unless @options[:share_url].match /utm_medium/
181
205
  combine_with = "&"
182
- @options[:url] = "#{@options[:url]}#{combine_with}utm_medium=share_link"
206
+ @options[:share_url] = "#{@options[:share_url]}#{combine_with}utm_medium=share_link"
183
207
  end
184
- unless @options[:url].match /utm_campaign/
208
+ unless @options[:share_url].match /utm_campaign/
185
209
  combine_with = "&"
186
- @options[:url] = "#{@options[:url]}#{combine_with}utm_campaign=social"
210
+ @options[:share_url] = "#{@options[:share_url]}#{combine_with}utm_campaign=social"
187
211
  end
188
212
  end
189
213
  if @options[:tags]
190
214
  @options[:tags].compact!
191
215
  @options[:hashtags] = @options[:tags][0..1].collect{|a| camelize_tag_when_needed(a) }.join(",") if @options[:tags] and !@options[:hashtags]
192
- @options[:hash_string] = @options[:tags] ? hashtag_string(@options[:tags][0..1]) : ""
193
216
  end
194
- unless @options[:tweet_text]
195
- max_length = 140 - ((@options[:hash_string] ? @options[:hash_string].length : 0) + 12 + 4) #hashstring + url length (shortened) + spaces
196
- @options[:tweet_text] = "#{quote_string(strip_string(@options[:title],max_length))}"
197
- end
198
- @options[:message] = [@options[:tweet_text],@options[:url],@options[:hash_string]].compact.join(" ") unless @options[:message]
199
- @options[:status] = @options[:message] unless @options[:status]
200
217
 
201
218
  # make sure urls are absolute
202
219
  @options[:url] = prefix_domain(@options[:url],@options[:domain])
220
+ @options[:share_url] = prefix_domain(@options[:share_url],@options[:domain])
221
+ @options[:canonical_url] = prefix_domain(@options[:canonical_url],@options[:domain])
203
222
  @options[:image_url] = prefix_domain(@options[:image_url],@options[:domain])
204
223
  @options[:media] = prefix_domain(@options[:media],@options[:domain])
205
224
 
206
- unless @options[:body]
207
- @options[:body] = ""
208
- @options[:body] += "#{@options[:summary]}\n" if @options[:summary]
209
- @options[:body] += "\n#{@options[:url]}\n" if @options[:url]
210
- @options[:body] += "\n#{@options[:description]}\n" if @options[:summary] != @options[:description] and @options[:description]
211
- @options[:body] += "\n#{@options[:media]}\n" if @options[:media] != @options[:url] and @options[:media]
212
- @options[:body] += "\n\n#{hashtag_string(@options[:tags])}" if @options[:tags]
213
- @options[:body] = nil if @options[:body].strip == ""
214
- end
215
-
216
225
  @options.each do |k,v|
217
226
  @options[k] = v.strip if v and v.is_a? String
218
227
  end
228
+ self
229
+ end
230
+ alias_method :update, :merge!
231
+
232
+ # Generates a large body of text (typical for email)
233
+ # @return String
234
+ def body
235
+ return options[:body] if options[:body]
236
+ rv = ""
237
+ rv += "#{@options[:summary]}\n" if options[:summary]
238
+ rv += "\n#{@options[:share_url]}\n" if options[:share_url]
239
+ rv += "\n#{@options[:description]}\n" if options[:summary] != options[:description] and options[:description]
240
+ rv += "\n#{@options[:media]}\n" if options[:media] != options[:share_url] and options[:media]
241
+ rv += "\n\n#{hashtag_string(@options[:tags])}" if options[:tags]
242
+ rv.strip!
243
+ rv = nil if rv == ""
244
+ return rv
245
+ end
246
+
247
+ # Turns the first two tags in to tweetable hash tags
248
+ # Conform recommendation never to have more than 2 tags in a twitter message
249
+ # @return String with two tags as #tags.
250
+ def twitter_hash_tags
251
+ options[:tags] ? hashtag_string(options[:tags][0..1]) : ""
252
+ end
253
+
254
+ # Generatess the text to tweet (Twitter)
255
+ # @return String with text to tweet
256
+ def twitter_text
257
+ return options[:twitter_text] if options[:twitter_text]
258
+ return options[:tweet_text] if options[:tweet_text]
259
+
260
+ max_length = 140 - (twitter_hash_tags.length + 12 + 4) #hashstring + url length (shortened) + spaces
261
+ "#{quote_string(strip_string(@options[:title],max_length))}"
262
+ end
263
+
264
+ # Generates a full twitter message includig url and hashtags
265
+ # @return String with full twitter message (typically for native app)
266
+ def twitter_text_with_url_and_hashags
267
+ return options[:twitter_text_with_url_and_hashags] if options[:twitter_text_with_url_and_hashags]
268
+ return options[:message] if options[:message]
269
+ return options[:status] if options[:status]
270
+ [twitter_text,twitter_hash_tags,share_url].delete_if{|a| a.nil? or a.empty?}.join(" ")
219
271
  end
272
+ alias_method :status, :twitter_text_with_url_and_hashags
220
273
 
221
274
  # It is assumed that paths are relative to the domainname if none is given
222
275
  # @param [String] path to file (if it is already a full url, it will be passed along)
@@ -250,6 +303,7 @@ module SocialLinker
250
303
  share_options[:params].each do |k,v|
251
304
  value_key = v||k #smartassery; v = nil for arrays
252
305
  value = options[value_key]
306
+ value = self.send(value_key) if self.methods.include?(value_key)
253
307
  if value and value.to_s.strip != ""
254
308
  value = value.gsub('<%=share_source%>', platform.to_s)
255
309
  url_params[k] = value
@@ -1,3 +1,3 @@
1
1
  module SocialLinker
2
- VERSION = "0.3.3"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: social_linker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - murb
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-01 00:00:00.000000000 Z
11
+ date: 2017-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler