compressed-cookie 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README.md +39 -0
- data/Rakefile +1 -0
- data/compressed-cookie.gemspec +24 -0
- data/lib/compressed-cookie.rb +1 -0
- data/lib/compressed_cookie/version.rb +3 -0
- data/lib/compressed_cookie.rb +114 -0
- data/spec/compressed_cookie_spec.rb +129 -0
- data/spec/spec_helper.rb +2 -0
- metadata +70 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Jens Bissinger
|
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,39 @@
|
|
1
|
+
# CompressedCookie
|
2
|
+
|
3
|
+
This gem provides a wrapper around key-based cookie accessors.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
gem install compressed_cookie
|
8
|
+
|
9
|
+
## Examples
|
10
|
+
|
11
|
+
# define your cookie class
|
12
|
+
class MyCookie < CompressedCookie
|
13
|
+
compressor_keys :foo => 0,
|
14
|
+
:bar => 1
|
15
|
+
end
|
16
|
+
|
17
|
+
# use it to write to a raw cookie object
|
18
|
+
raw_cookie = []
|
19
|
+
MyCookie.write(raw_cookie) do |writer|
|
20
|
+
writer.foo = 'hello world'
|
21
|
+
end
|
22
|
+
puts raw_cookie
|
23
|
+
# => ['hello world', nil]
|
24
|
+
|
25
|
+
# use it to read from a raw cookie object
|
26
|
+
MyCookie.read(raw_cookie) do |reader|
|
27
|
+
puts reader.foo
|
28
|
+
# => 'hello world'
|
29
|
+
end
|
30
|
+
|
31
|
+
## Development
|
32
|
+
|
33
|
+
`bundle install`
|
34
|
+
|
35
|
+
`bundle exec rspec spec`
|
36
|
+
|
37
|
+
# License
|
38
|
+
|
39
|
+
Copyright 2011 Jens Bissinger. All rights reserved. [MIT-LICENSE](MIT-LICENSE)
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "compressed_cookie/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "compressed-cookie"
|
7
|
+
s.version = CompressedCookie::VERSION
|
8
|
+
s.authors = ["Jens Bissinger"]
|
9
|
+
s.email = ["mail@jens-bissinger.de"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Minimize your cookie footprint'}
|
12
|
+
s.description = %q{Provides basically just a wrapper class around key-based cookie accessors}
|
13
|
+
|
14
|
+
s.rubyforge_project = "compressed-cookie"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'compressed_cookie')
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "compressed_cookie", "version")
|
2
|
+
|
3
|
+
class CompressedCookie
|
4
|
+
class UndefinedCompressorKeyError < ArgumentError; end
|
5
|
+
|
6
|
+
### IMPARATIVE DECLARATIONS ###
|
7
|
+
def self.compressor_keys(hash = nil)
|
8
|
+
@compressor_keys ||= {}
|
9
|
+
@compressor_keys.merge!(hash) if hash
|
10
|
+
@compressor_keys
|
11
|
+
end
|
12
|
+
def self.part_of(clazz, key)
|
13
|
+
@parent_cookie_class = clazz
|
14
|
+
@parent_cookie_key = key
|
15
|
+
end
|
16
|
+
|
17
|
+
### CONSTRUCTOR ###
|
18
|
+
# default
|
19
|
+
# @param [ #[], #[]= ] cookie object (external) itself
|
20
|
+
def initialize(cookie, write_access = false)
|
21
|
+
@cookie = self.class.initialize_cookie_part(cookie)
|
22
|
+
self.class.define_readers
|
23
|
+
self.class.define_writers if write_access
|
24
|
+
end
|
25
|
+
# WRITE
|
26
|
+
def self.write(cookie, &block)
|
27
|
+
writer = self.new(cookie, true)
|
28
|
+
# return the block's return value
|
29
|
+
if block_given?
|
30
|
+
yield writer
|
31
|
+
# return the writer itself
|
32
|
+
else
|
33
|
+
writer
|
34
|
+
end
|
35
|
+
end
|
36
|
+
# READ
|
37
|
+
def self.read(cookie, &block)
|
38
|
+
reader = self.new(cookie)
|
39
|
+
# return the block's return value
|
40
|
+
if block_given?
|
41
|
+
yield reader
|
42
|
+
# return the reader itself
|
43
|
+
else
|
44
|
+
reader
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
### BULK OPERATIONS ###
|
49
|
+
# READ
|
50
|
+
def to_hash
|
51
|
+
self.class.compressor_keys.inject({}) do |result, pair|
|
52
|
+
name, key = pair
|
53
|
+
result[name] = self.send(name)
|
54
|
+
result
|
55
|
+
end
|
56
|
+
end
|
57
|
+
# WRITE
|
58
|
+
def update!(hash)
|
59
|
+
hash.each do |key, value|
|
60
|
+
method_name = "#{key}="
|
61
|
+
self.send(method_name, value) if self.respond_to?(method_name)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.initialize_cookie_part(_cookie)
|
66
|
+
cookie = _cookie
|
67
|
+
if parent?
|
68
|
+
cookie = @parent_cookie_class.write(cookie) do |writer|
|
69
|
+
# extract the cookie part from the parent
|
70
|
+
if existing_cookie_part = writer.send("#{@parent_cookie_key}")
|
71
|
+
existing_cookie_part
|
72
|
+
# if the cookie part is empty => then create it as an empty array
|
73
|
+
else
|
74
|
+
new_cookie_part = writer.send("#{@parent_cookie_key}=", Array.new)
|
75
|
+
new_cookie_part
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
cookie
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.parent?
|
83
|
+
if defined?(@parent_cookie_class) && defined?(@parent_cookie_key)
|
84
|
+
true
|
85
|
+
else
|
86
|
+
false
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.key(name)
|
91
|
+
if compressor_keys.has_key? name
|
92
|
+
compressor_keys[name]
|
93
|
+
else
|
94
|
+
raise UndefinedCompressorKeyError.new "#{self.class} has no compressor key=#{name}"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
def self.define_readers
|
101
|
+
compressor_keys.each do |method_name, key|
|
102
|
+
define_method("#{method_name}") do
|
103
|
+
@cookie[key]
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
def self.define_writers
|
108
|
+
compressor_keys.each do |method_name, key|
|
109
|
+
define_method("#{method_name}=") do |first_method_arg|
|
110
|
+
@cookie[key] = first_method_arg
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'CompressedCookie' do
|
4
|
+
|
5
|
+
describe 'class declaration api' do
|
6
|
+
it 'should respond to #compressor_keys' do
|
7
|
+
CompressedCookie.should respond_to(:compressor_keys)
|
8
|
+
end
|
9
|
+
it 'should respond to #part_of' do
|
10
|
+
CompressedCookie.should respond_to(:part_of)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'simple cookie' do
|
15
|
+
class SimpleCookieMock < CompressedCookie
|
16
|
+
compressor_keys :zero => 0,
|
17
|
+
:one => 1,
|
18
|
+
:two => 2
|
19
|
+
end
|
20
|
+
before :each do
|
21
|
+
@cookie_mock = [
|
22
|
+
'foo', # zero
|
23
|
+
'bar' # one
|
24
|
+
# two is nil
|
25
|
+
]
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'class methods' do
|
29
|
+
it 'should lookup keys' do
|
30
|
+
SimpleCookieMock.key(:zero).should == 0
|
31
|
+
SimpleCookieMock.key(:one).should == 1
|
32
|
+
SimpleCookieMock.key(:two).should == 2
|
33
|
+
end
|
34
|
+
it 'should raise UndefinedCompressorKeyError if lookup key is undefined' do
|
35
|
+
lambda do
|
36
|
+
SimpleCookieMock.key(:three)
|
37
|
+
end.should raise_error(CompressedCookie::UndefinedCompressorKeyError)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'read-only instance' do
|
42
|
+
before :each do
|
43
|
+
@mock = SimpleCookieMock.new(@cookie_mock)
|
44
|
+
end
|
45
|
+
it 'should respond only to read methods' do
|
46
|
+
@mock.should respond_to(:one)
|
47
|
+
@mock.should respond_to(:two)
|
48
|
+
@mock.should_not respond_to('one=')
|
49
|
+
@mock.should_not respond_to('two=')
|
50
|
+
end
|
51
|
+
it 'should read single cookies value' do
|
52
|
+
@mock.one.should == 'bar'
|
53
|
+
@mock.two.should == nil
|
54
|
+
end
|
55
|
+
it 'should read multiple values via #to_hash' do
|
56
|
+
@mock.to_hash.should == {:zero => 'foo', :one => 'bar', :two => nil}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe 'read/write instance' do
|
61
|
+
before :each do
|
62
|
+
@mock = SimpleCookieMock.new(@cookie_mock, true)
|
63
|
+
end
|
64
|
+
it 'should respond to read + write methods' do
|
65
|
+
@mock.should respond_to(:one)
|
66
|
+
@mock.should respond_to(:two)
|
67
|
+
@mock.should respond_to('one=')
|
68
|
+
@mock.should respond_to('two=')
|
69
|
+
end
|
70
|
+
it 'should write new cookies value' do
|
71
|
+
@mock.two = 'new-baz'
|
72
|
+
@mock.two.should == 'new-baz'
|
73
|
+
@cookie_mock.should == ['foo', 'bar', 'new-baz']
|
74
|
+
end
|
75
|
+
it 'should overwrite existing cookies value' do
|
76
|
+
@mock.one = 'new-bar'
|
77
|
+
@mock.one.should == 'new-bar'
|
78
|
+
@cookie_mock.should == ['foo', 'new-bar']
|
79
|
+
end
|
80
|
+
it 'should write multiple values via #update!' do
|
81
|
+
@mock.update!(:zero => 'new-foo', :one => 'new-bar', :two => 'new-baz')
|
82
|
+
@cookie_mock.should == ['new-foo', 'new-bar', 'new-baz']
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe 'nested cookie' do
|
88
|
+
class RootCookieMock < CompressedCookie
|
89
|
+
compressor_keys :zero => 0,
|
90
|
+
:child => 1
|
91
|
+
end
|
92
|
+
class ChildCookieMock < CompressedCookie
|
93
|
+
part_of RootCookieMock, :child
|
94
|
+
compressor_keys :zero => 0,
|
95
|
+
:one => 1
|
96
|
+
end
|
97
|
+
before :each do
|
98
|
+
@cookie_mock = [:foo, [:bar, :baz]]
|
99
|
+
end
|
100
|
+
describe "root" do
|
101
|
+
before :each do
|
102
|
+
@mock = RootCookieMock.new(@cookie_mock, true)
|
103
|
+
end
|
104
|
+
it 'should read the childs values at one' do
|
105
|
+
@mock.child.should == [:bar, :baz]
|
106
|
+
end
|
107
|
+
it 'should tell that it has NO parent' do
|
108
|
+
RootCookieMock.parent?.should == false
|
109
|
+
end
|
110
|
+
end
|
111
|
+
describe "child" do
|
112
|
+
before :each do
|
113
|
+
@mock = ChildCookieMock.new(@cookie_mock, true)
|
114
|
+
end
|
115
|
+
it 'should read its cookie values' do
|
116
|
+
@mock.zero.should == :bar
|
117
|
+
@mock.one.should == :baz
|
118
|
+
end
|
119
|
+
it 'should write its cookie values' do
|
120
|
+
@mock.zero = :written_foo
|
121
|
+
@mock.zero.should == :written_foo
|
122
|
+
@cookie_mock.should == [:foo, [:written_foo, :baz]]
|
123
|
+
end
|
124
|
+
it 'should tell that it has a parent' do
|
125
|
+
ChildCookieMock.parent?.should == true
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: compressed-cookie
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jens Bissinger
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-10 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70226960847640 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70226960847640
|
25
|
+
description: Provides basically just a wrapper class around key-based cookie accessors
|
26
|
+
email:
|
27
|
+
- mail@jens-bissinger.de
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- MIT-LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- compressed-cookie.gemspec
|
38
|
+
- lib/compressed-cookie.rb
|
39
|
+
- lib/compressed_cookie.rb
|
40
|
+
- lib/compressed_cookie/version.rb
|
41
|
+
- spec/compressed_cookie_spec.rb
|
42
|
+
- spec/spec_helper.rb
|
43
|
+
homepage: ''
|
44
|
+
licenses: []
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project: compressed-cookie
|
63
|
+
rubygems_version: 1.8.10
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Minimize your cookie footprint'
|
67
|
+
test_files:
|
68
|
+
- spec/compressed_cookie_spec.rb
|
69
|
+
- spec/spec_helper.rb
|
70
|
+
has_rdoc:
|