├── lib └── sinatra │ └── redis.rb ├── sinatra-redis.gemspec └── README.md /lib/sinatra/redis.rb: -------------------------------------------------------------------------------- 1 | require 'uri' 2 | require 'redis' 3 | 4 | module Sinatra 5 | module RedisHelper 6 | def redis 7 | settings.redis 8 | end 9 | end 10 | 11 | module Redis 12 | def redis=(url) 13 | @redis = nil 14 | set :redis_url, url 15 | redis 16 | end 17 | 18 | def redis 19 | @redis ||= ( 20 | url = URI(redis_url) 21 | 22 | base_settings = { 23 | :host => url.host, 24 | :port => url.port, 25 | :db => url.path[1..-1], 26 | :password => url.password 27 | } 28 | 29 | ::Redis.new( 30 | base_settings.merge( 31 | redis_settings 32 | ) 33 | ) 34 | ) 35 | end 36 | 37 | protected 38 | 39 | def self.registered(app) 40 | app.set :redis_url, ENV['REDIS_URL'] || "redis://127.0.0.1:6379/0" 41 | app.set :redis_settings, {} 42 | app.helpers RedisHelper 43 | end 44 | end 45 | 46 | register Redis 47 | end 48 | -------------------------------------------------------------------------------- /sinatra-redis.gemspec: -------------------------------------------------------------------------------- 1 | Gem::Specification.new do |s| 2 | s.specification_version = 2 if s.respond_to? :specification_version= 3 | s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= 4 | 5 | s.name = 'sinatra-redis' 6 | s.version = '0.3.0' 7 | s.date = '2009-09-21' 8 | 9 | s.description = "Extends Sinatra with redis helpers for instant redis use" 10 | s.summary = s.description 11 | 12 | s.authors = ["Blake Mizerany"] 13 | s.email = "blake.mizerany@gmail.com" 14 | 15 | s.add_dependency "redis" 16 | 17 | # = MANIFEST = 18 | s.files = %w[ 19 | README.md 20 | lib/sinatra/redis.rb 21 | sinatra-redis.gemspec 22 | ] 23 | # = MANIFEST = 24 | 25 | s.extra_rdoc_files = %w[README.md] 26 | s.add_dependency 'sinatra', '>= 0.9.4' 27 | 28 | s.has_rdoc = true 29 | s.homepage = "http://github.com/rtomayko/sinatra-redis" 30 | s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Sinatra::Redis"] 31 | s.require_paths = %w[lib] 32 | s.rubyforge_project = 'bmizerany' 33 | s.rubygems_version = '1.1.1' 34 | end 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Sinatra Redis Extension 2 | ======================== 3 | 4 | Extends [Sinatra](http://www.sinatrarb.com/) with an extension method for 5 | dealing with redis databases using the [redis-rb](http://github.com/ezmobius/redis-rb) client library that 6 | comes with the redis source (pre-packaged with this library for convenience). 7 | You can Install the `sinatra-redis` with rip 8 | 9 | $ rip install git://github.com/bmizerany/sinatra-redis.git 10 | 11 | or 12 | $ gem install sinatra-redis -s http://gemcutter.org 13 | or 14 | 15 | $ gem install bmizerany-sinatra-redis -s http://gems.github.com 16 | 17 | and use like: 18 | 19 | $ vim sinatra-using-redis.rb 20 | 21 | require 'sinatra' 22 | require 'sinatra/redis' 23 | 24 | # Establish the database connection; or, omit this and use the REDIS_URL 25 | # environment variable as the connection string; or, default to redis://locahost:6379/0 26 | # 27 | # NOTE: The database is the integer in the path 28 | # set :redis, 'redis://some-remote-server:1234/5' 29 | 30 | # At this point, you can access the Redis object using the "redis" object: 31 | puts redis.delete "foos" 32 | puts redis.rpush "foos", "redis" 33 | puts redis.rpush "foos", "is" 34 | puts redis.rpush "foos", "sweet!" 35 | 36 | # access redis within the context of an HTTP request 37 | get '/foos' do 38 | @foos = redis.lrange("foos", 0, -1) # Array 39 | @foos.inspect 40 | end 41 | 42 | $ ruby sinatra-using-redis.rb 43 | 44 | ### Redis Reference Material 45 | 46 | * The [Redis Wiki](http://code.google.com/p/redis/) 47 | 48 | * The [Redis Command Reference](http://code.google.com/p/redis/wiki/CommandReference) 49 | 50 | * The [Redis Source](http://github.com/antirez/redis) 51 | 52 | * Ezra's Mountain West Ruby Conf '09 [Talk](http://mwrc2009.confreaks.com/13-mar-2009-19-24-redis-key-value-nirvana-ezra-zygmuntowicz.html) 53 | 54 | ### NOTE about the rip-off 55 | 56 | This Code and README.md is a heavy adaption of [rtomayko's sinatra-sequel](http://github.com/rtomayko/sinatra-sequel/) 57 | 58 | # LICENSE 59 | 60 | Copyright (c) 2009 Blake Mizerany 61 | 62 | Permission is hereby granted, free of charge, to any person 63 | obtaining a copy of this software and associated documentation 64 | files (the "Software"), to deal in the Software without 65 | restriction, including without limitation the rights to use, 66 | copy, modify, merge, publish, distribute, sublicense, and/or sell 67 | copies of the Software, and to permit persons to whom the 68 | Software is furnished to do so, subject to the following 69 | conditions: 70 | 71 | The above copyright notice and this permission notice shall be 72 | included in all copies or substantial portions of the Software. 73 | 74 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 75 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 76 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 77 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 78 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 79 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 80 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 81 | OTHER DEALINGS IN THE SOFTWARE. 82 | --------------------------------------------------------------------------------