jaap3-addthis 0.2.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -5,16 +5,21 @@ 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
7
  This plugin makes it easy to add plain or customized Addthis.com widgets to
8
- your Rails application.
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.
9
10
 
10
11
  = Installation
11
12
 
12
- As a Gem in environment.rb:
13
+ If you want to use the Rails 2.1 dependency manager add this environment.rb:
13
14
 
14
15
  config.gem 'jaap3-addthis', :lib => 'addthis',
15
16
  :source => 'http://gems.github.com'
16
17
 
17
- As a plugin:
18
+ Then run the following command in your Rails application root:
19
+
20
+ rake gems:install
21
+
22
+ Or use it as a plain plugin:
18
23
 
19
24
  script/plugin install git://github.com/jaap3/addthis.git
20
25
 
@@ -28,29 +33,29 @@ The most basic use is simply providing no arguments:
28
33
  The code generated by Addthis will then try to figure out the url and title by
29
34
  itself. This is not possible for addthis_feed_button.
30
35
 
31
- You can set a custom url, this is required for addthis_feed_button:
36
+ You can set a custom url, a required argument addthis_feed_button:
32
37
 
33
38
  addthis_bookmark_button("http://www.example.com/")
34
39
  addthis_email_button("http://www.example.com/")
35
40
  addthis_feed_button("http://www.example.com/")
36
41
 
37
- Setting a title is possible when adding a bookmark or email button:
42
+ Setting a title is also possible when adding a bookmark or email button:
38
43
 
39
44
  addthis_bookmark_button("http://www.example.com/", "Example website")
40
45
  addthis_email_button("http://www.example.com/", "Example website")
41
46
 
42
47
  To only set a custom title you can use to options hash:
43
48
 
44
- addthis_bookmark_button(:title => "Example title")
45
- addthis_email_button(:title => "Example title")
49
+ addthis_bookmark_button(:page_title => "Example title")
50
+ addthis_email_button(:page_title => "Example title")
46
51
 
47
- The options hash can be used to customize the widget to a great extend. Each of
52
+ The options hash can be used to customize the widget to a great extent. Each of
48
53
  the shown examples can take the options hash as an optional extra argument.
49
54
 
50
55
  = Usage
51
56
 
52
57
  If you wish to track analytics for your button, you must create an account at
53
- http://addthis.com/. Joining is free!
58
+ http://Addthis.com. Joining is free!
54
59
 
55
60
  After signing up create a new ruby file in config/initializers called
56
61
  addthis.rb. You can use this file to configure your publisher name (your
@@ -74,13 +79,65 @@ Besides overriding the default settings using the initializer, each helper
74
79
  method takes an options hash that can be used to override any setting for
75
80
  a specific button.
76
81
 
77
- So calling
82
+ = Secure pages
83
+
84
+ Calling one of the addthis_button methods with the secure option set to true
85
+ like this:
78
86
 
79
87
  addthis_bookmark_button(:publisher => "example", :secure => true)
80
88
 
81
- Will output a button usable on https pages with the publisher variable set
89
+ will output a button usable on https pages with the publisher variable set
82
90
  to "example" regardless of any configuration changes.
83
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
96
+
97
+ in your initializer.
98
+
99
+ = Custom images / text
100
+
101
+ Using a different image or link text is achieved by using the methods with a
102
+ block:
103
+
104
+ addthis_bookmark_button { "Click here to AddThis!" }
105
+
106
+ This will output a textlink that behaves exactly like a normal bookmark_button
107
+ but withouth the image.
108
+
109
+ You can offcourse use the block to call the Rails image_tag helper to embed your
110
+ own images.
111
+
112
+ You can change the default image of each button by modifying the following
113
+ constants in you initializer:
114
+
115
+ Jaap3::Addthis::BOOKMARK_BUTTON_DEFAULTS[:button_html] = "Click here to AddThis"
116
+ Jaap3::Addthis::FEED_BUTTON_DEFAULTS[:button_html] = "Click here to AddThis"
117
+ Jaap3::Addthis::EMAIL_BUTTON_DEFAULTS[:button_html] = "Click here to AddThis"
118
+
119
+ = Script options
120
+
121
+ Addthis.com allows for a few variables to be set to change the appearance of the
122
+ widget. These can either be given during a method call:
123
+
124
+ addthis_bookmark_button({
125
+ :brand => "Example brand",
126
+ :header_color => "white",
127
+ :header_background => "black",
128
+ :offset_top => -100,
129
+ :offset_left => 125,
130
+ :hover_delay => 200
131
+ })
132
+
133
+ Or by having your initializer change the defaults stored in:
134
+
135
+ Jaap3::Addthis::DEFAULT_OPTIONS
136
+
137
+ Note that you cannot customize individual buttons on a page. Custom settings
138
+ will be applied to all the buttons on a page. This is a limitation of the
139
+ Addthis.com widget.
140
+
84
141
  = Contributing
85
142
 
86
143
  If you would like to contribute to the Addthis plugin, just fork the code
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
+ :patch: 0
2
3
  :major: 0
3
- :minor: 2
4
- :patch: 1
4
+ :minor: 5
data/lib/addthis.rb CHANGED
@@ -5,66 +5,84 @@ module Jaap3
5
5
  }
6
6
  DEFAULT_OPTIONS = {
7
7
  :script_src => "http://s7.addthis.com/js/200/addthis_widget.js",
8
- :secure => false
8
+ :secure => false,
9
+ :brand => nil, :header_color => nil, :header_background => nil,
10
+ :offset_top => nil, :offset_left => nil, :hover_delay => nil
9
11
  }
10
12
  BOOKMARK_BUTTON_DEFAULTS = {
11
13
  :title => "",
12
- :alt => "Bookmark and Share"
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]}" />'
13
17
  }
14
18
  FEED_BUTTON_DEFAULTS = {
15
19
  :title => "Subscribe using any feed reader!",
16
- :alt => "Subscribe"
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]}" />'
17
23
  }
18
24
  EMAIL_BUTTON_DEFAULTS = {
19
25
  :title => "",
20
- :alt => "Email"
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]}" />'
21
29
  }
22
30
 
23
31
  module Helper
24
32
  def addthis_bookmark_button(*args)
25
33
  url, options = extract_addthis_url_and_options(args)
34
+ options[:button_html] = yield if block_given?
26
35
  options = BOOKMARK_BUTTON_DEFAULTS.merge(options)
27
- s = %Q{<a href="http://www.addthis.com/bookmark.php?v=20" onmouseover="}
28
- s << %Q{return addthis_open(this, '', '#{url}', '#{options[:page_title]}')"}
29
- s << %Q{ title="#{options[:title]}"}
30
- s << %Q{ onmouseout="addthis_close()" onclick="return addthis_sendto()">}
31
- s << %Q{<img src="http://s7.addthis.com/static/btn/lg-share-en.gif"}
32
- s << %Q{ width="125" height="16" alt="#{options[:alt]}" style="border:0"/></a>}
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()">}
33
41
  addthis_tag(s, options)
34
42
  end
35
43
  alias addthis_share_button addthis_bookmark_button
36
44
 
37
45
  def addthis_feed_button(url, *args)
38
46
  options = FEED_BUTTON_DEFAULTS.merge(extract_addthis_options(args))
39
- s = %Q{<a href="http://www.addthis.com/feed.php?pub=#{options[:publisher]}&h1=#{url}&t1="}
40
- s << %Q{ onclick="return addthis_open(this, 'feed', '#{url}')"}
41
- s << %Q{ title="#{options[:title]}" target="_blank">}
42
- s << %Q{<img src="http://s7.addthis.com/static/btn/lg-feed-en.gif"}
43
- s << %Q{width="125" height="16" alt="#{options[:alt]}" style="border:0"/></a>}
47
+ 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">}
44
52
  addthis_tag(s, options)
45
53
  end
46
54
 
47
55
  def addthis_email_button(*args)
48
56
  url, options = extract_addthis_url_and_options(args)
57
+ options[:button_html] = yield if block_given?
49
58
  options = EMAIL_BUTTON_DEFAULTS.merge(options)
50
- s = %Q{<a href="http://www.addthis.com/bookmark.php"}
51
- s << %Q{ style="text-decoration:none;" title="#{options[:title]}"}
52
- s << %Q{ onclick="return addthis_open(this, 'email', '#{url}', '#{options[:page_title]}');">}
53
- s << %Q{<img src="http://s7.addthis.com/button1-email.gif" width="54" height="16" border="0" alt="#{options[:alt]}" /></a>}
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]}">}
54
63
  addthis_tag(s, options)
55
64
  end
56
65
 
57
66
  protected
58
67
  def addthis_tag(str, options = {})
59
68
  s = [%Q{<!-- AddThis Button BEGIN -->}]
60
- s << %Q{<script type="text/javascript">var addthis_pub="#{options[:publisher]}";</script>}
61
- s << str
69
+ s << addthis_custom_script(options)
70
+ s << %Q{#{str}#{options[:button_html].gsub(/#\{options\[:alt\]\}/, options[:alt])}</a>}
62
71
  s << %Q{<script type="text/javascript" src="#{options[:script_src]}"></script>}
63
72
  s << %Q{<!-- AddThis Button END -->}
64
73
  s = s * "\n"
65
74
  options[:secure] ? s.gsub(/http:\/\/s[57]\.addthis\.com/, "https://secure.addthis.com") : s
66
75
  end
67
76
 
77
+ 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?
82
+ end
83
+ s << "</script>"
84
+ end
85
+
68
86
  def extract_addthis_url_and_options(args, options = {:page_title => "[TITLE]"})
69
87
  url = args[0].is_a?(String) ? args.shift : "[URL]"
70
88
  return url, options = extract_addthis_options(args, options)
@@ -74,8 +92,8 @@ module Jaap3
74
92
  page_title = args[0].is_a?(String) ? args.shift : options[:page_title]
75
93
  options = args[0].is_a?(Hash) ? args.shift : options
76
94
  options[:page_title] = page_title
77
- options = CONFIG.merge(DEFAULT_OPTIONS).merge(options)
78
95
  options.symbolize_keys! if options.respond_to?(:symbolize_keys!)
96
+ options = CONFIG.merge(DEFAULT_OPTIONS).merge(options)
79
97
  return options
80
98
  end
81
99
  end
data/test/addthis_test.rb CHANGED
@@ -27,6 +27,11 @@ class AddthisTest < Test::Unit::TestCase
27
27
  end
28
28
 
29
29
  should_set_script_src_to Jaap3::Addthis::DEFAULT_OPTIONS[:script_src]
30
+
31
+ [:brand, :header_color, :header_background,
32
+ :offset_top, :offset_left, :hover_delay].each do |attribute|
33
+ should_not_customize attribute
34
+ end
30
35
 
31
36
  should "start with an html comment" do
32
37
  assert_equal "<!-- AddThis Button BEGIN -->", @output_lines.first
@@ -81,9 +86,7 @@ class AddthisTest < Test::Unit::TestCase
81
86
  context "the output of #{m}" do
82
87
  setup { @output = method(m).call("http://example.com") }
83
88
 
84
- should "set addthis_pub to test_publisher" do
85
- assert_match 'var addthis_pub="test_publisher";', @output
86
- end
89
+ should_customize :pub, "test_publisher"
87
90
  end
88
91
  end
89
92
 
@@ -92,6 +95,22 @@ class AddthisTest < Test::Unit::TestCase
92
95
 
93
96
  should_set_href_to "http://www.addthis.com/feed.php?pub=test_publisher&h1=http://example.com&t1="
94
97
  end
98
+
99
+ context "in turn overwritten by options hash" do
100
+ [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
101
+ context "the output of #{m}" do
102
+ setup { @output = method(m).call("http://example.com", :publisher => "another_publisher") }
103
+
104
+ should_customize :pub, "another_publisher"
105
+ end
106
+ end
107
+
108
+ context "a feed button" do
109
+ setup { @output = addthis_feed_button("http://example.com", :publisher => "another_publisher") }
110
+
111
+ should_set_href_to "http://www.addthis.com/feed.php?pub=another_publisher&h1=http://example.com&t1="
112
+ end
113
+ end
95
114
  end
96
115
 
97
116
  context "with altered script_src" do
@@ -99,10 +118,7 @@ class AddthisTest < Test::Unit::TestCase
99
118
 
100
119
  [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
101
120
  context "the output of #{m}" do
102
- setup do
103
- @output = method(m).call("http://example.com")
104
- @output_lines = @output.split("\n")
105
- end
121
+ setup { @output = method(m).call("http://example.com") }
106
122
 
107
123
  should_set_script_src_to "http://example.com/example.js"
108
124
  end
@@ -111,10 +127,7 @@ class AddthisTest < Test::Unit::TestCase
111
127
  context "in turn overwritten by options hash" do
112
128
  [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
113
129
  context "the output of #{m}" do
114
- setup do
115
- @output = method(m).call("http://example.com", :script_src => "http://www.example.com/example.js")
116
- @output_lines = @output.split("\n")
117
- end
130
+ setup { @output = method(m).call("http://example.com", :script_src => "http://www.example.com/example.js") }
118
131
 
119
132
  should_set_script_src_to "http://www.example.com/example.js"
120
133
  end
@@ -122,13 +135,21 @@ class AddthisTest < Test::Unit::TestCase
122
135
  end
123
136
  end
124
137
 
138
+ context "when overwriting alt and title" do
139
+ [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
140
+ context "the output of #{m}" do
141
+ setup { @output = method(m).call("http://example.com", :alt => "Example", :title => "Example title") }
142
+
143
+ should_set_title_to "Example title"
144
+ should_set_alt_to "Example"
145
+ end
146
+ end
147
+ end
148
+
125
149
  context "when setting secure to true" do
126
150
  [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
127
151
  context "by using the options hash the output of #{m}" do
128
- setup do
129
- @output = method(m).call("http://example.com", :secure => true)
130
- @output_lines = @output.split("\n")
131
- end
152
+ setup { @output = method(m).call("http://example.com", :secure => true) }
132
153
 
133
154
  should_set_script_src_to "https://secure.addthis.com/js/200/addthis_widget.js"
134
155
  end
@@ -139,7 +160,6 @@ class AddthisTest < Test::Unit::TestCase
139
160
  setup do
140
161
  Jaap3::Addthis::DEFAULT_OPTIONS[:secure] = true
141
162
  @output = method(m).call("http://example.com")
142
- @output_lines = @output.split("\n")
143
163
  end
144
164
 
145
165
  should_set_script_src_to "https://secure.addthis.com/js/200/addthis_widget.js"
@@ -147,4 +167,75 @@ class AddthisTest < Test::Unit::TestCase
147
167
  end
148
168
  end
149
169
 
170
+ context "with a block" do
171
+ [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
172
+ context "the output of #{m}" do
173
+ setup do
174
+ @output = method(m).call("http://example.com") do
175
+ "Click here to AddThis"
176
+ end
177
+ end
178
+
179
+ should "contain the custom text" do
180
+ assert_match(/<a[^>]+>Click here to AddThis<\/a>/, @output)
181
+ end
182
+ end
183
+ end
184
+ end
185
+
186
+ context "with changed default html" do
187
+ setup do
188
+ Jaap3::Addthis::BOOKMARK_BUTTON_DEFAULTS[:button_html] = "Click here to AddThis"
189
+ Jaap3::Addthis::FEED_BUTTON_DEFAULTS[:button_html] = "Click here to AddThis"
190
+ Jaap3::Addthis::EMAIL_BUTTON_DEFAULTS[:button_html] = "Click here to AddThis"
191
+ end
192
+
193
+ [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
194
+ context "the output of #{m}" do
195
+ setup { @output = method(m).call("http://example.com") }
196
+
197
+ should "contain the custom text" do
198
+ assert_match(/<a[^>]+>Click here to AddThis<\/a>/, @output)
199
+ end
200
+ end
201
+ end
202
+ end
203
+
204
+ context "tricked out with options" do
205
+ options = {
206
+ :brand => "Example brand",
207
+ :header_color => "white",
208
+ :header_background => "black",
209
+ :offset_top => 40,
210
+ :offset_left => 60,
211
+ :hover_delay => 200
212
+ }
213
+ [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
214
+ context "the output of #{m}" do
215
+ setup do
216
+ @output = method(m).call("http://example.com", options)
217
+ end
218
+
219
+ options.each_pair do |attribute, value|
220
+ should_customize attribute, value
221
+ end
222
+ end
223
+ end
224
+
225
+ [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
226
+ context "by changing the defaults the output of #{m}" do
227
+ setup do
228
+ options.each_pair do |attribute, value|
229
+ Jaap3::Addthis::DEFAULT_OPTIONS[attribute] = value
230
+ end
231
+ @output = method(m).call("http://example.com", options)
232
+ end
233
+
234
+ options.each_pair do |attribute, value|
235
+ should_customize attribute, value
236
+ end
237
+ end
238
+ end
239
+ end
240
+
150
241
  end
data/test/test_helper.rb CHANGED
@@ -26,6 +26,18 @@ class Test::Unit::TestCase
26
26
  should_set_src_to expected
27
27
  end
28
28
  end
29
+
30
+ def should_not_customize(attribute)
31
+ should "not set addthis_#{attribute}" do
32
+ assert_no_match(/var addthis_#{attribute} = "[\"]+";/, @output)
33
+ end
34
+ end
35
+
36
+ def should_customize(attribute, value)
37
+ should "set addthis_#{attribute} to '#{value}" do
38
+ assert_match(/var addthis_#{attribute} = ["]?#{value}["]?;/, @output)
39
+ end
40
+ end
29
41
  end
30
42
 
31
43
  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.2.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaap Roes
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-23 00:00:00 -07:00
12
+ date: 2009-04-24 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -55,7 +55,7 @@ requirements: []
55
55
  rubyforge_project:
56
56
  rubygems_version: 1.2.0
57
57
  signing_key:
58
- specification_version: 2
58
+ specification_version: 3
59
59
  summary: TODO
60
60
  test_files:
61
61
  - test/test_helper.rb