riak_sessions 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CONTRIBUTORS ADDED
@@ -0,0 +1,3 @@
1
+ * Dan Reverri (dan@appush.com)
2
+ * Igor Guerrero (igor@appush.com)
3
+ * Matt Heitzenroder (matt@appush.com)
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Igor Guerrero
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ Riak Sessions
2
+ =============
3
+
4
+ Using [Riak][1] to store sessions in Rack based applications.
5
+
6
+ Overview
7
+ --------
8
+
9
+ Lets say you are already using Riak, this awesome document-oriented Web and you
10
+ might think of avoiding using Memcache, Pool and since you already have a
11
+ database, lets store it there right?, this library plugs it into Rack so you
12
+ can use it in your Sinatra, Merb, Camping, etc.
13
+
14
+ Why?
15
+ ----
16
+
17
+ Because I kinda like Riak.
18
+
19
+ Installing
20
+ ----------
21
+
22
+ Get Riak: [http://riak.basho.com/][1]
23
+ Get the Jiak Ruby client: [http://hg.basho.com/riak/src/tip/client_lib/jiak.rb][2]
24
+
25
+ Usage
26
+ -----
27
+
28
+ Quick Sinatra app, using flash to test the sessions (included in the examples folder too):
29
+
30
+ /config.ru:
31
+
32
+ require 'app'
33
+
34
+ run App
35
+
36
+ /app.rb:
37
+
38
+ require 'rubygems'
39
+ require 'sinatra/base'
40
+ require 'rack-flash'
41
+ require 'rack_sessions'
42
+
43
+ class App < Sinatra::Base
44
+ use Rack::Session::Riak
45
+
46
+ use Rack::Flash
47
+
48
+ get '/' do
49
+ flash[:notice] = "hello!"
50
+ erb :index
51
+ end
52
+ end
53
+
54
+ /views/index.erb
55
+
56
+ <% if flash.has?(:notice) %>
57
+ <h1>Notice: <%= flash[:notice] %></h1>
58
+ <% end %>
59
+
60
+ You can also specify options to your Riak server and port and options:
61
+
62
+ use Rack::Session::Riak, :riak_server => 'example.com', :riak_port => 8888, :options => {'w'=>'3','dw'=>'3'}
63
+
64
+ [1]: http://riak.basho.com/
65
+ [2]: http://hg.basho.com/riak/src/tip/client_lib/jiak.rb
data/example/app.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'sinatra/base'
3
+ require 'rack-flash'
4
+
5
+ require '../lib/riak_sessions'
6
+
7
+ class App < Sinatra::Base
8
+ use Rack::Session::Riak
9
+
10
+ use Rack::Flash
11
+
12
+ get '/' do
13
+ flash[:notice] = "hello!"
14
+ erb :index
15
+ end
16
+ end
data/example/config.ru ADDED
@@ -0,0 +1,3 @@
1
+ require 'app'
2
+
3
+ run App
@@ -0,0 +1,3 @@
1
+ <% if flash.has?(:notice) %>
2
+ <h1>Notice: <%= flash[:notice] %></h1>
3
+ <% end %>
@@ -0,0 +1,109 @@
1
+ require 'rack/session/abstract/id'
2
+ require 'thread'
3
+ require 'base64'
4
+ require 'jiak'
5
+
6
+ module Rack
7
+
8
+ # The Rack::Session::Riak uses the open source document-oriented web database Riak as a sessions backend,
9
+ # it uses the Jiak ruby client library: http://hg.basho.com/riak/src/tip/client_lib/jiak.rb
10
+ # more info about Riak: http://riak.basho.com/
11
+ #
12
+ # Examples:
13
+ # use Rack::Session::Riak
14
+ # will use Riak as a session handler
15
+
16
+ module Session
17
+ class RiakPool
18
+ def initialize(server, port, options)
19
+ @riak = JiakClient.new(server, port, '/jiak/', options)
20
+ end
21
+
22
+ def [](session_id)
23
+ get(session_id)
24
+ end
25
+
26
+ def get(session_id)
27
+ raise if session_id.nil? or session_id.empty?
28
+ session = @riak.fetch('rack_session', session_id)
29
+ data = Marshal.load(Base64.decode64(session['object']['data']))
30
+ return data
31
+ rescue JiakException => e
32
+ return nil
33
+ end
34
+
35
+ def store(session_id, data)
36
+ set(session_id, data)
37
+ end
38
+
39
+ def set(session_id, data)
40
+ begin
41
+ raise JiakException if session_id.nil?
42
+ session = @riak.fetch('rack_session', session_id)
43
+ rescue JiakException => e
44
+ session = {'bucket'=>'rack_session', 'key'=>session_id, 'links'=>[]}
45
+ end
46
+ session['object'] = {'data'=>Base64.encode64(Marshal.dump(data))}
47
+ @riak.store(session)
48
+ return true
49
+ end
50
+
51
+ def delete(session_id)
52
+ @riak.delete('rack_session', session_id)
53
+ end
54
+ end
55
+
56
+ class Riak < Abstract::ID
57
+ attr_reader :mutex, :pool
58
+ DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge(
59
+ :drop => false,
60
+ :riak_server => '127.0.0.1',
61
+ :riak_port => 8098,
62
+ :riak_options => {'w'=>'3','dw'=>'3'})
63
+
64
+ def initialize(app, options={})
65
+ super
66
+
67
+ @pool = RiakPool.new(@default_options[:riak_server],
68
+ @default_options[:riak_port],
69
+ @default_options[:riak_options])
70
+ @mutex = Mutex.new
71
+ end
72
+
73
+ def get_session(env, sid)
74
+ session = @pool[sid] if sid
75
+ @mutex.lock if env['rack.multithread']
76
+
77
+ unless sid and session
78
+ env['rack.errors'].puts("Session '#{sid.inspect}' not found, initializing...") if $VERBOSE and not sid.nil?
79
+ session = {}
80
+ sid = generate_sid
81
+ @pool.store sid, session
82
+ end
83
+
84
+ return [sid, session]
85
+ ensure
86
+ @mutex.unlock if env['rack.multithread']
87
+ end
88
+
89
+ def set_session(env, session_id, new_session, options)
90
+ @mutex.lock if env['rack.multithread']
91
+
92
+ if options[:renew] or options[:drop]
93
+ @pool.delete session_id
94
+ return false if options[:drop]
95
+ session_id = generate_sid
96
+ @pool.store session_id, 0
97
+ end
98
+
99
+ @pool.store session_id, new_session
100
+ return session_id
101
+ rescue
102
+ warn "#{new_session.inspect} has been lost."
103
+ warn $!.inspect
104
+ ensure
105
+ @mutex.unlock if env['rack.multithread']
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'riak_sessions'
5
+ s.version = '0.1'
6
+
7
+ s.authors = ['Igor Guerrero']
8
+ s.date = '2009-11-24'
9
+ s.description = "Riak sessions for Rack based application."
10
+ s.email = ['igor@appush.com']
11
+ s.homepage = 'http://appush.com'
12
+ s.files = ['riak_sessions.gemspec', 'CONTRIBUTORS', 'LICENSE', 'README.md', 'lib/riak_sessions.rb',
13
+ 'example/app.rb', 'example/config.ru', 'example/views/index.erb']
14
+ s.summary ='Riak sessions for Rack based applications'
15
+
16
+ s.add_dependency 'rack', '>= 1.0'
17
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: riak_sessions
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Igor Guerrero
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-24 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "1.0"
24
+ version:
25
+ description: Riak sessions for Rack based application.
26
+ email:
27
+ - igor@appush.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files: []
33
+
34
+ files:
35
+ - riak_sessions.gemspec
36
+ - CONTRIBUTORS
37
+ - LICENSE
38
+ - README.md
39
+ - lib/riak_sessions.rb
40
+ - example/app.rb
41
+ - example/config.ru
42
+ - example/views/index.erb
43
+ has_rdoc: true
44
+ homepage: http://appush.com
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.5
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Riak sessions for Rack based applications
71
+ test_files: []
72
+