Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Version 0.0.6 - url shortening (via bit.ly) introduced.

  • Loading branch information...
commit dc110cc2dfbb75de915c5cd013a98445c111f1e1 1 parent 2345466
5v3n authored
View
5 README.md
@@ -112,6 +112,11 @@ Now, you did most likely implement a tag listing on your toto blog. Congrats!
BTW: part of my to dos is encapsulating the tag parsing process so you won't have to fiddle around with too much ruby code here...
+### shor url (via bit.ly)
+
+ short_url = BlogHelper::short_url_bitly(<url>, <bit.ly login nam>, <bit.ly api key>)
+
+
### disqus comment counter
TBD... I have to refer to the source & ri for now.
View
2  Rakefile
@@ -12,7 +12,7 @@ require 'rake/testtask'
spec = Gem::Specification.new do |s|
s.name = 'blog_helper'
- s.version = '0.0.5'
+ s.version = '0.0.6'
s.has_rdoc = true
s.extra_rdoc_files = ['README.md', 'LICENSE']
s.summary = 'Some handy helpers for serious, toto and the likes...'
View
26 lib/blog_helper.rb
@@ -1,3 +1,5 @@
+require 'cgi'
+
# Some useful feature for serious, toto and the likes. Basically, any ruby based blog or site.
module BlogHelper
# create a list of links to tagged articles, default link_format: <code>%&amp;&lt;a href=&quot;/tagged?tag=#{tag}&quot; alt=&quot;articles concerning #{tag}&quot; &gt;#{tag}&lt;/a&gt; &amp;</code>
@@ -29,14 +31,13 @@ def BlogHelper.seo_friendly_title(path, title, seo_ending)
end
page_title
end
-
-#Generates javascript to include to the bottom of your index page.
-#Appending '#disqus_thread' to the end of permalinks will replace the text of these links with the comment count.
-#
-#For example, you may have a link with this HTML: <code>&lt;a href=&quot;http://example.com/my_article.html#disqus_thread&quot;&gt;Comments&lt;/a&gt; </code> The comment count code will replace the text "Comments" with the number of comments on the page
-#
-#(see http://disqus.com/comments/universal/ for details)
-#
+ #Generates javascript to include to the bottom of your index page.
+ #Appending '#disqus_thread' to the end of permalinks will replace the text of these links with the comment count.
+ #
+ #For example, you may have a link with this HTML: <code>&lt;a href=&quot;http://example.com/my_article.html#disqus_thread&quot;&gt;Comments&lt;/a&gt; </code> The comment count code will replace the text "Comments" with the number of comments on the page
+ #
+ #(see http://disqus.com/comments/universal/ for details)
+ #
def BlogHelper.disqus_comment_count_js(disqus_shortname)
%&
<script type="text/javascript">
@@ -50,4 +51,13 @@ def BlogHelper.disqus_comment_count_js(disqus_shortname)
& if disqus_shortname
end
+ # Retrieve bit.ly shortened url
+ def BlogHelper.short_url_bitly(url, login, api_key)
+ if api_key != "" && login != ""
+ rest_call=%{http://api.bit.ly/v3/shorten?login=#{login}&apikey=#{api_key}&longUrl=#{url}&format=txt}
+ Net::HTTP::get(URI.parse(rest_call))
+ else
+ url #fallback: return url to shorten - or nil if it isn't set
+ end
+ end
end
View
31 test/blog_helper_test.rb
@@ -14,44 +14,53 @@ class BlogHelperTest < Test::Unit::TestCase
STRING_03 = "tag_1, tag_2, tag_3"
STRING_04 = "hacks, love, rock 'n' roll"
+ EXPECTED_SHORT_URL = "http://bit.ly/c5BSQl\n"
+ TEST_URL = 'http://5v3n.com'
+ BITLY_LOGIN = 'bitlyapidemo'
+ BITLY_API_KEY = 'R_0da49e0a9118ff35f52f629d2d71bf07'
+
def test_process_to_array
result_nil = BlogHelper::csv_to_array(nil)
assert("nil should return nil!", result_nil == nil)
result_00 = BlogHelper::csv_to_array(STRING_00)
- p result_00
assert("Unexpected result for #{STRING_00}!", result_00 == [])
result_01 = BlogHelper::csv_to_array(STRING_01)
assert("Unexpected result for #{STRING_01}!", result_01 == ["tag_1"])
- p result_01
result_02 = BlogHelper::csv_to_array(STRING_02)
- p result_02
assert("Unexpected result for #{STRING_02}!", result_02 == Array["tag_1", "tag_2"])
result_03 = BlogHelper::csv_to_array(STRING_03)
assert("Unexpected result for #{STRING_03}!", result_03 == Array["tag_1", "tag_2", "tag_3"])
- p result_03
result_04 = BlogHelper::csv_to_array(STRING_04)
assert("Unexpected result for #{STRING_04}!", result_04 == Array["hacks", "love", "rock 'n' roll"])
- p result_04
end
def test_tag_link_list
result_nil = BlogHelper::tag_link_list(nil)
- p result_nil
assert("Unexpected mapping for nil!", result_nil == nil)
result_00 = BlogHelper::tag_link_list(STRING_00)
- p result_00
assert("Unexpected mapping for #{STRING_00}!", result_00 == "")
result_01 = BlogHelper::tag_link_list(STRING_01)
- p result_01
assert("Unexpected mapping for #{STRING_01}!", result_01 == "<a href=/tagged?tag=tag_1 >tag_1</a> ")
result_02 = BlogHelper::tag_link_list(STRING_02)
- p result_02
assert("Unexpected mapping for #{STRING_02}!", result_02 == "<a href=/tagged?tag=tag_1 >tag_1</a> <a href=/tagged?tag=tag_2 >tag_2</a> ")
result_03 = BlogHelper::tag_link_list(STRING_03)
- p result_03
assert("Unexpected mapping for #{STRING_03}!", result_03 == "<a href=/tagged?tag=tag_1 >tag_1</a> <a href=/tagged?tag=tag_2 >tag_2</a> <a href=/tagged?tag=tag_3 >tag_3</a> ")
result_04 = BlogHelper::tag_link_list(STRING_04)
- p result_04
assert("Unexpected mapping for #{STRING_04}!", result_04 == "<a href=/tagged?tag=hacks >hacks</a> <a href=/tagged?tag=love >love</a> <a href=/tagged?tag=rock 'n' roll >rock 'n' roll</a> ")
end
+ def test_short_url_bitly
+ short_url = BlogHelper::short_url_bitly(TEST_URL, "", "")
+ assert(short_url == TEST_URL, "generated short url: '#{short_url}', but should be '#{TEST_URL}' since no login and/or api key are given.")
+ aok = false
+ begin
+ short_url = BlogHelper::short_url_bitly("invalid url", BITLY_LOGIN, BITLY_API_KEY)
+ rescue URI::InvalidURIError
+ aok = true
+ ensure # make sure the result is checked & the remaining tests are executed
+ assert(aok, "Invalid url should have resulted in an InvalidURIError!")
+ short_url = BlogHelper::short_url_bitly(TEST_URL, BITLY_LOGIN, BITLY_API_KEY)
+ assert(short_url != 'RATE_LIMIT_EXCEEDED', "Use your own login & api key if the demo account's rate limit is reached...")
+ assert(short_url == EXPECTED_SHORT_URL, "generated short url: '#{short_url}', but should be '#{EXPECTED_SHORT_URL}'")
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.