Browse files

Release 0.4.2 (URI#conf)

  • Loading branch information...
1 parent 3d91b43 commit 4264526318117264df09fd6f5a42ee605f1db0fd @delano committed Dec 23, 2010
Showing with 43 additions and 5 deletions.
  1. +6 −0 CHANGES.txt
  2. +1 −1 VERSION.yml
  3. +36 −4 lib/uri/redis.rb
View
6 CHANGES.txt
@@ -1,5 +1,11 @@
URI-Redis, CHANGES
+#### 0.4.2 (2010-12-23) ###############################
+
+# CHANGE: URI#conf enforces symbols as keys
+* ADDED: URI#conf parses query string
+
+
#### 0.4.1 (2010-11-16) ###############################
* FIXED: Nil error in URI::Redis#key
View
2 VERSION.yml
@@ -1,5 +1,5 @@
---
:MAJOR: 0
:MINOR: 4
-:PATCH: 1
+:PATCH: 2
:BUILD: '001'
View
40 lib/uri/redis.rb
@@ -42,20 +42,52 @@ def db=(val)
self.path
end
+ # Returns a hash suitable for sending to Redis.new.
+ # The hash is generated from the host, port, db and
+ # password from the URI as well as any query vars.
+ #
+ # e.g.
+ #
+ # uri = URI.parse "redis://127.0.0.1/6/?timeout=5"
+ # uri.conf
+ # # => {:db=>6, :timeout=>"5", :host=>"127.0.0.1", :port=>6379}
+ #
def conf
- tmp = {
+ hsh = {
:host => host,
:port => port,
:db => db
- }
- tmp[:password] = password if password
- tmp
+ }.merge parse_query(query)
+ hsh[:password] = password if password
+ hsh
end
def serverid
'redis://%s:%s/%s' % [host, port, db]
end
+ private
+
+ # Based on / stolen from: https://github.com/chneukirchen/rack/blob/master/lib/rack/utils.rb
+ # which was based on / stolen from Mongrel
+ def parse_query(qs, d = '&;')
+ params = {}
+ (qs || '').split(/[#{d}] */n).each do |p|
+ k, v = p.split('=', 2).map { |str| str } # NOTE: uri_unescape
+ k = k.to_sym
+ if cur = params[k]
+ if cur.class == Array
+ params[k] << v
+ else
+ params[k] = [cur, v]
+ end
+ else
+ params[k] = v
+ end
+ end
+ params
+ end
+
end
@@schemes['REDIS'] = Redis

0 comments on commit 4264526

Please sign in to comment.