A custom Swedish time_ago_in_words implementation

Ruby posted 8 days ago by christian

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

Tagged time_ago_in_words, ruby, rails, time, swedish, svenska, inflections

How to fetch delicious data with Hpricot and OpenURI

Ruby posted 15 days ago by christian

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.

Tagged delicious, hpricot, ruby, rss, feed

Custom Rails time_ago_in_words implementation

Ruby posted about 1 month ago by christian

   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
Tagged time_ago_in_words, time, ruby

A random password generator for Ruby

Ruby posted 2 months ago by christian

This is a simple random password generator for Ruby that allows you to customize the characters included in the password:

   1  CHARS = ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a
   2  def random_password(length=10)
   3    CHARS.sort_by { rand }.join[0...length]
   4  end

In this example, the generated passwords contain numbers, uppercase, and lowercase characters.

Tagged random, password, ruby

How to write a custom DataMapper type for marshaled data

Ruby posted 2 months ago by christian

This is an example of how to write a custom data type for Ruby’s DataMapper. We’re going to write something similar to DataMapper::Property::Object

DataMapper >= 1.0

This code is for DataMapper version 1.0 and later where the DataMapper type concept has been merged with properties:

   1  module DataMapper
   2    class Property
   3      class Marshal < Object
   4        primitive ::Object
   5        def load(value)
   6          ::Marshal.load(value) if value
   7        end
   8  
   9        def dump(value)
  10          ::Marshal.dump(value) if value
  11        end
  12      end
  13    end
  14  end

DataMapper < 1.0

   1  require 'dm-core'
   2  
   3  module DataMapper
   4    module Types
   5      class Marshal < DataMapper::Type
   6        primitive Text
   7  
   8        def self.load(value, property)
   9          ::Marshal.load(value) if value
  10        end
  11  
  12        def self.dump(value, property)
  13          ::Marshal.dump(value) if value
  14        end
  15      end 
  16    end 
  17  end 

Next declare your field:

   1  property :properties, Marshal

Which is the same as property :properties, Object in this case.

Tagged marshal, datamapper, ruby, serialize, type, property