session_jsonizer 0.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.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +46 -0
- data/Rakefile +32 -0
- data/lib/session_jsonizer.rb +64 -0
- data/lib/session_jsonizer/version.rb +3 -0
- data/lib/tasks/session_jsonizer_tasks.rake +4 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6279189ea23b5a177508a70145bdcb360090d74c
|
4
|
+
data.tar.gz: 15482a1e2f226b460339b95bf883e9debf1ad97c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0fb691517c6ef8c5a67ff955693f5911029ca9fbf84b88eed223d7cc87c2de668e628ea9ddb01f94d1773146efa3fe049593d5c6c5ba1502503da41fab2f01e2
|
7
|
+
data.tar.gz: 6a6289f7b419b16ccd0c220b557d3fb51026315040a110b030c59b0c5133ce9953d0440fe8c65a39d58777f96f682406db742381276ecc7ae9584140a1e0c795
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 PIXTA Inc.
|
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,46 @@
|
|
1
|
+
# rails-session_jsonizer
|
2
|
+
|
3
|
+
rails-session_jsonizer is a simple library for Rails 3 and 4 to
|
4
|
+
serialize session into JSON in the same format.
|
5
|
+
|
6
|
+
*Work in progress*
|
7
|
+
|
8
|
+
[](https://travis-ci.org/pixta-dev/rails-session_jsonizer)
|
9
|
+
|
10
|
+
## Requirement
|
11
|
+
|
12
|
+
* Rails 3.1 or later
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
### In controller
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
session[:foo] = 'bar'
|
20
|
+
flash[:alert] = 'something went wrong'
|
21
|
+
```
|
22
|
+
|
23
|
+
### Serialize
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'session_jsonizer'
|
27
|
+
|
28
|
+
# dump
|
29
|
+
serializer = SessionJsonizer.new
|
30
|
+
|
31
|
+
json = serializer.dump(session)
|
32
|
+
|
33
|
+
json # => '{"foo":"bar", "flash":{"alert":"something went wrong"}, ...}'
|
34
|
+
|
35
|
+
# load
|
36
|
+
session = serializer.load(json)
|
37
|
+
```
|
38
|
+
|
39
|
+
### Example: Store session into memcached as JSON using [dalli](https://github.com/mperham/dalli)
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
# config/initializers/session_store.rb
|
43
|
+
|
44
|
+
require 'action_dispatch/middleware/session/dalli_store'
|
45
|
+
Rails.application.config.session_store :dalli_store, serializer: SessionJsonizer.new, ...
|
46
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'SessionJsonizer'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
task :spec do
|
18
|
+
Dir.chdir './spec/dummy' do
|
19
|
+
system 'bundle exec rspec' or abort
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
task :setup_spec do
|
24
|
+
Dir.chdir './spec/dummy' do
|
25
|
+
system 'RAILS_ENV=test bundle exec rake db:create' or abort
|
26
|
+
system 'RAILS_ENV=test bundle exec rake db:migrate' or abort
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
task :default => :spec
|
31
|
+
|
32
|
+
Bundler::GemHelper.install_tasks
|
@@ -0,0 +1,64 @@
|
|
1
|
+
class SessionJsonizer
|
2
|
+
|
3
|
+
def load(value)
|
4
|
+
::Rails.logger.debug "serializer load: #{value}"
|
5
|
+
if value.first == '{'
|
6
|
+
Hash[::JSON.load(value).map do |key, value|
|
7
|
+
[
|
8
|
+
key,
|
9
|
+
case
|
10
|
+
when key == 'flash'
|
11
|
+
load_flash(value)
|
12
|
+
when value.is_a?(Hash)
|
13
|
+
value.with_indifferent_access
|
14
|
+
else
|
15
|
+
value
|
16
|
+
end
|
17
|
+
]
|
18
|
+
end]
|
19
|
+
else
|
20
|
+
::Marshal.load(value)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def dump(session)
|
25
|
+
::Rails.logger.debug "serializer dump: #{session}"
|
26
|
+
|
27
|
+
new_session = Hash[session.map do |key, value|
|
28
|
+
[
|
29
|
+
key,
|
30
|
+
if key == 'flash'
|
31
|
+
dump_flash(value)
|
32
|
+
else
|
33
|
+
value
|
34
|
+
end
|
35
|
+
]
|
36
|
+
end]
|
37
|
+
|
38
|
+
::JSON.dump(new_session)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
case Rails::VERSION::MAJOR
|
44
|
+
when 4
|
45
|
+
def load_flash(data)
|
46
|
+
{ 'flashes' => data, 'discard' => [] }
|
47
|
+
end
|
48
|
+
|
49
|
+
def dump_flash(session_value)
|
50
|
+
ActionDispatch::Flash::FlashHash.from_session_value(session_value).to_hash
|
51
|
+
end
|
52
|
+
|
53
|
+
when 3
|
54
|
+
def load_flash(data)
|
55
|
+
ActionDispatch::Flash::FlashHash.new.update(data.symbolize_keys)
|
56
|
+
end
|
57
|
+
|
58
|
+
def dump_flash(flash)
|
59
|
+
flash.sweep
|
60
|
+
flash.to_hash
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: session_jsonizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryohei Ikegami
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: |-
|
56
|
+
session_jsonizer is a simple library for Rails 3 and 4 to
|
57
|
+
serialize session as JSON in the same format.
|
58
|
+
email:
|
59
|
+
- iofg2100@gmail.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- lib/session_jsonizer/version.rb
|
65
|
+
- lib/session_jsonizer.rb
|
66
|
+
- lib/tasks/session_jsonizer_tasks.rake
|
67
|
+
- MIT-LICENSE
|
68
|
+
- Rakefile
|
69
|
+
- README.md
|
70
|
+
homepage: https://github.com/pixta-dev/rails-session_jsonizer
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.0.14
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: Serialize Rails 3 / 4 session as JSON
|
94
|
+
test_files: []
|