Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/top4r/ext/stdlib.rb

Overview

Extension to Hash to create URL encoded string from key-values

Instance Method Summary collapse

Instance Method Details

#to_http_strObject

Returns string formatted for HTTP URL encoded name-value pairs. For example, => ‘thomas_hardy’.to_http_str # => “id=thomas_hardy” => 23423, :since => Time.now.to_http_str # => “since=Thu,%2021%20Jun%202007%2012:10:05%20-0500&id=23423”



10
11
12
13
14
15
16
17
# File 'lib/top4r/ext/stdlib.rb', line 10

def to_http_str
  result = ''
  return result if self.empty?
  self.each do |key, val|
    result << "#{key}=#{CGI.escape(val.to_s)}&"
  end
  result.chop # remove the last '&' character, since it can be discarded
end