Added v 0.2.2 snapshot.
[twitter4r-core.git] / lib / twitter / ext / stdlib.rb
blob1bd79751d4ca5edc17eea9a71cf8700b94353a2d
1 # Contains Ruby standard library extensions specific to <tt>Twitter4R</tt> library.
3 # Extension to Hash to create URL encoded string from key-values
4 class Hash
5   # Returns string formatted for HTTP URL encoded name-value pairs.
6   # For example,
7   #  {:id => 'thomas_hardy'}.to_http_str 
8   #  # => "id=thomas_hardy"
9   #  {:id => 23423, :since => Time.now}.to_http_str
10   #  # => "since=Thu,%2021%20Jun%202007%2012:10:05%20-0500&id=23423"
11   def to_http_str
12     result = ''
13     return result if self.empty?
14     self.each do |key, val|
15       result << "#{key}=#{URI.encode(val.to_s)}&"
16     end
17     result.chop # remove the last '&' character, since it can be discarded
18   end
19 end
21 # Extension to Time that outputs RFC2822 compliant string on #to_s
22 class Time
23   alias :old_to_s :to_s
24   # Returns RFC2822 compliant string for <tt>Time</tt> object.
25   # For example,
26   #  # Tony Blair's last day in office (hopefully)
27   #  best_day_ever = Time.local(2007, 6, 27)
28   #  best_day_ever.to_s # => "Wed, 27 Jun 2007 00:00:00 +0100"
29   def to_s
30     self.rfc2822
31   end
32 end