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

Class Method Details

.csv_to_array(csv_string) ⇒ Object

processes a csv-string into an array



34
35
36
37
# File 'lib/karakuri.rb', line 34

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



91
92
93
94
95
96
97
98
# File 'lib/karakuri.rb', line 91

def Karakuri.desired_articles(articles, tag)
  if(articles && tag)
    articles.select do |a|
      tags = Karakuri::csv_to_array(a[:tags])
      tags.include?(tag) if tags
    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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/karakuri.rb', line 100

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)



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/karakuri.rb', line 60

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


12
13
14
# File 'lib/karakuri.rb', line 12

def self.link_format(format)
  @link_format = format
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.



42
43
44
45
46
47
48
49
50
51
# File 'lib/karakuri.rb', line 42

def Karakuri.seo_friendly_title(path, title, seo_ending)
     if path == 'index'
      page_title = seo_ending
     elsif path.split('/').compact.length == 4
      page_title = title << " #{@title_separator} #{seo_ending}"
     else
      page_title = path.capitalize.gsub(/[-]/, ' ') << " #{@title_separator} #{seo_ending}"
    end
    page_title
end

.short_url_bitly(url, login, api_key) ⇒ Object

Retrieve bit.ly shortened url



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/karakuri.rb', line 76

def Karakuri.short_url_bitly(url, , api_key)
  if api_key != "" &&  != ""
    rest_call=%{http://api.bit.ly/v3/shorten?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_cloud(articles) ⇒ Object

Tag cloud, return a hash of tags and their frequency



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/karakuri.rb', line 118

def Karakuri.tag_cloud(articles)
  tag_cloud = {}
  if(articles)
    articles.select do |article|
      if tags = csv_to_array(article[:tags])
        tags.each do |tag| 
          if tag_cloud.has_key? tag
            tag_cloud[tag] += 1
          else
            tag_cloud[tag] = 1
          end
        end
      end
    end
  end
  tag_cloud
end

create a list of links to tagged articles, default link_format: %&amp;&lt;a href=&quot;/tagged?tag=#{tag}&quot; title=&quot;articles concerning #{tag}&quot; &gt;#{tag}&lt;/a&gt; &amp;



22
23
24
25
26
27
28
29
30
# File 'lib/karakuri.rb', line 22

def Karakuri.tag_link_list(csv_string)
  # read csv-string into array
  tag_list = csv_to_array(csv_string)
  if tag_list
    tag_string = ""
    tag_list.each { |tag| tag_string << @link_format.gsub('{tag}', tag) }
  end
  tag_string
end

.title_separator(separator) ⇒ Object



16
17
18
# File 'lib/karakuri.rb', line 16

def self.title_separator(separator)
  @title_separator = separator
end