junkfood 0.1.0 → 0.2.0
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.
- data/Gemfile +1 -0
- data/Gemfile.lock +38 -32
- data/README.md +43 -0
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/junkfood.gemspec +16 -4
- data/lib/junkfood/rack/error.rb +92 -0
- data/lib/junkfood/rack/sessions.rb +60 -0
- data/lib/junkfood/rack.rb +26 -0
- data/lib/junkfood.rb +1 -0
- data/spec/junkfood/rack/error_spec.rb +66 -0
- data/spec/junkfood/rack/sessions_spec.rb +20 -0
- metadata +57 -25
- data/.gitignore +0 -6
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
|
@@ -7,59 +7,64 @@ GEM
|
|
|
7
7
|
RubyInline (3.8.6)
|
|
8
8
|
ZenTest (~> 4.3)
|
|
9
9
|
ZenTest (4.4.0)
|
|
10
|
-
activemodel (3.0.
|
|
11
|
-
activesupport (= 3.0.
|
|
10
|
+
activemodel (3.0.2)
|
|
11
|
+
activesupport (= 3.0.2)
|
|
12
12
|
builder (~> 2.1.2)
|
|
13
13
|
i18n (~> 0.4.1)
|
|
14
|
-
activesupport (3.0.
|
|
14
|
+
activesupport (3.0.2)
|
|
15
15
|
bluecloth (2.0.9)
|
|
16
|
-
bson (1.
|
|
16
|
+
bson (1.1.2)
|
|
17
17
|
builder (2.1.2)
|
|
18
|
-
diff (0.3.6)
|
|
19
18
|
diff-lcs (1.1.2)
|
|
19
|
+
file-tail (1.0.5)
|
|
20
|
+
spruz (>= 0.1.0)
|
|
20
21
|
git (1.2.5)
|
|
21
|
-
i18n (0.4.
|
|
22
|
-
jeweler (1.5.
|
|
22
|
+
i18n (0.4.2)
|
|
23
|
+
jeweler (1.5.1)
|
|
23
24
|
bundler (~> 1.0.0)
|
|
24
25
|
git (>= 1.2.5)
|
|
25
26
|
rake
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
mongo (
|
|
27
|
+
json (1.4.6)
|
|
28
|
+
mongo (1.1.2)
|
|
29
|
+
bson (>= 1.1.1)
|
|
30
|
+
mongoid (2.0.0.beta.20)
|
|
31
|
+
activemodel (~> 3.0)
|
|
32
|
+
mongo (~> 1.1)
|
|
32
33
|
tzinfo (~> 0.3.22)
|
|
33
34
|
will_paginate (~> 3.0.pre)
|
|
34
|
-
predicated (0.2.
|
|
35
|
+
predicated (0.2.2)
|
|
35
36
|
rake (0.8.7)
|
|
36
37
|
rcov (0.9.9)
|
|
37
|
-
rspec (2.
|
|
38
|
-
rspec-core (
|
|
39
|
-
rspec-expectations (
|
|
40
|
-
rspec-mocks (
|
|
41
|
-
rspec-core (2.
|
|
42
|
-
rspec-expectations (2.
|
|
43
|
-
diff-lcs (
|
|
44
|
-
rspec-mocks (2.
|
|
45
|
-
rspec-core (= 2.0.0.beta.22)
|
|
46
|
-
rspec-expectations (= 2.0.0.beta.22)
|
|
38
|
+
rspec (2.1.0)
|
|
39
|
+
rspec-core (~> 2.1.0)
|
|
40
|
+
rspec-expectations (~> 2.1.0)
|
|
41
|
+
rspec-mocks (~> 2.1.0)
|
|
42
|
+
rspec-core (2.1.0)
|
|
43
|
+
rspec-expectations (2.1.0)
|
|
44
|
+
diff-lcs (~> 1.1.2)
|
|
45
|
+
rspec-mocks (2.1.0)
|
|
47
46
|
ruby2ruby (1.2.5)
|
|
48
47
|
ruby_parser (~> 2.0)
|
|
49
48
|
sexp_processor (~> 3.0)
|
|
50
49
|
ruby_parser (2.0.5)
|
|
51
50
|
sexp_processor (~> 3.0)
|
|
52
51
|
sexp_processor (3.0.5)
|
|
52
|
+
sourcify (0.4.0)
|
|
53
|
+
ruby2ruby (>= 1.2.5)
|
|
54
|
+
sexp_processor (>= 3.0.5)
|
|
55
|
+
spruz (0.2.2)
|
|
53
56
|
tzinfo (0.3.23)
|
|
54
57
|
will_paginate (3.0.pre2)
|
|
55
|
-
wrong (0.
|
|
56
|
-
ParseTree (
|
|
57
|
-
diff (
|
|
58
|
-
|
|
59
|
-
predicated (>= 0.2.
|
|
60
|
-
ruby2ruby (
|
|
61
|
-
ruby_parser (
|
|
62
|
-
|
|
58
|
+
wrong (0.4.5)
|
|
59
|
+
ParseTree (~> 3.0)
|
|
60
|
+
diff-lcs (~> 1.1.2)
|
|
61
|
+
file-tail (~> 1.0)
|
|
62
|
+
predicated (>= 0.2.2)
|
|
63
|
+
ruby2ruby (~> 1.2)
|
|
64
|
+
ruby_parser (~> 2.0.4)
|
|
65
|
+
sexp_processor (~> 3.0)
|
|
66
|
+
sourcify (>= 0.3.0)
|
|
67
|
+
yard (0.6.2)
|
|
63
68
|
|
|
64
69
|
PLATFORMS
|
|
65
70
|
ruby
|
|
@@ -69,6 +74,7 @@ DEPENDENCIES
|
|
|
69
74
|
bluecloth
|
|
70
75
|
bundler (~> 1.0.0)
|
|
71
76
|
jeweler (~> 1.5.0.pre3)
|
|
77
|
+
json
|
|
72
78
|
mongoid (~> 2.0.0.beta.18)
|
|
73
79
|
rcov
|
|
74
80
|
rspec (>= 2.0.0.beta.22)
|
data/README.md
CHANGED
|
@@ -15,6 +15,7 @@ Components
|
|
|
15
15
|
* CEB - "Command-Event Busing" for Command-Query Responsibility Separation
|
|
16
16
|
* OneTime - HMAC One Time Passwords
|
|
17
17
|
* PaperclipStringIO
|
|
18
|
+
* Rack - Generic middleware for rack applications.
|
|
18
19
|
* Settings
|
|
19
20
|
|
|
20
21
|
Adler32
|
|
@@ -40,6 +41,10 @@ Example:
|
|
|
40
41
|
digest = adler32.update 'Wikipedia' #=> 300286872
|
|
41
42
|
adler32.digest #=> 300286872
|
|
42
43
|
|
|
44
|
+
Requires:
|
|
45
|
+
|
|
46
|
+
* zlib - unless using the pure implementation
|
|
47
|
+
|
|
43
48
|
Assert
|
|
44
49
|
======
|
|
45
50
|
|
|
@@ -99,6 +104,9 @@ String Encodings internally.
|
|
|
99
104
|
:output => open('out.bin', 'w'))
|
|
100
105
|
io.close
|
|
101
106
|
|
|
107
|
+
Requires:
|
|
108
|
+
|
|
109
|
+
* stringio
|
|
102
110
|
|
|
103
111
|
Command Event Busing (CEB)
|
|
104
112
|
==========================
|
|
@@ -251,6 +259,12 @@ CEB Example
|
|
|
251
259
|
end
|
|
252
260
|
end
|
|
253
261
|
|
|
262
|
+
CEB Requirements
|
|
263
|
+
----------------
|
|
264
|
+
* activesupport
|
|
265
|
+
* delayed_job - when using the delayed job executor.
|
|
266
|
+
* mongoid - when using the BaseCommand and BaseEvent classes.
|
|
267
|
+
|
|
254
268
|
OneTime - HMAC One Time Passwords
|
|
255
269
|
=================================
|
|
256
270
|
|
|
@@ -300,6 +314,11 @@ Example:
|
|
|
300
314
|
puts one_time.otp! #=> 37359152
|
|
301
315
|
puts one_time.otp! #=> 26969429
|
|
302
316
|
|
|
317
|
+
Requires:
|
|
318
|
+
|
|
319
|
+
* openssl
|
|
320
|
+
* wrong
|
|
321
|
+
|
|
303
322
|
PaperclipStringIO
|
|
304
323
|
=================
|
|
305
324
|
|
|
@@ -325,6 +344,26 @@ Example:
|
|
|
325
344
|
:content_type => 'application/pdf'))
|
|
326
345
|
end
|
|
327
346
|
|
|
347
|
+
Requires:
|
|
348
|
+
|
|
349
|
+
* stringio
|
|
350
|
+
|
|
351
|
+
Rack
|
|
352
|
+
====
|
|
353
|
+
|
|
354
|
+
The Rack namespace provides simple generic utilities or middleware
|
|
355
|
+
that may be helpful to applications.
|
|
356
|
+
|
|
357
|
+
Visit the yardocs for examples and use cases.
|
|
358
|
+
|
|
359
|
+
The Middleware:
|
|
360
|
+
|
|
361
|
+
* Junkfood::Rack::ErrorHandler
|
|
362
|
+
* Junkfood::Rack::TransientSession
|
|
363
|
+
|
|
364
|
+
Requires:
|
|
365
|
+
|
|
366
|
+
* json
|
|
328
367
|
|
|
329
368
|
Settings
|
|
330
369
|
========
|
|
@@ -332,6 +371,10 @@ Settings
|
|
|
332
371
|
A singleton to allow all parts of a Rails application use the same set
|
|
333
372
|
of settings, loaded from the same settings yaml file.
|
|
334
373
|
|
|
374
|
+
Requires:
|
|
375
|
+
|
|
376
|
+
* rails
|
|
377
|
+
|
|
335
378
|
Testing
|
|
336
379
|
=======
|
|
337
380
|
This library uses [Bundler](http://gembundler.com/) for development and testing.
|
data/Rakefile
CHANGED
|
@@ -29,6 +29,7 @@ Jeweler::Tasks.new do |gem|
|
|
|
29
29
|
gem.add_development_dependency "rcov", ">= 0"
|
|
30
30
|
|
|
31
31
|
gem.add_runtime_dependency 'activesupport'
|
|
32
|
+
gem.add_runtime_dependency 'json'
|
|
32
33
|
gem.add_runtime_dependency 'mongoid', '~> 2.0.0.beta.18'
|
|
33
34
|
gem.add_runtime_dependency 'wrong'
|
|
34
35
|
end
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.
|
|
1
|
+
0.2.0
|
data/junkfood.gemspec
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
# Generated by jeweler
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
|
5
5
|
|
|
6
6
|
Gem::Specification.new do |s|
|
|
7
7
|
s.name = %q{junkfood}
|
|
8
|
-
s.version = "0.
|
|
8
|
+
s.version = "0.2.0"
|
|
9
9
|
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
11
|
s.authors = ["Benjamin Yu"]
|
|
12
|
-
s.date = %q{2010-
|
|
12
|
+
s.date = %q{2010-11-15}
|
|
13
13
|
s.description = %q{My mesh of an all-in-one library for disjoint code.}
|
|
14
14
|
s.email = %q{benjaminlyu@gmail.com}
|
|
15
15
|
s.extra_rdoc_files = [
|
|
@@ -19,7 +19,6 @@ Gem::Specification.new do |s|
|
|
|
19
19
|
]
|
|
20
20
|
s.files = [
|
|
21
21
|
".document",
|
|
22
|
-
".gitignore",
|
|
23
22
|
"Gemfile",
|
|
24
23
|
"Gemfile.lock",
|
|
25
24
|
"LICENSE",
|
|
@@ -43,6 +42,9 @@ Gem::Specification.new do |s|
|
|
|
43
42
|
"lib/junkfood/ceb/executors/event_executor.rb",
|
|
44
43
|
"lib/junkfood/one_time.rb",
|
|
45
44
|
"lib/junkfood/paperclip_string_io.rb",
|
|
45
|
+
"lib/junkfood/rack.rb",
|
|
46
|
+
"lib/junkfood/rack/error.rb",
|
|
47
|
+
"lib/junkfood/rack/sessions.rb",
|
|
46
48
|
"lib/junkfood/settings.rb",
|
|
47
49
|
"spec/.rspec",
|
|
48
50
|
"spec/junkfood/adler32_pure_spec.rb",
|
|
@@ -57,6 +59,8 @@ Gem::Specification.new do |s|
|
|
|
57
59
|
"spec/junkfood/ceb/executors/event_executor_spec.rb",
|
|
58
60
|
"spec/junkfood/one_time_spec.rb",
|
|
59
61
|
"spec/junkfood/paperclip_string_io_spec.rb",
|
|
62
|
+
"spec/junkfood/rack/error_spec.rb",
|
|
63
|
+
"spec/junkfood/rack/sessions_spec.rb",
|
|
60
64
|
"spec/junkfood/settings_spec.rb",
|
|
61
65
|
"spec/junkfood_spec.rb",
|
|
62
66
|
"spec/spec_helper.rb"
|
|
@@ -78,6 +82,8 @@ Gem::Specification.new do |s|
|
|
|
78
82
|
"spec/junkfood/ceb/executors/event_executor_spec.rb",
|
|
79
83
|
"spec/junkfood/one_time_spec.rb",
|
|
80
84
|
"spec/junkfood/paperclip_string_io_spec.rb",
|
|
85
|
+
"spec/junkfood/rack/error_spec.rb",
|
|
86
|
+
"spec/junkfood/rack/sessions_spec.rb",
|
|
81
87
|
"spec/junkfood/settings_spec.rb",
|
|
82
88
|
"spec/junkfood_spec.rb",
|
|
83
89
|
"spec/spec_helper.rb"
|
|
@@ -95,6 +101,7 @@ Gem::Specification.new do |s|
|
|
|
95
101
|
s.add_development_dependency(%q<jeweler>, ["~> 1.5.0.pre3"])
|
|
96
102
|
s.add_development_dependency(%q<rcov>, [">= 0"])
|
|
97
103
|
s.add_development_dependency(%q<activesupport>, [">= 0"])
|
|
104
|
+
s.add_development_dependency(%q<json>, [">= 0"])
|
|
98
105
|
s.add_development_dependency(%q<mongoid>, ["~> 2.0.0.beta.18"])
|
|
99
106
|
s.add_development_dependency(%q<wrong>, [">= 0"])
|
|
100
107
|
s.add_development_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
|
|
@@ -103,6 +110,7 @@ Gem::Specification.new do |s|
|
|
|
103
110
|
s.add_development_dependency(%q<jeweler>, ["~> 1.5.0.pre3"])
|
|
104
111
|
s.add_development_dependency(%q<rcov>, [">= 0"])
|
|
105
112
|
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
|
113
|
+
s.add_runtime_dependency(%q<json>, [">= 0"])
|
|
106
114
|
s.add_runtime_dependency(%q<mongoid>, ["~> 2.0.0.beta.18"])
|
|
107
115
|
s.add_runtime_dependency(%q<wrong>, [">= 0"])
|
|
108
116
|
else
|
|
@@ -113,6 +121,7 @@ Gem::Specification.new do |s|
|
|
|
113
121
|
s.add_dependency(%q<jeweler>, ["~> 1.5.0.pre3"])
|
|
114
122
|
s.add_dependency(%q<rcov>, [">= 0"])
|
|
115
123
|
s.add_dependency(%q<activesupport>, [">= 0"])
|
|
124
|
+
s.add_dependency(%q<json>, [">= 0"])
|
|
116
125
|
s.add_dependency(%q<mongoid>, ["~> 2.0.0.beta.18"])
|
|
117
126
|
s.add_dependency(%q<wrong>, [">= 0"])
|
|
118
127
|
s.add_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
|
|
@@ -121,6 +130,7 @@ Gem::Specification.new do |s|
|
|
|
121
130
|
s.add_dependency(%q<jeweler>, ["~> 1.5.0.pre3"])
|
|
122
131
|
s.add_dependency(%q<rcov>, [">= 0"])
|
|
123
132
|
s.add_dependency(%q<activesupport>, [">= 0"])
|
|
133
|
+
s.add_dependency(%q<json>, [">= 0"])
|
|
124
134
|
s.add_dependency(%q<mongoid>, ["~> 2.0.0.beta.18"])
|
|
125
135
|
s.add_dependency(%q<wrong>, [">= 0"])
|
|
126
136
|
end
|
|
@@ -132,6 +142,7 @@ Gem::Specification.new do |s|
|
|
|
132
142
|
s.add_dependency(%q<jeweler>, ["~> 1.5.0.pre3"])
|
|
133
143
|
s.add_dependency(%q<rcov>, [">= 0"])
|
|
134
144
|
s.add_dependency(%q<activesupport>, [">= 0"])
|
|
145
|
+
s.add_dependency(%q<json>, [">= 0"])
|
|
135
146
|
s.add_dependency(%q<mongoid>, ["~> 2.0.0.beta.18"])
|
|
136
147
|
s.add_dependency(%q<wrong>, [">= 0"])
|
|
137
148
|
s.add_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
|
|
@@ -140,6 +151,7 @@ Gem::Specification.new do |s|
|
|
|
140
151
|
s.add_dependency(%q<jeweler>, ["~> 1.5.0.pre3"])
|
|
141
152
|
s.add_dependency(%q<rcov>, [">= 0"])
|
|
142
153
|
s.add_dependency(%q<activesupport>, [">= 0"])
|
|
154
|
+
s.add_dependency(%q<json>, [">= 0"])
|
|
143
155
|
s.add_dependency(%q<mongoid>, ["~> 2.0.0.beta.18"])
|
|
144
156
|
s.add_dependency(%q<wrong>, [">= 0"])
|
|
145
157
|
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
# Copyright 2010 Benjamin Yu <http://benjaminyu.org/>
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
|
|
16
|
+
require 'json'
|
|
17
|
+
|
|
18
|
+
module Junkfood
|
|
19
|
+
module Rack
|
|
20
|
+
|
|
21
|
+
##
|
|
22
|
+
# Rack Middleware to catch exceptions, then transform the exceptions
|
|
23
|
+
# into serialized JSON objects to be returned to the caller.
|
|
24
|
+
#
|
|
25
|
+
# If this middleware catches an Exception, a rack request would be
|
|
26
|
+
# returned with an HTTP status code, a json mime-type in the headers,
|
|
27
|
+
# and JSON string as the response body.
|
|
28
|
+
#
|
|
29
|
+
# The HTTP status code will be 500 by default.
|
|
30
|
+
#
|
|
31
|
+
# The JSON object returned would have 'type', 'code' and 'message'
|
|
32
|
+
# fields.
|
|
33
|
+
# * 'type' will always be 'Error'.
|
|
34
|
+
# * 'code' will be the class name of the caught exception, or
|
|
35
|
+
# a customized value.
|
|
36
|
+
# * 'message' will be the message attribute of the caught exception, or
|
|
37
|
+
# a specifically set customized message.
|
|
38
|
+
#
|
|
39
|
+
# The developer can customize the HTTP status code, error code and
|
|
40
|
+
# error message via the constructor. Individually, or all at once.
|
|
41
|
+
#
|
|
42
|
+
# For example:
|
|
43
|
+
# error_map = {
|
|
44
|
+
# 'UnauthorizedException' => {
|
|
45
|
+
# 'status_code' => 401,
|
|
46
|
+
# 'code' => 'Unauthorized',
|
|
47
|
+
# 'message' => 'The request did not have the required authorization'
|
|
48
|
+
# }
|
|
49
|
+
# }
|
|
50
|
+
# use Junkfood::Rack::ErrorHandler, error_map
|
|
51
|
+
#
|
|
52
|
+
# When the 'UnauthorizedException' error is caught, the middleware
|
|
53
|
+
# will return a rack response with a 401 HTTP status code, a code with
|
|
54
|
+
# 'Unauthorized', and the given custom error message.
|
|
55
|
+
#
|
|
56
|
+
class ErrorHandler
|
|
57
|
+
##
|
|
58
|
+
# The default HTTP status code for caught errors.
|
|
59
|
+
DEFAULT_STATUS_CODE = 500
|
|
60
|
+
|
|
61
|
+
##
|
|
62
|
+
# @param app the rest of the rack stack
|
|
63
|
+
# @param mapping the mapping of Exceptions to Error codes and messages.
|
|
64
|
+
#
|
|
65
|
+
def initialize(app, mapping={})
|
|
66
|
+
@app = app
|
|
67
|
+
@mapping = mapping
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
##
|
|
71
|
+
# @param env the rack environment.
|
|
72
|
+
# @return a received rack response, or a generated JSON error
|
|
73
|
+
# in the response body with a custom status code.
|
|
74
|
+
#
|
|
75
|
+
def call(env)
|
|
76
|
+
return @app.call(env)
|
|
77
|
+
rescue Exception => e
|
|
78
|
+
map = @mapping[e.class.name] || {}
|
|
79
|
+
error = {
|
|
80
|
+
'type' => 'Error',
|
|
81
|
+
'code' => map['code'] || e.class.name,
|
|
82
|
+
'message' => map['message'] || e.message
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return [
|
|
86
|
+
map['status_code'] ? map['status_code'].to_i : DEFAULT_STATUS_CODE,
|
|
87
|
+
{ 'Content-Type' => 'application/json' },
|
|
88
|
+
[error.to_json]]
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
# Copyright 2010 Benjamin Yu <http://benjaminyu.org/>
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
|
|
16
|
+
module Junkfood
|
|
17
|
+
module Rack
|
|
18
|
+
|
|
19
|
+
##
|
|
20
|
+
# Rack Middleware that sets the 'rack.session' environment variable
|
|
21
|
+
# with a new Hash object.
|
|
22
|
+
#
|
|
23
|
+
# This is specifically useful for when an application, or other middleware,
|
|
24
|
+
# assumes that a rack session is set yet the developer doesn't want
|
|
25
|
+
# to save an actual Cookie or Database session.
|
|
26
|
+
#
|
|
27
|
+
# For example, one may want to use API tokens for authentication
|
|
28
|
+
# in a rack application protected by `warden`. Each request requires
|
|
29
|
+
# the API token for each call and we don't want the authenticated user
|
|
30
|
+
# to be saved in a Cookie session.
|
|
31
|
+
#
|
|
32
|
+
# Simply:
|
|
33
|
+
#
|
|
34
|
+
# use Junkfood::Rack::TransientSession
|
|
35
|
+
# use Warden::Manager do |manager|
|
|
36
|
+
# manager.default_strategies :web_api_access_token
|
|
37
|
+
# manager.failure_app = Junkfood::WebApi::UnauthenticatedHandler.new
|
|
38
|
+
# end
|
|
39
|
+
# use Junkfood::WebApi::AccessTokenAuthentication
|
|
40
|
+
#
|
|
41
|
+
class TransientSession
|
|
42
|
+
|
|
43
|
+
##
|
|
44
|
+
# @param app the rest of the Rack stack.
|
|
45
|
+
#
|
|
46
|
+
def initialize(app)
|
|
47
|
+
@app = app
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
##
|
|
51
|
+
# @param env the Rack environment.
|
|
52
|
+
# @return a Rack response from the application.
|
|
53
|
+
#
|
|
54
|
+
def call(env)
|
|
55
|
+
env['rack.session'] = {}
|
|
56
|
+
@app.call(env)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
# Copyright 2010 Benjamin Yu <http://benjaminyu.org/>
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
|
|
16
|
+
module Junkfood
|
|
17
|
+
|
|
18
|
+
##
|
|
19
|
+
# The Junkfood::Rack module is the namespace for generic Rack
|
|
20
|
+
# utilities and middleware.
|
|
21
|
+
module Rack
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
require 'junkfood/rack/error'
|
|
26
|
+
require 'junkfood/rack/sessions'
|
data/lib/junkfood.rb
CHANGED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
|
2
|
+
|
|
3
|
+
describe 'Junkfood::Rack' do
|
|
4
|
+
describe 'ErrorHandler' do
|
|
5
|
+
|
|
6
|
+
it 'should catch Exceptions and return a standard JSON error message' do
|
|
7
|
+
# Set up expected return values.
|
|
8
|
+
expected_headers = {
|
|
9
|
+
'Content-Type' => 'application/json'
|
|
10
|
+
}
|
|
11
|
+
expected_hash_body = {
|
|
12
|
+
'type' => 'Error',
|
|
13
|
+
'code' => 'RuntimeError',
|
|
14
|
+
'message' => 'My error message'
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
# Initialize the class under test.
|
|
18
|
+
handler = Junkfood::Rack::ErrorHandler.new Proc.new {
|
|
19
|
+
raise 'My error message'
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
# Execute the call to the middleware.
|
|
23
|
+
env = {}
|
|
24
|
+
status_code, headers, body = handler.call(env)
|
|
25
|
+
|
|
26
|
+
# Check the return values
|
|
27
|
+
status_code.should eql(500)
|
|
28
|
+
headers.should eql(expected_headers)
|
|
29
|
+
body_hash = JSON.parse body.join
|
|
30
|
+
body_hash.should eql(expected_hash_body)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it 'should catch Exceptions and return a custom JSON error message' do
|
|
34
|
+
# Set up expected return values.
|
|
35
|
+
expected_headers = {
|
|
36
|
+
'Content-Type' => 'application/json'
|
|
37
|
+
}
|
|
38
|
+
expected_hash_body = {
|
|
39
|
+
'type' => 'Error',
|
|
40
|
+
'code' => 'SomeCustomErrorCode',
|
|
41
|
+
'message' => 'My CUSTOM error message'
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
# Initialize the class under test.
|
|
45
|
+
handler = Junkfood::Rack::ErrorHandler.new(Proc.new {
|
|
46
|
+
raise 'My error message'
|
|
47
|
+
}, {
|
|
48
|
+
'RuntimeError' => {
|
|
49
|
+
'code' => 'SomeCustomErrorCode',
|
|
50
|
+
'message' => 'My CUSTOM error message',
|
|
51
|
+
'status_code' => 400
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
# Execute the call to the middleware.
|
|
56
|
+
env = {}
|
|
57
|
+
status_code, headers, body = handler.call(env)
|
|
58
|
+
|
|
59
|
+
# Check the return values
|
|
60
|
+
status_code.should eql(400)
|
|
61
|
+
headers.should eql(expected_headers)
|
|
62
|
+
body_hash = JSON.parse body.join
|
|
63
|
+
body_hash.should eql(expected_hash_body)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
|
2
|
+
|
|
3
|
+
describe 'Junkfood::Rack' do
|
|
4
|
+
describe 'TransientSession' do
|
|
5
|
+
|
|
6
|
+
it 'should set the rack.session env parameter with an empty hash' do
|
|
7
|
+
# We manually create a rack application with the TransientSession
|
|
8
|
+
# acting as the only middleware. The application itself is a
|
|
9
|
+
# proc object that just checks that the passed rack environment
|
|
10
|
+
# is a hash with the rack.session field set to an empty hash.
|
|
11
|
+
# The test is checked when the app is called with an empty hash
|
|
12
|
+
# that is to be transformed by the TransientSession middleware.
|
|
13
|
+
app = Junkfood::Rack::TransientSession.new Proc.new { |env|
|
|
14
|
+
env.should eql 'rack.session' => {}
|
|
15
|
+
[200, {}, []]
|
|
16
|
+
}
|
|
17
|
+
app.call({})
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
metadata
CHANGED
|
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
|
4
4
|
prerelease: false
|
|
5
5
|
segments:
|
|
6
6
|
- 0
|
|
7
|
-
-
|
|
7
|
+
- 2
|
|
8
8
|
- 0
|
|
9
|
-
version: 0.
|
|
9
|
+
version: 0.2.0
|
|
10
10
|
platform: ruby
|
|
11
11
|
authors:
|
|
12
12
|
- Benjamin Yu
|
|
@@ -14,7 +14,7 @@ autorequire:
|
|
|
14
14
|
bindir: bin
|
|
15
15
|
cert_chain: []
|
|
16
16
|
|
|
17
|
-
date: 2010-
|
|
17
|
+
date: 2010-11-15 00:00:00 -08:00
|
|
18
18
|
default_executable:
|
|
19
19
|
dependencies:
|
|
20
20
|
- !ruby/object:Gem::Dependency
|
|
@@ -120,8 +120,21 @@ dependencies:
|
|
|
120
120
|
prerelease: false
|
|
121
121
|
version_requirements: *id007
|
|
122
122
|
- !ruby/object:Gem::Dependency
|
|
123
|
-
name:
|
|
123
|
+
name: json
|
|
124
124
|
requirement: &id008 !ruby/object:Gem::Requirement
|
|
125
|
+
none: false
|
|
126
|
+
requirements:
|
|
127
|
+
- - ">="
|
|
128
|
+
- !ruby/object:Gem::Version
|
|
129
|
+
segments:
|
|
130
|
+
- 0
|
|
131
|
+
version: "0"
|
|
132
|
+
type: :development
|
|
133
|
+
prerelease: false
|
|
134
|
+
version_requirements: *id008
|
|
135
|
+
- !ruby/object:Gem::Dependency
|
|
136
|
+
name: mongoid
|
|
137
|
+
requirement: &id009 !ruby/object:Gem::Requirement
|
|
125
138
|
none: false
|
|
126
139
|
requirements:
|
|
127
140
|
- - ~>
|
|
@@ -135,10 +148,10 @@ dependencies:
|
|
|
135
148
|
version: 2.0.0.beta.18
|
|
136
149
|
type: :development
|
|
137
150
|
prerelease: false
|
|
138
|
-
version_requirements: *
|
|
151
|
+
version_requirements: *id009
|
|
139
152
|
- !ruby/object:Gem::Dependency
|
|
140
153
|
name: wrong
|
|
141
|
-
requirement: &
|
|
154
|
+
requirement: &id010 !ruby/object:Gem::Requirement
|
|
142
155
|
none: false
|
|
143
156
|
requirements:
|
|
144
157
|
- - ">="
|
|
@@ -148,10 +161,10 @@ dependencies:
|
|
|
148
161
|
version: "0"
|
|
149
162
|
type: :development
|
|
150
163
|
prerelease: false
|
|
151
|
-
version_requirements: *
|
|
164
|
+
version_requirements: *id010
|
|
152
165
|
- !ruby/object:Gem::Dependency
|
|
153
166
|
name: rspec
|
|
154
|
-
requirement: &
|
|
167
|
+
requirement: &id011 !ruby/object:Gem::Requirement
|
|
155
168
|
none: false
|
|
156
169
|
requirements:
|
|
157
170
|
- - ">="
|
|
@@ -165,10 +178,10 @@ dependencies:
|
|
|
165
178
|
version: 2.0.0.beta.19
|
|
166
179
|
type: :development
|
|
167
180
|
prerelease: false
|
|
168
|
-
version_requirements: *
|
|
181
|
+
version_requirements: *id011
|
|
169
182
|
- !ruby/object:Gem::Dependency
|
|
170
183
|
name: yard
|
|
171
|
-
requirement: &
|
|
184
|
+
requirement: &id012 !ruby/object:Gem::Requirement
|
|
172
185
|
none: false
|
|
173
186
|
requirements:
|
|
174
187
|
- - ~>
|
|
@@ -180,10 +193,10 @@ dependencies:
|
|
|
180
193
|
version: 0.6.0
|
|
181
194
|
type: :development
|
|
182
195
|
prerelease: false
|
|
183
|
-
version_requirements: *
|
|
196
|
+
version_requirements: *id012
|
|
184
197
|
- !ruby/object:Gem::Dependency
|
|
185
198
|
name: bundler
|
|
186
|
-
requirement: &
|
|
199
|
+
requirement: &id013 !ruby/object:Gem::Requirement
|
|
187
200
|
none: false
|
|
188
201
|
requirements:
|
|
189
202
|
- - ~>
|
|
@@ -195,10 +208,10 @@ dependencies:
|
|
|
195
208
|
version: 1.0.0
|
|
196
209
|
type: :development
|
|
197
210
|
prerelease: false
|
|
198
|
-
version_requirements: *
|
|
211
|
+
version_requirements: *id013
|
|
199
212
|
- !ruby/object:Gem::Dependency
|
|
200
213
|
name: jeweler
|
|
201
|
-
requirement: &
|
|
214
|
+
requirement: &id014 !ruby/object:Gem::Requirement
|
|
202
215
|
none: false
|
|
203
216
|
requirements:
|
|
204
217
|
- - ~>
|
|
@@ -211,10 +224,10 @@ dependencies:
|
|
|
211
224
|
version: 1.5.0.pre3
|
|
212
225
|
type: :development
|
|
213
226
|
prerelease: false
|
|
214
|
-
version_requirements: *
|
|
227
|
+
version_requirements: *id014
|
|
215
228
|
- !ruby/object:Gem::Dependency
|
|
216
229
|
name: rcov
|
|
217
|
-
requirement: &
|
|
230
|
+
requirement: &id015 !ruby/object:Gem::Requirement
|
|
218
231
|
none: false
|
|
219
232
|
requirements:
|
|
220
233
|
- - ">="
|
|
@@ -224,10 +237,10 @@ dependencies:
|
|
|
224
237
|
version: "0"
|
|
225
238
|
type: :development
|
|
226
239
|
prerelease: false
|
|
227
|
-
version_requirements: *
|
|
240
|
+
version_requirements: *id015
|
|
228
241
|
- !ruby/object:Gem::Dependency
|
|
229
242
|
name: activesupport
|
|
230
|
-
requirement: &
|
|
243
|
+
requirement: &id016 !ruby/object:Gem::Requirement
|
|
231
244
|
none: false
|
|
232
245
|
requirements:
|
|
233
246
|
- - ">="
|
|
@@ -237,10 +250,23 @@ dependencies:
|
|
|
237
250
|
version: "0"
|
|
238
251
|
type: :runtime
|
|
239
252
|
prerelease: false
|
|
240
|
-
version_requirements: *
|
|
253
|
+
version_requirements: *id016
|
|
254
|
+
- !ruby/object:Gem::Dependency
|
|
255
|
+
name: json
|
|
256
|
+
requirement: &id017 !ruby/object:Gem::Requirement
|
|
257
|
+
none: false
|
|
258
|
+
requirements:
|
|
259
|
+
- - ">="
|
|
260
|
+
- !ruby/object:Gem::Version
|
|
261
|
+
segments:
|
|
262
|
+
- 0
|
|
263
|
+
version: "0"
|
|
264
|
+
type: :runtime
|
|
265
|
+
prerelease: false
|
|
266
|
+
version_requirements: *id017
|
|
241
267
|
- !ruby/object:Gem::Dependency
|
|
242
268
|
name: mongoid
|
|
243
|
-
requirement: &
|
|
269
|
+
requirement: &id018 !ruby/object:Gem::Requirement
|
|
244
270
|
none: false
|
|
245
271
|
requirements:
|
|
246
272
|
- - ~>
|
|
@@ -254,10 +280,10 @@ dependencies:
|
|
|
254
280
|
version: 2.0.0.beta.18
|
|
255
281
|
type: :runtime
|
|
256
282
|
prerelease: false
|
|
257
|
-
version_requirements: *
|
|
283
|
+
version_requirements: *id018
|
|
258
284
|
- !ruby/object:Gem::Dependency
|
|
259
285
|
name: wrong
|
|
260
|
-
requirement: &
|
|
286
|
+
requirement: &id019 !ruby/object:Gem::Requirement
|
|
261
287
|
none: false
|
|
262
288
|
requirements:
|
|
263
289
|
- - ">="
|
|
@@ -267,7 +293,7 @@ dependencies:
|
|
|
267
293
|
version: "0"
|
|
268
294
|
type: :runtime
|
|
269
295
|
prerelease: false
|
|
270
|
-
version_requirements: *
|
|
296
|
+
version_requirements: *id019
|
|
271
297
|
description: My mesh of an all-in-one library for disjoint code.
|
|
272
298
|
email: benjaminlyu@gmail.com
|
|
273
299
|
executables: []
|
|
@@ -280,7 +306,6 @@ extra_rdoc_files:
|
|
|
280
306
|
- README.md
|
|
281
307
|
files:
|
|
282
308
|
- .document
|
|
283
|
-
- .gitignore
|
|
284
309
|
- Gemfile
|
|
285
310
|
- Gemfile.lock
|
|
286
311
|
- LICENSE
|
|
@@ -304,6 +329,9 @@ files:
|
|
|
304
329
|
- lib/junkfood/ceb/executors/event_executor.rb
|
|
305
330
|
- lib/junkfood/one_time.rb
|
|
306
331
|
- lib/junkfood/paperclip_string_io.rb
|
|
332
|
+
- lib/junkfood/rack.rb
|
|
333
|
+
- lib/junkfood/rack/error.rb
|
|
334
|
+
- lib/junkfood/rack/sessions.rb
|
|
307
335
|
- lib/junkfood/settings.rb
|
|
308
336
|
- spec/.rspec
|
|
309
337
|
- spec/junkfood/adler32_pure_spec.rb
|
|
@@ -318,6 +346,8 @@ files:
|
|
|
318
346
|
- spec/junkfood/ceb/executors/event_executor_spec.rb
|
|
319
347
|
- spec/junkfood/one_time_spec.rb
|
|
320
348
|
- spec/junkfood/paperclip_string_io_spec.rb
|
|
349
|
+
- spec/junkfood/rack/error_spec.rb
|
|
350
|
+
- spec/junkfood/rack/sessions_spec.rb
|
|
321
351
|
- spec/junkfood/settings_spec.rb
|
|
322
352
|
- spec/junkfood_spec.rb
|
|
323
353
|
- spec/spec_helper.rb
|
|
@@ -335,7 +365,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
335
365
|
requirements:
|
|
336
366
|
- - ">="
|
|
337
367
|
- !ruby/object:Gem::Version
|
|
338
|
-
hash:
|
|
368
|
+
hash: 529643725
|
|
339
369
|
segments:
|
|
340
370
|
- 0
|
|
341
371
|
version: "0"
|
|
@@ -367,6 +397,8 @@ test_files:
|
|
|
367
397
|
- spec/junkfood/ceb/executors/event_executor_spec.rb
|
|
368
398
|
- spec/junkfood/one_time_spec.rb
|
|
369
399
|
- spec/junkfood/paperclip_string_io_spec.rb
|
|
400
|
+
- spec/junkfood/rack/error_spec.rb
|
|
401
|
+
- spec/junkfood/rack/sessions_spec.rb
|
|
370
402
|
- spec/junkfood/settings_spec.rb
|
|
371
403
|
- spec/junkfood_spec.rb
|
|
372
404
|
- spec/spec_helper.rb
|