padrino-cookies 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ archive/*
3
+ doc/*
4
+ .yardoc
5
+ .bundle
6
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.yardopts ADDED
@@ -0,0 +1,9 @@
1
+ --no-save
2
+ --no-private
3
+ --title Padrino Cookies
4
+ --markup-provider redcarpet
5
+ --markup markdown
6
+ lib/**/*.rb
7
+ -
8
+ README.md
9
+ LICENSE
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2012 Benjamin Bloch
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ Overview
2
+ --------
3
+
4
+ Padrino Cookies is a plugin for the [Padrino](https://github.com/padrino/padrino-framework) web framework which adds support for [Rails](https://github.com/rails/rails) like cookie manipulation.
5
+
6
+ Setup & Installation
7
+ --------------------
8
+
9
+ Include it in your project's `Gemfile`:
10
+
11
+ ``` ruby
12
+ gem 'padrino-cookies'
13
+ ```
14
+
15
+ Modify your `app/app.rb` file to register the plugin:
16
+
17
+ ``` ruby
18
+ class ExampleApplication < Padrino::Application
19
+ register Padrino::Cookies
20
+ end
21
+ ```
22
+
23
+ Dependencies
24
+ ------------
25
+
26
+ * [Padrino-Core](https://github.com/padrino/padrino-framework)
27
+ * [Ruby](http://www.ruby-lang.org/en) >= 1.9.2
28
+
29
+ TODO
30
+ -----
31
+
32
+ * Additional documentation
33
+ * Signed cookies
34
+
35
+ Copyright
36
+ ---------
37
+
38
+ Copyright &copy; 2012 Benjamin Bloch (Cirex). See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
2
+ require 'padrino-cookies/version'
3
+
4
+ require 'rake'
5
+ require 'yard'
6
+ require 'rspec'
7
+ require 'rspec/core/rake_task'
8
+
9
+ RSpec::Core::RakeTask.new do |task|
10
+ task.pattern = 'spec/**/*_spec.rb'
11
+ end
12
+
13
+ YARD::Rake::YardocTask.new
14
+
15
+ task :build do
16
+ `gem build padrino-cookies.gemspec`
17
+ end
18
+
19
+ task :install => :build do
20
+ `gem install padrino-cookies-#{Padrino::Cookies::VERSION}.gem`
21
+ end
22
+
23
+ desc 'Releases the current version into the wild'
24
+ task :release => :build do
25
+ `git tag -a v#{Padrino::Cookies::VERSION} -m "Version #{Padrino::Cookies::VERSION}"`
26
+ `gem push padrino-cookies-#{Padrino::Cookies::VERSION}.gem`
27
+ `git push --tags`
28
+ end
29
+
30
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+ require 'padrino-core'
3
+ FileSet.glob_require('padrino-cookies/**/*.rb', __FILE__)
4
+
5
+ module Padrino
6
+ module Cookies
7
+ class << self
8
+ # @private
9
+ def registered(app)
10
+ app.helpers Helpers
11
+ end
12
+ end # self
13
+ end # Cookies
14
+ end # Padrino
@@ -0,0 +1,23 @@
1
+ # encoding: UTF-8
2
+ module Padrino
3
+ module Cookies
4
+ module Helpers
5
+ ###
6
+ # Returns the cookie storage object
7
+ #
8
+ # @return [Jar]
9
+ # ...Nom ....Nom ....Nom
10
+ #
11
+ # @example
12
+ # cookie[:remembrance] = '71ab53190d2f863b5f3b12381d2d5986512f8e15b34d439e6b66e3daf41b5e35'
13
+ # cookies.delete :remembrance
14
+ #
15
+ # since 0.1.0
16
+ # @api public
17
+ def cookies
18
+ @cookie_jar ||= Jar.new(self)
19
+ end
20
+ alias_method :cookie, :cookies
21
+ end # Helpers
22
+ end # Cookies
23
+ end # Padrino
@@ -0,0 +1,236 @@
1
+ # encoding: UTF-8
2
+ require 'active_support/core_ext/integer/time'
3
+ require 'active_support/core_ext/numeric/time'
4
+ require 'active_support/core_ext/numeric/bytes'
5
+ require 'active_support/core_ext/date/calculations'
6
+
7
+ module Padrino
8
+ module Cookies
9
+ class Jar
10
+ include Enumerable
11
+
12
+ # @private
13
+ def initialize(app)
14
+ @response = app.response
15
+ @request = app.request
16
+ @cookies = app.request.cookies
17
+
18
+ @options = {
19
+ path: '/',
20
+ httponly: true,
21
+ secure: @request.secure?
22
+ }
23
+ end
24
+
25
+ ###
26
+ # Returns the value of the specified cookie
27
+ #
28
+ # @return [String]
29
+ # Value of the cookie
30
+ #
31
+ # @example
32
+ # cookies[:remembrance]
33
+ # # => '71ab53190d2f863b5f3b12381d2d5986512f8e15b34d439e6b66e3daf41b5e35'
34
+ #
35
+ # @since 0.1.0
36
+ # @api public
37
+ def [](name)
38
+ @cookies[name.to_s]
39
+ end
40
+
41
+ ###
42
+ # Sets the specified cookie
43
+ #
44
+ # @overload []=(name, value)
45
+ # @param [Symbol] name
46
+ # The name of the cookie being set
47
+ # @param [String] value
48
+ # The value of the cookie being set
49
+ #
50
+ # @overload []=(name, options)
51
+ # @param [Symbol] name
52
+ # The name of the cookie being set
53
+ # @param [Hash] options
54
+ # The options to set along with this cookie
55
+ #
56
+ # @option options [String] :value
57
+ # The value of the cookie being set
58
+ # @option options [Time] :expires
59
+ # The time when this cookie expires
60
+ # @option options [Boolean] :httponly (true)
61
+ # Should the cookie be accessible to the HTTP protocol only
62
+ # @option options [Boolean] :secure
63
+ # Should the cookie only be sent over secure connections such as HTTPS
64
+ # @option options [String] :path ('/')
65
+ # The scope in which this cookie is accessible
66
+ # @option options [String] :domain
67
+ # The scope in which this cookie is accessible
68
+ #
69
+ # @example
70
+ # cookies[:remembrance] = '71ab53190d2f863b5f3b12381d2d5986512f8e15b34d439e6b66e3daf41b5e35'
71
+ #
72
+ # @since 0.1.0
73
+ # @api public
74
+ def []=(name, options)
75
+ unless options.is_a?(Hash)
76
+ options = { value: options }
77
+ end
78
+
79
+ @response.set_cookie(name, @options.merge(options))
80
+ @cookies[name.to_s] = options[:value]
81
+ end
82
+
83
+ ###
84
+ # Returns an array of cookies that have been set
85
+ #
86
+ # @return [Array<String>]
87
+ # Cookies set
88
+ #
89
+ # @example
90
+ # cookies.keys
91
+ # # => ['remembrance']
92
+ #
93
+ # @since 0.1.0
94
+ # @api public
95
+ def keys
96
+ @cookies.keys
97
+ end
98
+
99
+ ###
100
+ # Returns whether or not the specified cookie is set
101
+ #
102
+ # @return [Boolean]
103
+ # *true* if it is, *false* otherwise
104
+ #
105
+ # @example
106
+ # cookie.key?(:remembrance)
107
+ # # => true
108
+ #
109
+ # @since 0.1.0
110
+ # @api public
111
+ def key?(name)
112
+ @cookies.key?(name.to_s)
113
+ end
114
+ alias_method :has_key?, :key?
115
+ alias_method :include?, :key?
116
+
117
+ ###
118
+ # Deletes the specified cookie
119
+ #
120
+ # @return [String]
121
+ # Value of the deleted cookie
122
+ #
123
+ # @example
124
+ # cookies.delete :remembrance
125
+ #
126
+ # @since 0.1.0
127
+ # @api public
128
+ def delete(name)
129
+ @response.delete_cookie(name)
130
+ @cookies.delete(name.to_s)
131
+ end
132
+
133
+ ###
134
+ # Deletes all cookies that are currently set
135
+ #
136
+ # @example
137
+ # cookies.clear
138
+ #
139
+ # @since 0.1.0
140
+ # @api public
141
+ def clear
142
+ @cookies.each_key { |name| delete(name) }
143
+ end
144
+
145
+ ###
146
+ # Returns whether or not any cookies have been set
147
+ #
148
+ # @return [Boolean]
149
+ # *true* if cookies are set, *false* otherwise
150
+ #
151
+ # @example
152
+ # cookies.empty?
153
+ # # => true
154
+ #
155
+ # @since 0.1.0
156
+ # @api public
157
+ def empty?
158
+ @cookies.empty?
159
+ end
160
+
161
+ ###
162
+ # Returns the total amount of cookies that have been set
163
+ #
164
+ # @return [Integer]
165
+ # Total cookies set
166
+ #
167
+ # @example
168
+ # cookies.length
169
+ # # => 2
170
+ #
171
+ # @since 0.1.0
172
+ # @api public
173
+ def length
174
+ @cookies.length
175
+ end
176
+ alias_method :size, :length
177
+
178
+ ###
179
+ # Iterates through set cookies
180
+ #
181
+ # @example
182
+ # cookies.each do |key, value|
183
+ # # ...
184
+ # end
185
+ #
186
+ # @since 0.1.0
187
+ # @api public
188
+ def each(&block)
189
+ @cookies.each(&block)
190
+ end
191
+
192
+ # @since 0.1.0
193
+ # @api public
194
+ def to_hash
195
+ @cookies.dup
196
+ end
197
+
198
+ # @since 0.1.0
199
+ # @api public
200
+ def to_s
201
+ @cookies.to_s
202
+ end
203
+
204
+ ###
205
+ # Sets a permanent cookie
206
+ #
207
+ # @example
208
+ # cookies.permanent[:remembrance] = '71ab53190d2f863b5f3b12381d2d5986512f8e15b34d439e6b66e3daf41b5e35'
209
+ #
210
+ # @since 0.1.0
211
+ # @api public
212
+ def permanent
213
+ @permanent ||= PermanentJar.new(self)
214
+ end
215
+ end # Jar
216
+
217
+ class PermanentJar # @private
218
+ def initialize(parent_jar)
219
+ @parent_jar = parent_jar
220
+ end
221
+
222
+ def []=(key, options)
223
+ unless options.is_a?(Hash)
224
+ options = { value: options }
225
+ end
226
+
227
+ options[:expires] = 1.year.from_now
228
+ @parent_jar[key] = options
229
+ end
230
+
231
+ def method_missing(method, *args, &block)
232
+ @parent_jar.send(method, *args, &block)
233
+ end
234
+ end # PermanentJar
235
+ end # Cookies
236
+ end # Padrino
@@ -0,0 +1,6 @@
1
+ # encoding: UTF-8
2
+ module Padrino
3
+ module Cookies
4
+ VERSION = '0.1.0'
5
+ end
6
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'padrino-cookies/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'padrino-cookies'
7
+ s.version = Padrino::Cookies::VERSION
8
+ s.authors = ['Benjamin Bloch']
9
+ s.email = ['cirex@gamesol.org']
10
+ s.homepage = 'https://github.com/Cirex/padrino-cookies'
11
+ s.description = 'A plugin for the Padrino web framework which adds support for Rails like cookie manipulation'
12
+ s.summary = s.description
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ['lib']
18
+
19
+ s.add_dependency 'padrino-core'
20
+
21
+ s.add_development_dependency 'rspec', '>= 2.0.0'
22
+ s.add_development_dependency 'rspec-html-matchers'
23
+ s.add_development_dependency 'rack-test'
24
+ end
@@ -0,0 +1,178 @@
1
+ require_relative 'spec'
2
+
3
+ describe Padrino::Cookies do
4
+ let :cookies do
5
+ route('foo=bar', 'bar=foo') { cookies }
6
+ end
7
+
8
+ context :[] do
9
+ it 'can retrieve existing cookies' do
10
+ cookies['foo'].should == 'bar'
11
+ cookies['bar'].should == 'foo'
12
+ end
13
+
14
+ it 'should allow symbols to be used as keys' do
15
+ cookies[:foo].should == 'bar'
16
+ cookies['foo'].should == 'bar'
17
+ end
18
+
19
+ it 'should return nil when the cookie is not found' do
20
+ cookies[:nom_nom].should be_nil
21
+ end
22
+ end
23
+
24
+ context :[]= do
25
+ it 'should add the cookie to the jar' do
26
+ cookies['test'] = 'test'
27
+ cookies['test'].should == 'test'
28
+ end
29
+
30
+ it 'should allow symbols to be used as keys' do
31
+ cookies[:test] = 'test'
32
+ cookies[:test].should == 'test'
33
+ cookies['test'].should == 'test'
34
+ end
35
+
36
+ it 'should set the response headers when setting a cookie' do
37
+ result = route do
38
+ cookies['foo'] = 'bar'
39
+ response['Set-Cookie']
40
+ end
41
+ result.should == 'foo=bar; path=/; HttpOnly'
42
+ end
43
+
44
+ it 'should set the path to / by default' do
45
+ result = route do
46
+ cookies['foo'] = 'bar'
47
+ response['Set-Cookie']
48
+ end
49
+
50
+ result.should include('path=/')
51
+ end
52
+
53
+ it 'should set HttpOnly by default' do
54
+ result = route do
55
+ cookies['foo'] = 'bar'
56
+ response['Set-Cookie']
57
+ end
58
+
59
+ result.should include('HttpOnly')
60
+ end
61
+ end
62
+
63
+ context :delete do
64
+ it 'should remove the cookie from the jar' do
65
+ cookies['foo'].should == 'bar'
66
+ cookies.delete 'foo'
67
+ cookies['foo'].should be_nil
68
+ end
69
+
70
+ it 'should allow symbols to be used as keys' do
71
+ cookies.delete :foo
72
+ cookies['foo'].should be_nil
73
+ end
74
+
75
+ it 'should set the response headers when deleting a cookie' do
76
+ result = route('foo=bar') do
77
+ cookies.delete 'foo'
78
+ response['Set-Cookie']
79
+ end
80
+
81
+ result.should == 'foo=; expires=Thu, 01-Jan-1970 00:00:00 GMT'
82
+ end
83
+
84
+ it 'should return the cookie value when deleting a cookie' do
85
+ cookies.delete('foo').should == 'bar'
86
+ end
87
+ end
88
+
89
+ describe :clear do
90
+ it 'can delete all cookies that are set' do
91
+ cookies['foo'].should == 'bar'
92
+ cookies['bar'].should == 'foo'
93
+ cookies.clear
94
+ cookies['foo'].should be_nil
95
+ cookies['bar'].should be_nil
96
+ end
97
+ end
98
+
99
+ context :length do
100
+ it 'can tell you how many cookies are set' do
101
+ cookies.length.should == 2
102
+ cookies['baz'] = 'foo'
103
+ cookies.length.should == 3
104
+ end
105
+
106
+ it 'should return 0 when no cookies are set' do
107
+ cookies.length.should == 2
108
+ cookies.clear
109
+ cookies.length.should == 0
110
+ end
111
+ end
112
+
113
+ describe :keys do
114
+ it 'can give you a list of cookies that are set' do
115
+ cookies.keys.should == ['bar', 'foo']
116
+ end
117
+ end
118
+
119
+ context :key? do
120
+ it 'should return true when a cookie is set' do
121
+ cookies.key?('foo').should be_true
122
+ end
123
+
124
+ it 'should return false when a cookie is not set' do
125
+ cookies.key?('nom_nom').should be_false
126
+ end
127
+
128
+ it 'should allow symbols to be used as keys' do
129
+ cookies.key?(:foo).should be_true
130
+ end
131
+ end
132
+
133
+ context :empty? do
134
+ it 'should return true when no cookies are set' do
135
+ cookies.clear
136
+ cookies.empty?.should be_true
137
+ end
138
+
139
+ it 'should return false when cookies are set' do
140
+ cookies.empty?.should be_false
141
+ end
142
+ end
143
+
144
+ context :each do
145
+ it 'can iterate through cookies' do
146
+ result = []
147
+ cookies.each do |key, value|
148
+ result << [key, value]
149
+ end
150
+ result[0].should == ['bar', 'foo']
151
+ result[1].should == ['foo', 'bar']
152
+ end
153
+
154
+ it 'should allow enumeration' do
155
+ result = cookies.collect do |key, value|
156
+ [key, value]
157
+ end
158
+ result[0].should == ['bar', 'foo']
159
+ result[1].should == ['foo', 'bar']
160
+ end
161
+ end
162
+
163
+ context :permanent do
164
+ it 'should add cookies to the parent jar' do
165
+ cookies.permanent['baz'] = 'foo'
166
+ cookies['baz'].should == 'foo'
167
+ end
168
+
169
+ it 'should set a cookie that expires in the distant future' do
170
+ result = route do
171
+ cookies.permanent['baz'] = 'foo'
172
+ response['Set-Cookie']
173
+ end
174
+
175
+ result.should =~ /#{1.year.from_now.year}/
176
+ end
177
+ end
178
+ end
data/spec/spec.rb ADDED
@@ -0,0 +1,28 @@
1
+ PADRINO_ENV = 'test'
2
+
3
+ require 'rspec'
4
+ require 'rack/test'
5
+ require 'padrino-cookies'
6
+
7
+ module TestHelpers
8
+ def app
9
+ @app ||= Sinatra.new(Padrino::Application) do
10
+ register Padrino::Cookies
11
+ disable :logging
12
+ end
13
+ end
14
+
15
+ def route(*cookies, &block)
16
+ result = nil
17
+ set_cookie(cookies)
18
+ app.get('cookies') { result = instance_eval(&block) }
19
+ get 'cookies'
20
+ result
21
+ end
22
+ end
23
+
24
+ RSpec.configure do |configuration|
25
+ configuration.include TestHelpers
26
+ configuration.include Rack::Test::Methods
27
+ end
28
+
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: padrino-cookies
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Benjamin Bloch
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: padrino-core
16
+ requirement: &15464784 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *15464784
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &15464232 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *15464232
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec-html-matchers
38
+ requirement: &15463920 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *15463920
47
+ - !ruby/object:Gem::Dependency
48
+ name: rack-test
49
+ requirement: &15463500 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *15463500
58
+ description: A plugin for the Padrino web framework which adds support for Rails like
59
+ cookie manipulation
60
+ email:
61
+ - cirex@gamesol.org
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - .rspec
68
+ - .yardopts
69
+ - Gemfile
70
+ - LICENSE
71
+ - README.md
72
+ - Rakefile
73
+ - lib/padrino-cookies.rb
74
+ - lib/padrino-cookies/helpers.rb
75
+ - lib/padrino-cookies/jar.rb
76
+ - lib/padrino-cookies/version.rb
77
+ - padrino-cookies.gemspec
78
+ - spec/cookies_spec.rb
79
+ - spec/spec.rb
80
+ homepage: https://github.com/Cirex/padrino-cookies
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.15
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: A plugin for the Padrino web framework which adds support for Rails like
104
+ cookie manipulation
105
+ test_files: []
106
+ has_rdoc: