A custom Swedish time_ago_in_words implementation
This is an easily customizable time_ago_in_words implementation in Swedish that will produce this output:
1 < 5 minuter 2 < 45 minuter 3 < 1 timme 4 > 2 timmar 5 > 1 dag, 2 timmar 6 20 April 2010 (if more than 31 days ago)
The code:
1 class Time 2 def time_ago_in_words 3 words = '' 4 timestamp = self 5 if timestamp 6 time_until = (Time.now.to_datetime - timestamp.to_datetime) 7 days = time_until.to_i 8 hours, minutes, seconds, frac = Date.day_fraction_to_time(time_until) 9 hours = hours % 24 10 11 if days == 0 12 if hours < 1 13 words = case minutes 14 when 0..4 then "< 5 minuter" 15 when 5..14 then "< 15 minuter" 16 when 15..29 then "< 30 minuter" 17 when 30..44 then "< 45 minuter" 18 when 45..59 then "< 1 timme" 19 end 20 else 21 words = "> #{hours} #{'timme'.inflect(hours)}" 22 end 23 elsif days < 31 24 words = "> #{days} #{'dag'.inflect(days)}" 25 if hours > 0 26 words += ", #{hours} #{'timme'.inflect(hours)}" 27 end 28 else 29 words = timestamp.l(:format => :daymonthyear) 30 end 31 end 32 words 33 end 34 end
You’ll need this for inflections:
1 class String 2 def inflect(count) 3 count > 1 ? ActiveSupport::Inflector.pluralize(self) : self 4 end 5 end
Add this to config/locales/sv-SE.yml for localizing the date in Swedish:
1 time: 2 formats: 3 daymonthyear: "%d %B %Y"
Add this to config/initializers/inflections.rb for Swedish inflections:
1 ActiveSupport::Inflector.inflections do |inflect| 2 inflect.irregular 'dag', 'dagar' 3 inflect.irregular 'vecka', 'veckor' 4 inflect.irregular 'månad', 'månader' 5 inflect.irregular 'timme', 'timmar' 6 inflect.irregular 'minut', 'minuter' 7 inflect.irregular 'sekund', 'sekunder' 8 end
How to fetch delicious data with Hpricot and OpenURI
The code:
1 class Delicious 2 class << self 3 def tag(username, name, count = 15) 4 links = [] 5 url = "http://feeds.delicious.com/v2/rss/#{username}/#{name}?count=#{count}" 6 feed = Hpricot(open(url)) 7 8 feed.search("item").each do |i| 9 item = OpenStruct.new 10 item.link = i.at('link').next.to_s 11 item.title = i.at('title').innerHTML 12 item.description = i.at('description').innerHTML rescue nil 13 14 links << item 15 end 16 17 links 18 end 19 end 20 end
Usage:
1 # Return last 15 items tagged with business and news from jebus's account: 2 Delicious.tag 'jebus', 'business+news', 15
Returns an array of items.
Fix for "530 5.7.0 Must issue a STARTTLS command first" and Ruby mail gem
I’m using the mail gem and received this error when sending emails:
1 530 5.7.0 Must issue a STARTTLS command first
The fix was to first install the tlsmail gem:
1 gem install tlsmail
Then I had to add this to config/initializers/email.rb:
1 require 'tlsmail' 2 Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
How to support optional variables in ERB templates
This will give the variable “something” a value of “nothing” if it’s not defined:
1 <% 2 something = 'nothing' if local_assigns.has_key? :something 3 %>
Custom Rails time_ago_in_words implementation
1 class Time 2 def until_in_words 3 words = '' 4 if self 5 time_until = (self.to_datetime - Time.now.to_datetime) 6 days = time_until.to_i 7 if days == 0 8 hours, minutes, seconds, frac = Date.day_fraction_to_time(time_until) 9 words += %Q{<span class="number">#{hours.abs}</span> h} 10 words += %Q{<span class="number"> #{minutes.abs}</span> min} 11 words += %Q{<span class="number"> #{seconds.abs}</span> sec} 12 13 if time_until.to_f < 0 14 words += " ago" 15 else 16 words = "in " + words 17 end 18 else 19 words = %Q{<span class="number">#{days.abs}</span> } + "day".p(days.abs) 20 21 if days < 0 22 words += " ago" 23 else 24 words = "in " + words 25 end 26 end 27 28 end 29 words 30 end 31 end 32 end
This will give you, for example, the following:
- in 1684 days
- 3794 days ago
- 1 day ago
- 22 h 12 min 6 sec ago