useless 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/useless.rb +4 -0
- data/lib/useless/fs.rb +32 -0
- data/lib/useless/logger.rb +48 -0
- data/lib/useless/mongo.rb +15 -0
- data/lib/useless/rack.rb +10 -21
- data/lib/useless/rack/base/files.rb +3 -1
- data/lib/useless/rack/middleware/authentication/query_string.rb +2 -0
- data/lib/useless/rack/middleware/authentication/request_header.rb +2 -0
- data/lib/useless/rack/middleware/doc.rb +2 -0
- data/lib/useless/version.rb +1 -1
- data/spec/useless/{rack/fs_spec.rb → fs_spec.rb} +8 -6
- data/spec/useless/{rack/logger_spec.rb → logger_spec.rb} +10 -9
- data/spec/useless/{rack/mongo_spec.rb → mongo_spec.rb} +9 -8
- data/spec/useless/rack/base/files_spec.rb +1 -0
- data/spec/useless/rack/middleware/authentication/query_string_spec.rb +1 -0
- data/spec/useless/rack/middleware/authentication/request_header_spec.rb +1 -0
- data/spec/useless/rack/middleware/exceptions_spec.rb +1 -0
- metadata +11 -10
- data/lib/useless/rack/fs.rb +0 -25
- data/lib/useless/rack/logger.rb +0 -50
- data/lib/useless/rack/mongo.rb +0 -17
data/lib/useless.rb
ADDED
data/lib/useless/fs.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'mongo'
|
2
|
+
|
3
|
+
module Useless
|
4
|
+
# Useless::FS is a minimal wrapper around Mongo::Grid.
|
5
|
+
class FS
|
6
|
+
|
7
|
+
def self.for_env(env = nil)
|
8
|
+
mongo = Useless::Mongo.for_env(env)
|
9
|
+
self.new(mongo)
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(mongo)
|
13
|
+
@mongo = mongo
|
14
|
+
end
|
15
|
+
|
16
|
+
def put(file, opts = {})
|
17
|
+
@mongo.grid.put(file, opts)
|
18
|
+
end
|
19
|
+
|
20
|
+
class FileNotFound < StandardError; end
|
21
|
+
|
22
|
+
def get(id)
|
23
|
+
@mongo.grid.get(id)
|
24
|
+
|
25
|
+
# To preserve the abstraction, raise our own exception when a file isn't
|
26
|
+
# found
|
27
|
+
rescue ::Mongo::GridFileNotFound => e
|
28
|
+
raise FileNotFound, e.message
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module Useless
|
4
|
+
# Useless::Logger is a wrapper around Logger for the purposes of decorating.
|
5
|
+
class Logger
|
6
|
+
attr_accessor :request_id
|
7
|
+
|
8
|
+
def initialize(io = $stdout)
|
9
|
+
@logger = ::Logger.new(io)
|
10
|
+
end
|
11
|
+
|
12
|
+
def level=(level)
|
13
|
+
@logger.level = level
|
14
|
+
end
|
15
|
+
|
16
|
+
def level
|
17
|
+
@logger.level
|
18
|
+
end
|
19
|
+
|
20
|
+
def fatal(message)
|
21
|
+
add ::Logger::FATAL, message
|
22
|
+
end
|
23
|
+
|
24
|
+
def error(message)
|
25
|
+
add ::Logger::ERROR, message
|
26
|
+
end
|
27
|
+
|
28
|
+
def warn(message)
|
29
|
+
add ::Logger::WARN, message
|
30
|
+
end
|
31
|
+
|
32
|
+
def info(message)
|
33
|
+
add ::Logger::INFO, message
|
34
|
+
end
|
35
|
+
|
36
|
+
def debug(message)
|
37
|
+
add ::Logger::DEBUG, message
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def add(level, message)
|
43
|
+
formatted_message = message
|
44
|
+
formatted_message = "[#{@request_id}] #{formatted_message}" if @request_id
|
45
|
+
@logger.add level, formatted_message
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'low/mongo'
|
2
|
+
|
3
|
+
module Useless
|
4
|
+
module Mongo
|
5
|
+
# Returns an instance of the appropriate `Low::Mongo` helper class based upon the
|
6
|
+
# `RACK_ENV` environment variable (or the `env` parameter, if specified).
|
7
|
+
def self.for_env(env = nil)
|
8
|
+
case (env || ENV['RACK_ENV'])
|
9
|
+
when 'test' then Low::Mongo::Local.new 'useless_test'
|
10
|
+
when 'production' then Low::Mongo::Remote.new ENV['MONGOLAB_URI']
|
11
|
+
else Low::Mongo::Local.new 'useless_development'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/useless/rack.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
$:.push File.expand_path('../lib', __FILE__)
|
2
|
-
|
3
1
|
require 'low/rack/log_level'
|
4
2
|
require 'low/rack/rack_errors'
|
5
3
|
require 'low/rack/request_id'
|
@@ -7,27 +5,17 @@ require 'low/rack/request_logger'
|
|
7
5
|
require 'low/rack/subdomain_map'
|
8
6
|
require 'low/scoped_logger'
|
9
7
|
|
8
|
+
require 'useless/rack/middleware/exceptions'
|
9
|
+
require 'useless/rack/middleware/assets'
|
10
|
+
require 'useless/rack/middleware/mongo'
|
11
|
+
require 'useless/rack/middleware/fs'
|
12
|
+
require 'useless/rack/middleware/doc'
|
13
|
+
require 'useless/rack/middleware/authentication/query_string'
|
14
|
+
require 'useless/rack/middleware/authentication/request_header'
|
15
|
+
require 'useless/rack/base'
|
16
|
+
|
10
17
|
module Useless
|
11
18
|
class Rack
|
12
|
-
autoload :Base, 'useless/rack/base'
|
13
|
-
autoload :Doc, 'useless/rack/doc'
|
14
|
-
autoload :FS, 'useless/rack/fs'
|
15
|
-
autoload :Logger, 'useless/rack/logger'
|
16
|
-
autoload :Mongo, 'useless/rack/mongo'
|
17
|
-
|
18
|
-
module Middleware
|
19
|
-
autoload :Assets, 'useless/rack/middleware/assets'
|
20
|
-
autoload :Doc, 'useless/rack/middleware/doc'
|
21
|
-
autoload :Exceptions, 'useless/rack/middleware/exceptions'
|
22
|
-
autoload :FS, 'useless/rack/middleware/fs'
|
23
|
-
autoload :Mongo, 'useless/rack/middleware/mongo'
|
24
|
-
|
25
|
-
module Authentication
|
26
|
-
autoload :AccessToken, 'useless/rack/middleware/authentication/access_token'
|
27
|
-
autoload :QueryString, 'useless/rack/middleware/authentication/query_string'
|
28
|
-
autoload :RequestHeader, 'useless/rack/middleware/authentication/request_header'
|
29
|
-
end
|
30
|
-
end
|
31
19
|
|
32
20
|
def self.mongo
|
33
21
|
@mongo ||= Mongo.for_env
|
@@ -65,5 +53,6 @@ module Useless
|
|
65
53
|
run Low::Rack::SubdomainMap.new(Base.new, @map)
|
66
54
|
end.call(env)
|
67
55
|
end
|
56
|
+
|
68
57
|
end
|
69
58
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'useless/fs'
|
2
|
+
|
1
3
|
module Useless
|
2
4
|
class Rack
|
3
5
|
class Base
|
@@ -22,7 +24,7 @@ module Useless
|
|
22
24
|
# Two things can go wrong, and they'll both raise an error:
|
23
25
|
# * the specified ID is not a valid object ID
|
24
26
|
# * there is no file corresponding to the ID in the FS
|
25
|
-
rescue BSON::InvalidObjectId, Useless::
|
27
|
+
rescue BSON::InvalidObjectId, Useless::FS::FileNotFound
|
26
28
|
[404, {'Content-Type' => 'text/plain'}, "File not found: #{id}"]
|
27
29
|
end
|
28
30
|
end
|
data/lib/useless/version.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
require File.dirname(__FILE__) + '
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
require 'useless/fs'
|
2
3
|
|
3
|
-
describe Useless::
|
4
|
+
describe Useless::FS do
|
4
5
|
it 'should allow for files to be added and retrieved via .put and .get' do
|
5
6
|
original_file = File.open(asset_path('muffin-milk.jpg'))
|
6
|
-
|
7
|
-
|
7
|
+
fs = Useless::FS.for_env('test')
|
8
|
+
original_file_id = fs.put(original_file)
|
9
|
+
stored_file = fs.get(original_file_id)
|
8
10
|
|
9
|
-
# Useless::
|
11
|
+
# Useless::FS stores the file as an un-encoded binary blob, so in order to
|
10
12
|
# compare, we must strip the encoding, i.e. force a binary encoding.
|
11
13
|
original_file.rewind
|
12
14
|
original_blob = original_file.read
|
@@ -16,7 +18,7 @@ describe Useless::Rack::FS do
|
|
16
18
|
|
17
19
|
describe '.get' do
|
18
20
|
it 'should raise Useless::FS::FileNotFound if the specified ID doesn\'t exist' do
|
19
|
-
-> { Useless::
|
21
|
+
-> { Useless::FS.for_env('test').get('nonexistant') }.should raise_error(Useless::FS::FileNotFound)
|
20
22
|
end
|
21
23
|
end
|
22
24
|
end
|
@@ -1,10 +1,11 @@
|
|
1
|
-
require File.dirname(__FILE__) + '
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
require 'useless/logger'
|
2
3
|
|
3
|
-
describe Useless::
|
4
|
+
describe Useless::Logger do
|
4
5
|
describe '#fatal' do
|
5
6
|
it 'should emit a fatal message' do
|
6
7
|
io = StringIO.new
|
7
|
-
logger = Useless::
|
8
|
+
logger = Useless::Logger.new(io)
|
8
9
|
logger.fatal 'Oh noes!'
|
9
10
|
|
10
11
|
io.rewind; message = io.read
|
@@ -16,7 +17,7 @@ describe Useless::Rack::Logger do
|
|
16
17
|
describe '#error' do
|
17
18
|
it 'should emit an error message' do
|
18
19
|
io = StringIO.new
|
19
|
-
logger = Useless::
|
20
|
+
logger = Useless::Logger.new(io)
|
20
21
|
logger.error 'Wrong!'
|
21
22
|
|
22
23
|
io.rewind; message = io.read
|
@@ -28,7 +29,7 @@ describe Useless::Rack::Logger do
|
|
28
29
|
describe '#warn' do
|
29
30
|
it 'should emit a warn message' do
|
30
31
|
io = StringIO.new
|
31
|
-
logger = Useless::
|
32
|
+
logger = Useless::Logger.new(io)
|
32
33
|
logger.warn 'Watchout!'
|
33
34
|
|
34
35
|
io.rewind; message = io.read
|
@@ -40,7 +41,7 @@ describe Useless::Rack::Logger do
|
|
40
41
|
describe '#info' do
|
41
42
|
it 'should emit an info message' do
|
42
43
|
io = StringIO.new
|
43
|
-
logger = Useless::
|
44
|
+
logger = Useless::Logger.new(io)
|
44
45
|
logger.info 'Such and such'
|
45
46
|
|
46
47
|
io.rewind; message = io.read
|
@@ -52,7 +53,7 @@ describe Useless::Rack::Logger do
|
|
52
53
|
describe '#debug' do
|
53
54
|
it 'should emit a debug message' do
|
54
55
|
io = StringIO.new
|
55
|
-
logger = Useless::
|
56
|
+
logger = Useless::Logger.new(io)
|
56
57
|
logger.debug '101011'
|
57
58
|
|
58
59
|
io.rewind; message = io.read
|
@@ -62,7 +63,7 @@ describe Useless::Rack::Logger do
|
|
62
63
|
|
63
64
|
it 'should emit nothing if the level is too high' do
|
64
65
|
io = StringIO.new
|
65
|
-
logger = Useless::
|
66
|
+
logger = Useless::Logger.new(io)
|
66
67
|
logger.level = ::Logger::INFO
|
67
68
|
logger.debug '101011'
|
68
69
|
|
@@ -74,7 +75,7 @@ describe Useless::Rack::Logger do
|
|
74
75
|
describe '#request_id' do
|
75
76
|
it 'should append the specified ID to all messages' do
|
76
77
|
io = StringIO.new
|
77
|
-
logger = Useless::
|
78
|
+
logger = Useless::Logger.new(io)
|
78
79
|
logger.request_id = 'abc123'
|
79
80
|
logger.info 'Yadda'
|
80
81
|
|
@@ -1,9 +1,10 @@
|
|
1
|
-
require File.dirname(__FILE__) + '
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
require 'useless/mongo'
|
2
3
|
|
3
|
-
describe Useless::
|
4
|
+
describe Useless::Mongo do
|
4
5
|
describe '.for_env' do
|
5
6
|
it 'should return a instance for \'useless_development\' if \'development\' is specified' do
|
6
|
-
mongo = Useless::
|
7
|
+
mongo = Useless::Mongo.for_env('development')
|
7
8
|
mongo.database.should == 'useless_development'
|
8
9
|
end
|
9
10
|
|
@@ -11,7 +12,7 @@ describe Useless::Rack::Mongo do
|
|
11
12
|
original_rack_env = ENV['RACK_ENV']
|
12
13
|
begin
|
13
14
|
ENV['RACK_ENV'] = 'development'
|
14
|
-
mongo = Useless::
|
15
|
+
mongo = Useless::Mongo.for_env
|
15
16
|
mongo.database.should == 'useless_development'
|
16
17
|
ensure
|
17
18
|
ENV['RACK_ENV'] = original_rack_env
|
@@ -19,7 +20,7 @@ describe Useless::Rack::Mongo do
|
|
19
20
|
end
|
20
21
|
|
21
22
|
it 'should return a instance for \'useless_test\' if \'test\' is specified' do
|
22
|
-
mongo = Useless::
|
23
|
+
mongo = Useless::Mongo.for_env('test')
|
23
24
|
mongo.database.should == 'useless_test'
|
24
25
|
end
|
25
26
|
|
@@ -27,7 +28,7 @@ describe Useless::Rack::Mongo do
|
|
27
28
|
original_rack_env = ENV['RACK_ENV']
|
28
29
|
begin
|
29
30
|
ENV['RACK_ENV'] = 'test'
|
30
|
-
mongo = Useless::
|
31
|
+
mongo = Useless::Mongo.for_env
|
31
32
|
mongo.database.should == 'useless_test'
|
32
33
|
ensure
|
33
34
|
ENV['RACK_ENV'] = original_rack_env
|
@@ -38,7 +39,7 @@ describe Useless::Rack::Mongo do
|
|
38
39
|
original_mongolab_uri = ENV['MONGOLAB_URI']
|
39
40
|
begin
|
40
41
|
ENV['MONGOLAB_URI'] = 'mongodb://mongo.com'
|
41
|
-
mongo = Useless::
|
42
|
+
mongo = Useless::Mongo.for_env('production')
|
42
43
|
mongo.uri.should == 'mongodb://mongo.com'
|
43
44
|
ensure
|
44
45
|
ENV['MONGOLAB_URI'] = original_mongolab_uri
|
@@ -51,7 +52,7 @@ describe Useless::Rack::Mongo do
|
|
51
52
|
begin
|
52
53
|
ENV['RACK_ENV'] = 'production'
|
53
54
|
ENV['MONGOLAB_URI'] = 'mongodb://mongo.com'
|
54
|
-
mongo = Useless::
|
55
|
+
mongo = Useless::Mongo.for_env
|
55
56
|
mongo.uri.should == 'mongodb://mongo.com'
|
56
57
|
ensure
|
57
58
|
ENV['RACK_ENV'] = original_rack_env
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: useless
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -136,13 +136,15 @@ files:
|
|
136
136
|
- README.md
|
137
137
|
- Rakefile
|
138
138
|
- assets/application.css
|
139
|
+
- lib/useless.rb
|
140
|
+
- lib/useless/fs.rb
|
141
|
+
- lib/useless/logger.rb
|
142
|
+
- lib/useless/mongo.rb
|
139
143
|
- lib/useless/rack.rb
|
140
144
|
- lib/useless/rack/base.rb
|
141
145
|
- lib/useless/rack/base/doc.rb
|
142
146
|
- lib/useless/rack/base/files.rb
|
143
147
|
- lib/useless/rack/doc.rb
|
144
|
-
- lib/useless/rack/fs.rb
|
145
|
-
- lib/useless/rack/logger.rb
|
146
148
|
- lib/useless/rack/middleware/assets.rb
|
147
149
|
- lib/useless/rack/middleware/authentication/access_token.rb
|
148
150
|
- lib/useless/rack/middleware/authentication/query_string.rb
|
@@ -151,17 +153,16 @@ files:
|
|
151
153
|
- lib/useless/rack/middleware/exceptions.rb
|
152
154
|
- lib/useless/rack/middleware/fs.rb
|
153
155
|
- lib/useless/rack/middleware/mongo.rb
|
154
|
-
- lib/useless/rack/mongo.rb
|
155
156
|
- lib/useless/version.rb
|
156
157
|
- spec/spec_helper.rb
|
157
158
|
- spec/support/assets/muffin-milk.jpg
|
159
|
+
- spec/useless/fs_spec.rb
|
160
|
+
- spec/useless/logger_spec.rb
|
161
|
+
- spec/useless/mongo_spec.rb
|
158
162
|
- spec/useless/rack/base/files_spec.rb
|
159
|
-
- spec/useless/rack/fs_spec.rb
|
160
|
-
- spec/useless/rack/logger_spec.rb
|
161
163
|
- spec/useless/rack/middleware/authentication/query_string_spec.rb
|
162
164
|
- spec/useless/rack/middleware/authentication/request_header_spec.rb
|
163
165
|
- spec/useless/rack/middleware/exceptions_spec.rb
|
164
|
-
- spec/useless/rack/mongo_spec.rb
|
165
166
|
- useless.gemspec
|
166
167
|
homepage: https://github.com/khy/useless.rb
|
167
168
|
licenses: []
|
@@ -190,10 +191,10 @@ summary: The useless.io Ruby library.
|
|
190
191
|
test_files:
|
191
192
|
- spec/spec_helper.rb
|
192
193
|
- spec/support/assets/muffin-milk.jpg
|
194
|
+
- spec/useless/fs_spec.rb
|
195
|
+
- spec/useless/logger_spec.rb
|
196
|
+
- spec/useless/mongo_spec.rb
|
193
197
|
- spec/useless/rack/base/files_spec.rb
|
194
|
-
- spec/useless/rack/fs_spec.rb
|
195
|
-
- spec/useless/rack/logger_spec.rb
|
196
198
|
- spec/useless/rack/middleware/authentication/query_string_spec.rb
|
197
199
|
- spec/useless/rack/middleware/authentication/request_header_spec.rb
|
198
200
|
- spec/useless/rack/middleware/exceptions_spec.rb
|
199
|
-
- spec/useless/rack/mongo_spec.rb
|
data/lib/useless/rack/fs.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
module Useless
|
2
|
-
class Rack
|
3
|
-
# Useless::FS is a minimal wrapper around Mongo::Grid.
|
4
|
-
class FS
|
5
|
-
class FileNotFound < StandardError; end
|
6
|
-
|
7
|
-
def initialize(mongo)
|
8
|
-
@mongo = mongo
|
9
|
-
end
|
10
|
-
|
11
|
-
def put(file, opts = {})
|
12
|
-
@mongo.grid.put(file, opts)
|
13
|
-
end
|
14
|
-
|
15
|
-
def get(id)
|
16
|
-
@mongo.grid.get(id)
|
17
|
-
|
18
|
-
# To preserve the abstraction, raise our own exception when a file isn't
|
19
|
-
# found
|
20
|
-
rescue ::Mongo::GridFileNotFound => e
|
21
|
-
raise FileNotFound, e.message
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
data/lib/useless/rack/logger.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
require 'logger'
|
2
|
-
|
3
|
-
module Useless
|
4
|
-
class Rack
|
5
|
-
# Useless::Logger is a wrapper around Logger for the purposes of decorating.
|
6
|
-
class Logger
|
7
|
-
attr_accessor :request_id
|
8
|
-
|
9
|
-
def initialize(io = $stdout)
|
10
|
-
@logger = ::Logger.new(io)
|
11
|
-
end
|
12
|
-
|
13
|
-
def level=(level)
|
14
|
-
@logger.level = level
|
15
|
-
end
|
16
|
-
|
17
|
-
def level
|
18
|
-
@logger.level
|
19
|
-
end
|
20
|
-
|
21
|
-
def fatal(message)
|
22
|
-
add ::Logger::FATAL, message
|
23
|
-
end
|
24
|
-
|
25
|
-
def error(message)
|
26
|
-
add ::Logger::ERROR, message
|
27
|
-
end
|
28
|
-
|
29
|
-
def warn(message)
|
30
|
-
add ::Logger::WARN, message
|
31
|
-
end
|
32
|
-
|
33
|
-
def info(message)
|
34
|
-
add ::Logger::INFO, message
|
35
|
-
end
|
36
|
-
|
37
|
-
def debug(message)
|
38
|
-
add ::Logger::DEBUG, message
|
39
|
-
end
|
40
|
-
|
41
|
-
private
|
42
|
-
|
43
|
-
def add(level, message)
|
44
|
-
formatted_message = message
|
45
|
-
formatted_message = "[#{@request_id}] #{formatted_message}" if @request_id
|
46
|
-
@logger.add level, formatted_message
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
data/lib/useless/rack/mongo.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
require 'low/mongo'
|
2
|
-
|
3
|
-
module Useless
|
4
|
-
class Rack
|
5
|
-
module Mongo
|
6
|
-
# Returns an instance of the appropriate `Low::Mongo` helper class based upon the
|
7
|
-
# `RACK_ENV` environment variable (or the `env` parameter, if specified).
|
8
|
-
def self.for_env(env = nil)
|
9
|
-
case (env || ENV['RACK_ENV'])
|
10
|
-
when 'test' then Low::Mongo::Local.new 'useless_test'
|
11
|
-
when 'production' then Low::Mongo::Remote.new ENV['MONGOLAB_URI']
|
12
|
-
else Low::Mongo::Local.new 'useless_development'
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|