diff options
author | Azul <azul@riseup.net> | 2016-03-24 14:14:18 +0100 |
---|---|---|
committer | Azul <azul@riseup.net> | 2016-05-02 08:28:44 -0300 |
commit | 4bfbeedd9cf5f0f2a4b364764afd9c6acdc7399f (patch) | |
tree | 73179bde2951099510f6f99138192378cd4d543a | |
parent | 5d18e8c396181ee8fab3f8579bc19abaee106d52 (diff) |
upgrade: separate CouchRest::Session from Document
CouchRest::Session now offers the high leven API while
CouchRest::Session::Document only extends the CouchRest::Document
to have the properties (rotation, naming, etc...) that we want.
A Session has a Session::Document instead of inheriting from it.
6 files changed, 114 insertions, 110 deletions
diff --git a/vendor/gems/couchrest_session_store/.ruby-version b/vendor/gems/couchrest_session_store/.ruby-version deleted file mode 100644 index f3a9c9a..0000000 --- a/vendor/gems/couchrest_session_store/.ruby-version +++ /dev/null @@ -1 +0,0 @@ -1.9.3-p194 diff --git a/vendor/gems/couchrest_session_store/lib/couchrest/session.rb b/vendor/gems/couchrest_session_store/lib/couchrest/session.rb index 430732c..416a88d 100644 --- a/vendor/gems/couchrest_session_store/lib/couchrest/session.rb +++ b/vendor/gems/couchrest_session_store/lib/couchrest/session.rb @@ -1,3 +1,9 @@ +class CouchRest::Session +end + +require 'couchrest/session/utility' +require 'couchrest/session/document' + module CouchRest class StorageMissing < Exception @@ -9,7 +15,91 @@ module CouchRest end end - module Session + class Session + include CouchRest::Session::Utility + + def self.fetch(sid) + self.allocate.tap do |session_doc| + session_doc.fetch(sid) || raise(CouchRest::NotFound) + end + end + + def self.build(sid, session, options = {}) + session_doc = CouchRest::Session::Document.new "_id" => sid + self.new(session_doc). + update session, options + end + + def self.build_or_update(sid, session, options = {}) + options[:marshal_data] = true if options[:marshal_data].nil? + self.fetch(sid). + update session, options + rescue CouchRest::NotFound + self.build sid, session, options + end + + def initialize(doc) + @doc = doc + end + + def fetch(sid = nil) + @doc = CouchRest::Session::Document.fetch(sid || doc['_id']) + end + + def to_session + if doc["marshalled"] + session = unmarshal(doc["data"]) + else + session = doc["data"] + end + return session + end + + def delete + doc.destroy + end + + def update(session, options) + # clean up old data but leave id and revision intact + doc.reject! { |k,_v| k[0] != '_' } + doc.merge! data_for_doc(session, options) + self + end + + def save + doc.save + rescue CouchRest::Conflict + fetch + retry + rescue CouchRest::NotFound => exc + if exc.http_body =~ /no_db_file/ + exc = CouchRest::StorageMissing.new(exc.response, doc.database) + end + raise exc + end + + def expired? + expires && expires < Time.now + end + + protected + + attr_reader :doc + + def data_for_doc(session, options) + { "data" => options[:marshal_data] ? marshal(session) : session, + "marshalled" => options[:marshal_data], + "expires" => expiry_from_options(options) } + end + + def expiry_from_options(options) + expire_after = options[:expire_after] + expire_after && (Time.now + expire_after).utc + end + + def expires + doc["expires"] && Time.iso8601(doc["expires"]) + end + end end - diff --git a/vendor/gems/couchrest_session_store/lib/couchrest/session/document.rb b/vendor/gems/couchrest_session_store/lib/couchrest/session/document.rb index b1e73cc..dc938cf 100644 --- a/vendor/gems/couchrest_session_store/lib/couchrest/session/document.rb +++ b/vendor/gems/couchrest_session_store/lib/couchrest/session/document.rb @@ -1,34 +1,15 @@ -require 'couchrest/session/utility' require 'time' class CouchRest::Session::Document < CouchRest::Document include CouchRest::Model::Configuration include CouchRest::Model::Connection - include CouchRest::Session::Utility include CouchRest::Model::Rotation rotate_database 'sessions', :every => 1.month, :expiration_field => :expires - def self.fetch(sid) - self.allocate.tap do |session_doc| - session_doc.fetch(sid) - end - end - - def self.build(sid, session, options = {}) - self.new(CouchRest::Document.new({"_id" => sid})).tap do |session_doc| - session_doc.update session, options - end - end - - def self.build_or_update(sid, session, options = {}) - options[:marshal_data] = true if options[:marshal_data].nil? - doc = self.fetch(sid) - doc.update(session, options) - return doc - rescue CouchRest::NotFound - self.build(sid, session, options) + def self.fetch(id) + database.get(id) end def self.find_by_expires(options = {}) @@ -50,70 +31,4 @@ class CouchRest::Session::Document < CouchRest::Document db end - def initialize(doc) - @doc = doc - end - - def fetch(sid = nil) - @doc = database.get(sid || doc['_id']) - end - - def to_session - if doc["marshalled"] - session = unmarshal(doc["data"]) - else - session = doc["data"] - end - return session - end - - def delete - database.delete_doc(doc) - end - - def update(session, options) - # clean up old data but leave id and revision intact - doc.reject! do |k,v| - k[0] != '_' - end - doc.merge! data_for_doc(session, options) - end - - def save - database.save_doc(doc) - rescue CouchRest::Conflict - fetch - retry - rescue CouchRest::NotFound => exc - if exc.http_body =~ /no_db_file/ - exc = CouchRest::StorageMissing.new(exc.response, database) - end - raise exc - end - - def expired? - expires && expires < Time.now - end - - protected - - def data_for_doc(session, options) - { "data" => options[:marshal_data] ? marshal(session) : session, - "marshalled" => options[:marshal_data], - "expires" => expiry_from_options(options) } - end - - def expiry_from_options(options) - expire_after = options[:expire_after] - expire_after && (Time.now + expire_after).utc - end - - def expires - doc["expires"] && Time.iso8601(doc["expires"]) - end - - def doc - @doc - end - end diff --git a/vendor/gems/couchrest_session_store/lib/couchrest/session/store.rb b/vendor/gems/couchrest_session_store/lib/couchrest/session/store.rb index f209f54..516d5dd 100644 --- a/vendor/gems/couchrest_session_store/lib/couchrest/session/store.rb +++ b/vendor/gems/couchrest_session_store/lib/couchrest/session/store.rb @@ -54,8 +54,8 @@ class CouchRest::Session::Store < ActionDispatch::Session::AbstractStore def set_session(env, sid, session, options) raise CouchRest::NotFound if /^_design\/(.*)/ =~ sid - doc = build_or_update_doc(sid, session, options) - doc.save + couchrest_session = build_or_update_doc(sid, session, options) + couchrest_session.save return sid # if we can't store the session we just return false. rescue CouchRest::Unauthorized, @@ -75,12 +75,12 @@ class CouchRest::Session::Store < ActionDispatch::Session::AbstractStore def fetch_session(sid) return nil unless sid - doc = secure_get(sid) - doc.to_session unless doc.expired? + couchrest_session = secure_get(sid) + couchrest_session.to_session unless couchrest_session.expired? end def build_or_update_doc(sid, session, options) - CouchRest::Session::Document.build_or_update(sid, session, options) + CouchRest::Session.build_or_update(sid, session, options) end # prevent access to design docs @@ -88,7 +88,7 @@ class CouchRest::Session::Store < ActionDispatch::Session::AbstractStore # but better be save than sorry. def secure_get(sid) raise CouchRest::NotFound if /^_design\/(.*)/ =~ sid - CouchRest::Session::Document.fetch(sid) + CouchRest::Session.fetch(sid) end end diff --git a/vendor/gems/couchrest_session_store/test/session_document_test.rb b/vendor/gems/couchrest_session_store/test/session_document_test.rb index 2125d10..43fbbca 100644 --- a/vendor/gems/couchrest_session_store/test/session_document_test.rb +++ b/vendor/gems/couchrest_session_store/test/session_document_test.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.dirname(__FILE__) + '/test_helper') +require_relative 'test_helper' class SessionDocumentTest < MiniTest::Test @@ -6,22 +6,22 @@ class SessionDocumentTest < MiniTest::Test sid = '1234' session = {'a' => 'b'} options = {} - doc = CouchRest::Session::Document.build_or_update(sid, session, options) - doc.save - doc.fetch(sid) - assert_equal session, doc.to_session + couchrest_session = CouchRest::Session.build_or_update(sid, session, options) + couchrest_session.save + couchrest_session.fetch(sid) + assert_equal session, couchrest_session.to_session end def test_storing_session_with_conflict sid = '1234' session = {'a' => 'b'} options = {} - doc = CouchRest::Session::Document.build_or_update(sid, session, options) - doc2 = CouchRest::Session::Document.build_or_update(sid, session, options) - doc.save - doc2.save - doc2.fetch(sid) - assert_equal session, doc2.to_session + cr_session = CouchRest::Session.build_or_update(sid, session, options) + cr_session2 = CouchRest::Session.build_or_update(sid, session, options) + cr_session.save + cr_session2.save + cr_session2.fetch(sid) + assert_equal session, cr_session2.to_session end end diff --git a/vendor/gems/couchrest_session_store/test/session_store_test.rb b/vendor/gems/couchrest_session_store/test/session_store_test.rb index 4fbf30b..5949ae6 100644 --- a/vendor/gems/couchrest_session_store/test/session_store_test.rb +++ b/vendor/gems/couchrest_session_store/test/session_store_test.rb @@ -64,10 +64,10 @@ class SessionStoreTest < MiniTest::Test def test_stored_and_not_expired_yet sid, session = expiring_session - doc = CouchRest::Session::Document.fetch(sid) - expires = doc.send :expires + couchrest_session = CouchRest::Session.fetch(sid) + expires = couchrest_session.send :expires assert expires - assert !doc.expired? + assert !couchrest_session.expired? assert (expires - Time.now) > 0, "Exiry should be in the future" assert (expires - Time.now) <= 300, "Should expire after 300 seconds - not more" assert_equal [sid, session], store.send(:get_session, env, sid) |