jaap3-addthis 0.5.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -4,11 +4,10 @@ Addthis.com provides widgets that make sharing, bookmarking and emailing pages,
4
4
  and subscribing to feeds easy for your visitors. It supports a broad range of
5
5
  social network sites and web applications.
6
6
 
7
- This plugin makes it easy to add plain or customized Addthis.com widgets to
8
- your Rails application. As a matter of fact, the code is framework agnostic
9
- and should be usable in other Ruby projects as well.
7
+ This helper makes it easy to add plain or customized Addthis.com widgets to
8
+ your Ruby (on Rails) application.
10
9
 
11
- = Installation
10
+ == Installation
12
11
 
13
12
  If you want to use the Rails 2.1 dependency manager add this environment.rb:
14
13
 
@@ -23,7 +22,7 @@ Or use it as a plain plugin:
23
22
 
24
23
  script/plugin install git://github.com/jaap3/addthis.git
25
24
 
26
- = Quick Start
25
+ == Quick Start
27
26
 
28
27
  The most basic use is simply providing no arguments:
29
28
 
@@ -33,7 +32,7 @@ The most basic use is simply providing no arguments:
33
32
  The code generated by Addthis will then try to figure out the url and title by
34
33
  itself. This is not possible for addthis_feed_button.
35
34
 
36
- You can set a custom url, a required argument addthis_feed_button:
35
+ You can set a custom url, a required argument for addthis_feed_button:
37
36
 
38
37
  addthis_bookmark_button("http://www.example.com/")
39
38
  addthis_email_button("http://www.example.com/")
@@ -52,7 +51,7 @@ To only set a custom title you can use to options hash:
52
51
  The options hash can be used to customize the widget to a great extent. Each of
53
52
  the shown examples can take the options hash as an optional extra argument.
54
53
 
55
- = Usage
54
+ == Usage
56
55
 
57
56
  If you wish to track analytics for your button, you must create an account at
58
57
  http://Addthis.com. Joining is free!
@@ -79,24 +78,20 @@ Besides overriding the default settings using the initializer, each helper
79
78
  method takes an options hash that can be used to override any setting for
80
79
  a specific button.
81
80
 
82
- = Secure pages
81
+ === Custom services
83
82
 
84
- Calling one of the addthis_button methods with the secure option set to true
85
- like this:
83
+ To change the initial list of services shown when opening the bookmark menu
84
+ pass the options hash a list of services you wish to see.
86
85
 
87
- addthis_bookmark_button(:publisher => "example", :secure => true)
86
+ addthis_bookmark_button(:options => "facebook, email, twitter, more")
88
87
 
89
- will output a button usable on https pages with the publisher variable set
90
- to "example" regardless of any configuration changes.
91
-
92
- The default value of the secure option is false, this default can be changed by
93
- setting
94
-
95
- Jaap3::Addthis::DEFAULT_OPTIONS[:secure] = true
88
+ For a list of available services see http://addthis.com/help/customize/services.
89
+ By modifying the defaults in the initializer you can avoid having to set your
90
+ preferred set of services every call.
96
91
 
97
- in your initializer.
92
+ Jaap3::Addthis::DEFAULT_OPTIONS[:options] = "facebook, email, twitter, more"
98
93
 
99
- = Custom images / text
94
+ === Custom images / text
100
95
 
101
96
  Using a different image or link text is achieved by using the methods with a
102
97
  block:
@@ -106,17 +101,17 @@ block:
106
101
  This will output a textlink that behaves exactly like a normal bookmark_button
107
102
  but withouth the image.
108
103
 
109
- You can offcourse use the block to call the Rails image_tag helper to embed your
104
+ You can also use the block to call the Rails image_tag helper to embed your
110
105
  own images.
111
106
 
112
- You can change the default image of each button by modifying the following
113
- constants in you initializer:
107
+ You can permanently change the default image of each button by modifying the
108
+ following constants in you initializer:
114
109
 
115
110
  Jaap3::Addthis::BOOKMARK_BUTTON_DEFAULTS[:button_html] = "Click here to AddThis"
116
111
  Jaap3::Addthis::FEED_BUTTON_DEFAULTS[:button_html] = "Click here to AddThis"
117
112
  Jaap3::Addthis::EMAIL_BUTTON_DEFAULTS[:button_html] = "Click here to AddThis"
118
113
 
119
- = Script options
114
+ === Script options
120
115
 
121
116
  Addthis.com allows for a few variables to be set to change the appearance of the
122
117
  widget. These can either be given during a method call:
@@ -138,7 +133,43 @@ Note that you cannot customize individual buttons on a page. Custom settings
138
133
  will be applied to all the buttons on a page. This is a limitation of the
139
134
  Addthis.com widget.
140
135
 
141
- = Contributing
136
+ Read more about these options on the the addthis.com website:
137
+ * http://addthis.com/help/customize/branding
138
+ * http://addthis.com/help/customize/menu-offset
139
+ * http://addthis.com/help/customize/hover-delay
140
+
141
+ === Secure pages
142
+
143
+ Calling one of the addthis_button methods with the secure option set to true
144
+ like this:
145
+
146
+ addthis_bookmark_button(:secure => true)
147
+
148
+ will output a button usable on https pages.
149
+
150
+ The default value of the secure option is false, this default can be changed by
151
+ setting
152
+
153
+ Jaap3::Addthis::DEFAULT_OPTIONS[:secure] = true
154
+
155
+ in your initializer.
156
+
157
+ === Custom language
158
+
159
+ The Addthis widget language will automatically choose a language based on
160
+ the user's browser settings. To override this behaviour you can set a custom
161
+ language.
162
+
163
+ addthis_bookmark_button(:language => "nl")
164
+
165
+ or
166
+
167
+ Jaap3::Addthis::DEFAULT_OPTIONS[:language] = "nl"
168
+
169
+ For a list of supported language codes see the bottom of
170
+ http://addthis.com/help/localization/translations
171
+
172
+ == Contributing
142
173
 
143
174
  If you would like to contribute to the Addthis plugin, just fork the code
144
175
  and send me a pull request after you are done fiddling.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :patch: 0
3
3
  :major: 0
4
- :minor: 5
4
+ :minor: 7
data/lib/addthis.rb CHANGED
@@ -1,31 +1,23 @@
1
1
  module Jaap3
2
2
  module Addthis
3
- CONFIG = {
4
- :publisher => ""
5
- }
3
+ CONFIG = {:publisher => ""}
6
4
  DEFAULT_OPTIONS = {
7
5
  :script_src => "http://s7.addthis.com/js/200/addthis_widget.js",
8
- :secure => false,
9
6
  :brand => nil, :header_color => nil, :header_background => nil,
10
- :offset_top => nil, :offset_left => nil, :hover_delay => nil
7
+ :offset_top => nil, :offset_left => nil, :hover_delay => nil,
8
+ :options => nil, :language => nil, :secure => false
11
9
  }
12
10
  BOOKMARK_BUTTON_DEFAULTS = {
13
11
  :title => "",
14
- :alt => "Bookmark and Share",
15
- :button_html => '<img src="http://s7.addthis.com/static/btn/lg-share-en.gif"
16
- width="125" height="16" border="0" alt="#{options[:alt]}" />'
12
+ :button_html => '<img src="http://s7.addthis.com/static/btn/lg-share-en.gif" width="125" height="16" border="0" alt="Bookmark and Share" />'
17
13
  }
18
14
  FEED_BUTTON_DEFAULTS = {
19
15
  :title => "Subscribe using any feed reader!",
20
- :alt => "Subscribe",
21
- :button_html => '<img src="http://s7.addthis.com/static/btn/lg-feed-en.gif"
22
- width="125" height="16" border="0" alt="#{options[:alt]}" />'
16
+ :button_html => '<img src="http://s7.addthis.com/static/btn/lg-feed-en.gif" width="125" height="16" border="0" alt="Subscribe" />'
23
17
  }
24
18
  EMAIL_BUTTON_DEFAULTS = {
25
19
  :title => "",
26
- :alt => "Email",
27
- :button_html => '<img src="http://s7.addthis.com/button1-email.gif"
28
- width="54" height="16" border="0" alt="#{options[:alt]}" />'
20
+ :button_html => '<img src="http://s7.addthis.com/button1-email.gif" width="54" height="16" border="0" alt="Email" />'
29
21
  }
30
22
 
31
23
  module Helper
@@ -33,56 +25,48 @@ module Jaap3
33
25
  url, options = extract_addthis_url_and_options(args)
34
26
  options[:button_html] = yield if block_given?
35
27
  options = BOOKMARK_BUTTON_DEFAULTS.merge(options)
36
- s = %Q{
37
- <a href="http://www.addthis.com/bookmark.php?v=20"
38
- onmouseover="return addthis_open(this, '', '#{url}', '#{options[:page_title]}')"
39
- title="#{options[:title]}" onmouseout="addthis_close()"
40
- onclick="return addthis_sendto()">}
28
+ s = %Q{<a href="http://www.addthis.com/bookmark.php?v=20" onmouseover="#{addthis_open("", url, options[:page_title])}" onmouseout="addthis_close()" onclick="return addthis_sendto()" title="#{options[:title]}">}
41
29
  addthis_tag(s, options)
42
30
  end
43
31
  alias addthis_share_button addthis_bookmark_button
44
32
 
45
- def addthis_feed_button(url, *args)
46
- options = FEED_BUTTON_DEFAULTS.merge(extract_addthis_options(args))
33
+ def addthis_email_button(*args)
34
+ url, options = extract_addthis_url_and_options(args)
47
35
  options[:button_html] = yield if block_given?
48
- s = %Q{
49
- <a href="http://www.addthis.com/feed.php?pub=#{options[:publisher]}&h1=#{url}&t1="
50
- onclick="return addthis_open(this, 'feed', '#{url}')"
51
- title="#{options[:title]}" target="_blank">}
36
+ options = EMAIL_BUTTON_DEFAULTS.merge(options)
37
+ s = %Q{<a href="http://www.addthis.com/bookmark.php" onclick="#{addthis_open("email", url, options[:page_title])}" title="#{options[:title]}">}
52
38
  addthis_tag(s, options)
53
39
  end
54
40
 
55
- def addthis_email_button(*args)
56
- url, options = extract_addthis_url_and_options(args)
41
+ def addthis_feed_button(url, *args)
42
+ options = FEED_BUTTON_DEFAULTS.merge(extract_addthis_options(args))
57
43
  options[:button_html] = yield if block_given?
58
- options = EMAIL_BUTTON_DEFAULTS.merge(options)
59
- s = %Q{
60
- <a href="http://www.addthis.com/bookmark.php"
61
- onclick="return addthis_open(this, 'email', '#{url}', '#{options[:page_title]}')"
62
- title="#{options[:title]}">}
44
+ s = %Q{<a href="http://www.addthis.com/feed.php?pub=#{options[:publisher]}&h1=#{url.gsub(/[^a-zA-Z0-9_\.\-]/n) {|c| sprintf('%%%02x', c[0]) }}&t1=" onclick="#{addthis_open("feed", url)}" title="#{options[:title]}" target="_blank">}
63
45
  addthis_tag(s, options)
64
46
  end
65
47
 
66
48
  protected
67
49
  def addthis_tag(str, options = {})
68
- s = [%Q{<!-- AddThis Button BEGIN -->}]
50
+ s = ["<!-- AddThis Button BEGIN -->"]
69
51
  s << addthis_custom_script(options)
70
- s << %Q{#{str}#{options[:button_html].gsub(/#\{options\[:alt\]\}/, options[:alt])}</a>}
71
- s << %Q{<script type="text/javascript" src="#{options[:script_src]}"></script>}
72
- s << %Q{<!-- AddThis Button END -->}
52
+ s << "#{str}#{options[:button_html]}</a><script type=\"text/javascript\" src=\"#{options[:script_src]}\"></script>"
53
+ s << "<!-- AddThis Button END -->"
73
54
  s = s * "\n"
74
55
  options[:secure] ? s.gsub(/http:\/\/s[57]\.addthis\.com/, "https://secure.addthis.com") : s
75
56
  end
76
57
 
77
58
  def addthis_custom_script(options = {})
78
- s = %Q{<script type="text/javascript">
79
- var addthis_pub = "#{options[:publisher]}";}
80
- [:brand, :header_color, :header_background, :offset_top, :offset_left, :hover_delay].each do |custom|
81
- s << %Q{var addthis_#{custom} = #{options[custom].is_a?(Integer) ? options[custom] : %Q("#{options[custom]}")};} unless options[custom].nil?
59
+ s = "<script type=\"text/javascript\">var addthis_pub = '#{options[:publisher]}';"
60
+ [:brand, :header_color, :header_background, :offset_top, :offset_left, :hover_delay, :options, :language].each do |custom|
61
+ s << "var addthis_#{custom} = #{options[custom].is_a?(Integer) ? options[custom] : "'#{options[custom]}'"};" unless options[custom].nil?
82
62
  end
83
63
  s << "</script>"
84
64
  end
85
65
 
66
+ def addthis_open(type, url, title = nil)
67
+ "addthis_open(this, '#{type}', '#{url}'#{ title.nil? ? "" : ", '#{title.gsub("'", "\\u0027").gsub("\"", "\\u0022")}'"})"
68
+ end
69
+
86
70
  def extract_addthis_url_and_options(args, options = {:page_title => "[TITLE]"})
87
71
  url = args[0].is_a?(String) ? args.shift : "[URL]"
88
72
  return url, options = extract_addthis_options(args, options)
@@ -91,8 +75,8 @@ module Jaap3
91
75
  def extract_addthis_options(args, options = {})
92
76
  page_title = args[0].is_a?(String) ? args.shift : options[:page_title]
93
77
  options = args[0].is_a?(Hash) ? args.shift : options
94
- options[:page_title] = page_title
95
78
  options.symbolize_keys! if options.respond_to?(:symbolize_keys!)
79
+ options[:page_title] = page_title
96
80
  options = CONFIG.merge(DEFAULT_OPTIONS).merge(options)
97
81
  return options
98
82
  end
data/test/addthis_test.rb CHANGED
@@ -18,7 +18,7 @@ class AddthisTest < Test::Unit::TestCase
18
18
  should "provide addthis_email_button" do
19
19
  assert respond_to?(:addthis_email_button)
20
20
  end
21
-
21
+
22
22
  [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
23
23
  context "the output of #{m}" do
24
24
  setup do
@@ -43,10 +43,19 @@ class AddthisTest < Test::Unit::TestCase
43
43
  end
44
44
  end
45
45
 
46
+ [:addthis_bookmark_button, :addthis_email_button].each do |m|
47
+ context "the output of #{m} with url and title set" do
48
+ setup { @output = method(m).call("http://example.com", "It's an \"example\"") }
49
+
50
+ should "include title" do
51
+ assert_match "'It\\u0027s an \\u0022example\\u0022'", @output
52
+ end
53
+ end
54
+ end
55
+
46
56
  context "a bookmark/share button" do
47
57
  setup { @output = addthis_bookmark_button }
48
58
 
49
- should_set_alt_to Jaap3::Addthis::BOOKMARK_BUTTON_DEFAULTS[:alt]
50
59
  should_set_title_to Jaap3::Addthis::BOOKMARK_BUTTON_DEFAULTS[:title]
51
60
  should_set_href_to "http://www.addthis.com/bookmark.php?v=20"
52
61
 
@@ -62,9 +71,8 @@ class AddthisTest < Test::Unit::TestCase
62
71
  context "a feed button" do
63
72
  setup { @output = addthis_feed_button("http://example.com") }
64
73
 
65
- should_set_alt_to Jaap3::Addthis::FEED_BUTTON_DEFAULTS[:alt]
66
74
  should_set_title_to Jaap3::Addthis::FEED_BUTTON_DEFAULTS[:title]
67
- should_set_href_to "http://www.addthis.com/feed.php?pub=&h1=http://example.com&t1="
75
+ should_set_href_to "http://www.addthis.com/feed.php?pub=&h1=http%3a%2f%2fexample.com&t1="
68
76
 
69
77
  should "set url to example.com" do
70
78
  assert_match "'http://example.com')", @output
@@ -74,7 +82,6 @@ class AddthisTest < Test::Unit::TestCase
74
82
  context "an email button" do
75
83
  setup { @output = addthis_email_button }
76
84
 
77
- should_set_alt_to Jaap3::Addthis::EMAIL_BUTTON_DEFAULTS[:alt]
78
85
  should_set_title_to Jaap3::Addthis::EMAIL_BUTTON_DEFAULTS[:title]
79
86
  should_set_href_to "http://www.addthis.com/bookmark.php"
80
87
  end
@@ -93,7 +100,7 @@ class AddthisTest < Test::Unit::TestCase
93
100
  context "a feed button" do
94
101
  setup { @output = addthis_feed_button("http://example.com") }
95
102
 
96
- should_set_href_to "http://www.addthis.com/feed.php?pub=test_publisher&h1=http://example.com&t1="
103
+ should_set_href_to "http://www.addthis.com/feed.php?pub=test_publisher&h1=http%3a%2f%2fexample.com&t1="
97
104
  end
98
105
 
99
106
  context "in turn overwritten by options hash" do
@@ -108,7 +115,7 @@ class AddthisTest < Test::Unit::TestCase
108
115
  context "a feed button" do
109
116
  setup { @output = addthis_feed_button("http://example.com", :publisher => "another_publisher") }
110
117
 
111
- should_set_href_to "http://www.addthis.com/feed.php?pub=another_publisher&h1=http://example.com&t1="
118
+ should_set_href_to "http://www.addthis.com/feed.php?pub=another_publisher&h1=http%3a%2f%2fexample.com&t1="
112
119
  end
113
120
  end
114
121
  end
@@ -135,13 +142,12 @@ class AddthisTest < Test::Unit::TestCase
135
142
  end
136
143
  end
137
144
 
138
- context "when overwriting alt and title" do
145
+ context "when overwriting title" do
139
146
  [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
140
147
  context "the output of #{m}" do
141
- setup { @output = method(m).call("http://example.com", :alt => "Example", :title => "Example title") }
148
+ setup { @output = method(m).call("http://example.com", :title => "Example title") }
142
149
 
143
150
  should_set_title_to "Example title"
144
- should_set_alt_to "Example"
145
151
  end
146
152
  end
147
153
  end
@@ -212,9 +218,7 @@ class AddthisTest < Test::Unit::TestCase
212
218
  }
213
219
  [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
214
220
  context "the output of #{m}" do
215
- setup do
216
- @output = method(m).call("http://example.com", options)
217
- end
221
+ setup { @output = method(m).call("http://example.com", options) }
218
222
 
219
223
  options.each_pair do |attribute, value|
220
224
  should_customize attribute, value
@@ -228,7 +232,7 @@ class AddthisTest < Test::Unit::TestCase
228
232
  options.each_pair do |attribute, value|
229
233
  Jaap3::Addthis::DEFAULT_OPTIONS[attribute] = value
230
234
  end
231
- @output = method(m).call("http://example.com", options)
235
+ @output = method(m).call("http://example.com")
232
236
  end
233
237
 
234
238
  options.each_pair do |attribute, value|
@@ -238,4 +242,27 @@ class AddthisTest < Test::Unit::TestCase
238
242
  end
239
243
  end
240
244
 
245
+ context "with a custom list of services" do
246
+ context "the output of addthis_bookmark_button" do
247
+ setup { @output = addthis_bookmark_button(:options => "facebook, email, twitter, more") }
248
+
249
+ should_customize :options, "facebook, email, twitter, more"
250
+ end
251
+
252
+ context "by changing the defaults the output of addthis_bookmark_button" do
253
+ setup do
254
+ Jaap3::Addthis::DEFAULT_OPTIONS[:options] = "facebook, email, twitter, more"
255
+ @output = addthis_bookmark_button
256
+ end
257
+
258
+ should_customize :options, "facebook, email, twitter, more"
259
+ end
260
+ end
261
+
262
+ context "with a custom language set" do
263
+ setup { @output = addthis_bookmark_button(:language => "nl") }
264
+
265
+ should_customize :language, "nl"
266
+ end
267
+
241
268
  end
data/test/test_helper.rb CHANGED
@@ -29,13 +29,13 @@ class Test::Unit::TestCase
29
29
 
30
30
  def should_not_customize(attribute)
31
31
  should "not set addthis_#{attribute}" do
32
- assert_no_match(/var addthis_#{attribute} = "[\"]+";/, @output)
32
+ assert_no_match(/var addthis_#{attribute} = '[^']+';/, @output)
33
33
  end
34
34
  end
35
35
 
36
36
  def should_customize(attribute, value)
37
37
  should "set addthis_#{attribute} to '#{value}" do
38
- assert_match(/var addthis_#{attribute} = ["]?#{value}["]?;/, @output)
38
+ assert_match(/var addthis_#{attribute} = [']?#{value}[']?;/, @output)
39
39
  end
40
40
  end
41
41
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jaap3-addthis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaap Roes