Module: Karakuri
- Defined in:
- lib/karakuri.rb
Overview
Some useful feature for toto and the likes. Basically, any ruby based blog or site.
Class Method Summary collapse
-
.csv_to_array(csv_string) ⇒ Object
processes a csv-string into an array.
-
.desired_articles(articles, tag) ⇒ Object
desired articles matching a corresponding tag.
-
.desired_tag(query_string) ⇒ Object
extract desired tag from
env["QUERY_STRING"]
or an equally formed expression, e.g. -
.disqus_comment_count_js(disqus_shortname) ⇒ Object
Generates javascript to include to the bottom of your index page.
-
.seo_friendly_title(path, title, seo_ending) ⇒ Object
pass the path (@path for a toto blog) & the desired SEO ending, e.g.
-
.short_url_bitly(url, login, api_key) ⇒ Object
Retrieve bit.ly shortened url.
-
.tag_link_list(csv_string) ⇒ Object
create a list of links to tagged articles, default link_format:
%&<a href="/tagged?tag=#{tag}" alt="articles concerning #{tag}" >#{tag}</a> &
.
Class Method Details
.csv_to_array(csv_string) ⇒ Object
processes a csv-string into an array
25 26 27 28 |
# File 'lib/karakuri.rb', line 25 def Karakuri.csv_to_array(csv_string) #split & handle forgotten spaces after the separator. then flatten the multidemnsional array: csv_string.split(', ').map{ |e| e.split(',')}.flatten if csv_string end |
.desired_articles(articles, tag) ⇒ Object
desired articles matching a corresponding tag
83 84 85 86 87 88 89 90 |
# File 'lib/karakuri.rb', line 83 def Karakuri.desired_articles(articles, tag) if(articles && tag) articles.select do |a| = Karakuri::csv_to_array(a[:tags]) .include?(tag) if end end end |
.desired_tag(query_string) ⇒ Object
extract desired tag from env["QUERY_STRING"]
or an equally formed expression, e.g. tag=desired_tag
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/karakuri.rb', line 92 def Karakuri.desired_tag(query_string) if query_string start = query_string.index("tag=") if start start = start + 3 stop = query_string.index("&") stop = 0 unless stop desired_tag = query_string[start+1..stop-1] desired_tag = CGI::unescape(desired_tag) else '' #fallback: return empty string to prevent nil errors end end end |
.disqus_comment_count_js(disqus_shortname) ⇒ Object
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: <a href="http://example.com/my_article.html#disqus_thread">Comments</a>
The comment count code will replace the text “Comments” with the number of comments on the page
(see disqus.com/comments/universal/ for details)
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/karakuri.rb', line 52 def Karakuri.disqus_comment_count_js(disqus_shortname) %& <script type="text/javascript"> var disqus_shortname = '#{disqus_shortname}'; (function () { var s = document.createElement('script'); s.async = true; s.src = 'http://disqus.com/forums/#{disqus_shortname}/count.js'; (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s); }()); </script> & if disqus_shortname end |
.seo_friendly_title(path, title, seo_ending) ⇒ Object
pass the path (@path for a toto blog) & the desired SEO ending, e.g. the name of your blog. example for toto: seo_friendly_title(@path, title, "mysite.com") will produce 'subpage | mysite.com' as seo friendly page title.
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/karakuri.rb', line 33 def Karakuri.seo_friendly_title(path, title, seo_ending) #TODO use custom title separator... if path == 'index' page_title = seo_ending elsif path.split('/').compact.length == 4 page_title = title << " | #{seo_ending}" else page_title = path.capitalize.gsub(/[-]/, ' ') << " | #{seo_ending}" end page_title end |
.short_url_bitly(url, login, api_key) ⇒ Object
Retrieve bit.ly shortened url
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/karakuri.rb', line 68 def Karakuri.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} begin Net::HTTP::get(URI.parse(rest_call)) # handle http errors (esp. timeouts!) rescue URI::InvalidURIError raise URI::InvalidURIError rescue url#in the case of a web service or HTTP error, we'll just ignore it & return the long url end else url #fallback: return long url if no proper login has been provided. TODO: check if a call w/o login is possible end end |
.tag_link_list(csv_string) ⇒ Object
create a list of links to tagged articles, default link_format: %&<a href="/tagged?tag=#{tag}" alt="articles concerning #{tag}" >#{tag}</a> &
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/karakuri.rb', line 12 def Karakuri.tag_link_list(csv_string) # read csv-string into array tag_list = csv_to_array(csv_string) if tag_list tag_string = "" #TODO pass a format via parameter tag_list.each { |tag| tag_string << %&<a href="/tagged?tag=#{tag}" alt="articles concerning #{tag}" >#{tag}</a> & } end tag_string end |