redis_json_serializer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - rbx-19mode
6
+
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in redis_json_serializer.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rspec'
8
+ gem 'rspec-core'
9
+ gem 'rake'
10
+ end
11
+
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012- HARUYAMA Makoto - http://github.com/SpringMT
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,35 @@
1
+ # RedisJsonSerializer [![Build Status](https://travis-ci.org/SpringMT/redis_json_serializer.png)](https://travis-ci.org/SpringMT/redis_json_serializer)
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'redis_json_serializer'
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install redis_json_serializer
16
+
17
+ ## Usage
18
+
19
+ require 'redis_json_serializer'
20
+
21
+ ## Contributing
22
+
23
+ 1. Fork it
24
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
25
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
26
+ 4. Push to the branch (`git push origin my-new-feature`)
27
+ 5. Create new Pull Request
28
+
29
+ ## Copyright
30
+
31
+ Copyright (c) 2012- HARUAYMA Makoto (SpringMT)
32
+
33
+ ## License
34
+ MIT (see MIT-LICENSE)
35
+
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :test
3
+
4
+ task :test do
5
+ require 'rspec/core'
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:test) do |spec|
8
+ spec.pattern = FileList['spec/**/*_spec.rb']
9
+ end
10
+ end
11
+
@@ -0,0 +1,51 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'redis_json_serializer/version'
4
+ require 'redis'
5
+ require 'redis-namespace'
6
+ require 'json'
7
+
8
+ module RedisJsonSerializer
9
+ class Serializer < Redis::Namespace
10
+ def initialize(options = {})
11
+ @host = options[:host] || '127.0.0.1'
12
+ @port = options[:port] || 6379
13
+ @namespace = options[:namespace] || :mock
14
+
15
+ r = ::Redis.new(host: @host, port: @port)
16
+ super(@namespace, redis: r)
17
+ end
18
+
19
+ def set(key, value)
20
+ _serialize(value) { |serialized_value| super key, serialized_value }
21
+ end
22
+
23
+ def setex(key, ttl = 60, value)
24
+ _serialize(value) { |serialized_value| super key, ttl, serialized_value }
25
+ end
26
+
27
+ def get(key)
28
+ _deserialize super(key)
29
+ end
30
+
31
+ private
32
+ def _serialize(val)
33
+ if val.nil?
34
+ yield val
35
+ elsif val.class == String
36
+ yield val.to_json_raw
37
+ else
38
+ yield val.to_json
39
+ end
40
+ end
41
+ def _deserialize(val)
42
+ return if val.nil?
43
+ ret = (val.empty? ) ? val : JSON.parse(val, symbolize_names: true)
44
+ if ret.class == Hash && ret[:json_class] == "String"
45
+ ret[:raw].pack("c*").force_encoding('utf-8')
46
+ else
47
+ ret
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module RedisJsonSerializer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "redis_json_serializer/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "redis_json_serializer"
7
+ s.version = RedisJsonSerializer::VERSION
8
+ s.authors = ["Spring_MT"]
9
+ s.email = ["today.is.sky.blue.sky@gmail.com"]
10
+ s.homepage = "https://github.com/SpringMT/redis_json_serializer"
11
+ s.summary = %q{json serializer for redis}
12
+
13
+ s.rubyforge_project = "redis_json_serializer"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency 'redis', '~> 3.0'
21
+ s.add_dependency 'redis-namespace', '~> 1.2'
22
+
23
+ s.description = <<description
24
+ Store data by converting JSON to redis
25
+ description
26
+ end
@@ -0,0 +1,179 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ require File.dirname(__FILE__) + '/spec_helper'
5
+
6
+ require 'redis'
7
+ require 'redis-namespace'
8
+ require 'json'
9
+
10
+ options = {
11
+ host: "192.168.100.1",
12
+ port: 1234,
13
+ namespace: :test,
14
+ }
15
+ test_options = {
16
+ namespace: :test,
17
+ }
18
+
19
+ test_nil = nil
20
+ test_blank = ''
21
+ test_space = 'test test'
22
+ test_tab = "test test"
23
+ test_carriage_return = "test\n\rtest"
24
+ test_string = 'testてすと'
25
+ test_array = %w('test1てすと' 'test2 test2' 'test3\ntest3' 'test4\rtest4' 'test5\n\rtest5' 'test6 test6' '' nil)
26
+ test_hash = { test1: 'test1てすと', test2: 'test2 test2', test3: 'test3\ntest3', test4: 'test4\rtest4', test5: 'test5\n\rtest5', test6: 'test6 test6', test7: '', nil_test: nil }
27
+
28
+ r = Redis.new(host: "127.0.0.1", port: 6379)
29
+ test_redis = Redis::Namespace.new(:test, redis: r)
30
+
31
+ describe RedisJsonSerializer::Serializer do
32
+ describe :new do
33
+ context 'no params' do
34
+ subject { RedisJsonSerializer::Serializer.new }
35
+ it { subject.client.host.should be_eql("127.0.0.1") }
36
+ it { subject.client.port.should be_eql(6379) }
37
+ it { subject.namespace.should be_eql(:mock) }
38
+ end
39
+ context 'exixsts params' do
40
+ subject { RedisJsonSerializer::Serializer.new(options) }
41
+ it { subject.client.host.should be_eql("192.168.100.1") }
42
+ it { subject.client.port.should be_eql(1234) }
43
+ it { subject.namespace.should be_eql(:test) }
44
+ end
45
+ end
46
+
47
+ describe :setex do
48
+ before { @set_test_redis = RedisJsonSerializer::Serializer.new(test_options) }
49
+ context 'with a valid key' do
50
+ context 'nil' do
51
+ before { @set_test_redis.setex(:test_setex_nil, 1, test_nil) }
52
+ it { test_redis.get(:test_setex_nil).should be_eql("") }
53
+ end
54
+ context 'blank' do
55
+ before { @set_test_redis.setex(:test_setex_blank, 1, test_blank) }
56
+ it { test_redis.get(:test_setex_blank).should be_eql(test_blank.to_json_raw) }
57
+ end
58
+ context 'space' do
59
+ before { @set_test_redis.setex(:test_setex_space, 1, test_space) }
60
+ it { test_redis.get(:test_setex_space).should be_eql(test_space.to_json_raw) }
61
+ end
62
+ context 'tab' do
63
+ before { @set_test_redis.setex(:test_setex_tab, 1, test_tab) }
64
+ it { test_redis.get(:test_setex_tab).should be_eql(test_tab.to_json_raw) }
65
+ #it { test_redis.get(:test_set_tab).should be_eql("{\"json_class\":\"String\",\"raw\":[116,101,115,116,9,9,116,101,115,116]}") }
66
+ end
67
+ context 'carriage return' do
68
+ before { @set_test_redis.setex(:test_setex_carriage_return, 1, test_carriage_return) }
69
+ it { test_redis.get(:test_setex_carriage_return).should be_eql(test_carriage_return.to_json_raw) }
70
+ end
71
+ context 'String' do
72
+ before { @set_test_redis.setex(:test_setex_string, 1, test_string) }
73
+ it { test_redis.get(:test_setex_string).should be_eql(test_string.to_json_raw) }
74
+ end
75
+ context 'Array' do
76
+ before { @set_test_redis.setex(:test_setex_array, 1, test_array) }
77
+ it { test_redis.get(:test_setex_array).should be_eql(test_array.to_json) }
78
+ end
79
+ context 'Hash' do
80
+ before { @set_test_redis.setex(:test_setex_hash, 1, test_hash) }
81
+ it { test_redis.get(:test_setex_hash).should be_eql(test_hash.to_json) }
82
+ end
83
+ end
84
+ context 'ttl check' do
85
+ before { @set_test_redis.setex(:test_setex_check_ttl, 1, test_hash) }
86
+ it do
87
+ sleep 2
88
+ test_redis.get(:test_setex_hash).should be_nil
89
+ end
90
+ end
91
+ end
92
+
93
+ describe :set do
94
+ before { @set_test_redis = RedisJsonSerializer::Serializer.new(test_options) }
95
+ context 'with a valid key' do
96
+ context 'nil' do
97
+ before { @set_test_redis.set(:test_set_nil, test_nil) }
98
+ it { test_redis.get(:test_set_nil).should be_eql("") }
99
+ end
100
+ context 'blank' do
101
+ before { @set_test_redis.set(:test_set_blank, test_blank) }
102
+ it { test_redis.get(:test_set_blank).should be_eql(test_blank.to_json_raw) }
103
+ end
104
+ context 'space' do
105
+ before { @set_test_redis.set(:test_set_space, test_space) }
106
+ it { test_redis.get(:test_set_space).should be_eql(test_space.to_json_raw) }
107
+ end
108
+ context 'tab' do
109
+ before { @set_test_redis.set(:test_set_tab, test_tab) }
110
+ it { test_redis.get(:test_set_tab).should be_eql(test_tab.to_json_raw) }
111
+ #it { test_redis.get(:test_set_tab).should be_eql("{\"json_class\":\"String\",\"raw\":[116,101,115,116,9,9,116,101,115,116]}") }
112
+ end
113
+ context 'carriage return' do
114
+ before { @set_test_redis.set(:test_set_carriage_return, test_carriage_return) }
115
+ it { test_redis.get(:test_set_carriage_return).should be_eql(test_carriage_return.to_json_raw) }
116
+ end
117
+ context 'String' do
118
+ before { @set_test_redis.set(:test_set_string, test_string) }
119
+ it { test_redis.get(:test_set_string).should be_eql(test_string.to_json_raw) }
120
+ end
121
+ context 'Array' do
122
+ before { @set_test_redis.set(:test_set_array, test_array) }
123
+ it { test_redis.get(:test_set_array).should be_eql(test_array.to_json) }
124
+ end
125
+ context 'Hash' do
126
+ before { @set_test_redis.set(:test_set_hash, test_hash) }
127
+ it { test_redis.get(:test_set_hash).should be_eql(test_hash.to_json) }
128
+ end
129
+ end
130
+ end
131
+
132
+ describe :get do
133
+ before do
134
+ test_redis.set :test_get_nil, test_nil
135
+ test_redis.set :test_get_blank, test_blank.to_json_raw
136
+ test_redis.set :test_get_space, test_space.to_json_raw
137
+ test_redis.set :test_get_tab, test_tab.to_json_raw
138
+ test_redis.set :test_get_carriage_return, test_carriage_return.to_json_raw
139
+ test_redis.set :test_get_string, test_string.to_json_raw
140
+ test_redis.set :test_get_array, test_array.to_json
141
+ test_redis.set :test_get_hash, test_hash.to_json
142
+ end
143
+ context 'with a valid key' do
144
+ subject { RedisJsonSerializer::Serializer.new(test_options) }
145
+ context 'nil' do
146
+ # nil converts "" so retrun value check not nil but ''. However nil in Hash converts nil.
147
+ it { subject.get(:test_get_nil).should be_eql('') }
148
+ end
149
+ context 'blank' do
150
+ it { subject.get(:test_get_blank).should be_eql(test_blank) }
151
+ end
152
+ context 'space' do
153
+ it { subject.get(:test_get_space).should be_eql(test_space) }
154
+ end
155
+ context 'tab' do
156
+ it { subject.get(:test_get_tab).should be_eql(test_tab) }
157
+ end
158
+ context 'carriage return' do
159
+ it { subject.get(:test_get_carriage_return).should be_eql(test_carriage_return) }
160
+ end
161
+ context 'String' do
162
+ it { subject.get(:test_get_string).should be_eql(test_string) }
163
+ end
164
+ context 'Array' do
165
+ it { subject.get(:test_get_array).should be_eql(test_array) }
166
+ end
167
+ context 'Hash' do
168
+ it { subject.get(:test_get_hash).should be_eql(test_hash) }
169
+ end
170
+ end
171
+ context 'with an invalid key' do
172
+ subject { RedisJsonSerializer::Serializer.new(test_options) }
173
+ it { subject.get(:no_key).should be_nil }
174
+ end
175
+ end
176
+
177
+ end
178
+
179
+
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ Bundler.setup(:default, :test)
6
+ Bundler.require(:default, :test)
7
+
8
+ require 'rspec'
9
+
10
+ $TESTING=true
11
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
12
+ require 'redis_json_serializer'
13
+
14
+
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_json_serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Spring_MT
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: redis-namespace
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.2'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.2'
46
+ description: ! ' Store data by converting JSON to redis
47
+
48
+ '
49
+ email:
50
+ - today.is.sky.blue.sky@gmail.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - .travis.yml
57
+ - Gemfile
58
+ - MIT-LICENSE
59
+ - README.md
60
+ - Rakefile
61
+ - lib/redis_json_serializer.rb
62
+ - lib/redis_json_serializer/version.rb
63
+ - redis_json_serializer.gemspec
64
+ - spec/redis_json_serializer_spec.rb
65
+ - spec/spec_helper.rb
66
+ homepage: https://github.com/SpringMT/redis_json_serializer
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project: redis_json_serializer
86
+ rubygems_version: 1.8.24
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: json serializer for redis
90
+ test_files:
91
+ - spec/redis_json_serializer_spec.rb
92
+ - spec/spec_helper.rb