nutheory_rack 0.0.1

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.
@@ -0,0 +1,2 @@
1
+ rdoc
2
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Blake Carlson
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.
@@ -0,0 +1,45 @@
1
+ = Rack::GridFS
2
+
3
+ Rack:GridFS is a Rack middleware for creating HTTP endpoints for files
4
+ stored in MongoDB's GridFS. You can configure a prefix string which
5
+ will be used to match the path of a request and create an id for looking
6
+ up the file in the GridFS store.
7
+
8
+ For example,
9
+
10
+ GET '/gridfs/someobjectid'
11
+
12
+ If the prefix is "gridfs", then the key will be be "someobjectid".
13
+
14
+ == Installation
15
+
16
+ gem install jnunemaker--rack-gridfs
17
+
18
+ == Usage
19
+
20
+ require 'rack/gridfs'
21
+ use Rack::GridFS, :hostname => 'localhost', :port => 27017, :database => 'test', :prefix => 'gridfs'
22
+
23
+ You must specify MongoDB database details:
24
+ - hostname: the hostname/IP where the MongoDB server is running. Default 'localhost'.
25
+ - port: the port of the MongoDB server. Default 27017.
26
+ - database: the MongoDB database to connect to.
27
+ - prefix: a string used to match against incoming paths and route to through the middleware. Default 'gridfs'.
28
+
29
+ == Sinatra Example
30
+
31
+ require 'rubygems'
32
+ require 'sinatra'
33
+
34
+ require 'rack/gridfs'
35
+ use Rack::GridFS, :hostname => 'localhost', :port => 27017, :database => 'test', :prefix => 'gridfs'
36
+
37
+ # put a file in gridfs and try visiting /gridfs/objectid
38
+
39
+ get /.*/ do
40
+ "Whatchya talking about?"
41
+ end
42
+
43
+ == Copyright
44
+
45
+ Copyright (c) 2009 Blake Carlson. See LICENSE for details.
@@ -0,0 +1,66 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "nutheory-rack-gridfs"
8
+ gem.summary = "Rack middleware for creating HTTP endpoints for files stored in MongoDB's GridFS"
9
+ gem.email = "blake@coin-operated.net"
10
+ gem.homepage = "http://github.com/skinandbones/rack-gridfs"
11
+ gem.authors = ["Blake Carlson"]
12
+ gem.rubyforge_project = "rack-gridfs"
13
+
14
+ gem.add_dependency('rack')
15
+ gem.add_dependency('mongo', '0.19.3')
16
+ #gem.add_dependency('mongo', '0.19.1')
17
+
18
+ gem.add_development_dependency('mocha', '0.9.4')
19
+ gem.add_development_dependency('rack-test')
20
+ gem.add_development_dependency('shoulda')
21
+ end
22
+ Jeweler::GemcutterTasks.new
23
+ Jeweler::RubyforgeTasks.new do |rubyforge|
24
+ rubyforge.doc_task = "rdoc"
25
+ end
26
+ rescue LoadError
27
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
28
+ end
29
+
30
+ require 'rake/testtask'
31
+ Rake::TestTask.new(:test) do |test|
32
+ test.libs << 'lib' << 'test'
33
+ test.pattern = 'test/**/*_test.rb'
34
+ test.verbose = true
35
+ end
36
+
37
+ begin
38
+ require 'rcov/rcovtask'
39
+ Rcov::RcovTask.new do |test|
40
+ test.libs << 'test'
41
+ test.pattern = 'test/**/*_test.rb'
42
+ test.verbose = true
43
+ end
44
+ rescue LoadError
45
+ task :rcov do
46
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
47
+ end
48
+ end
49
+
50
+ task :test => :check_dependencies
51
+
52
+ task :default => :test
53
+
54
+ require 'rake/rdoctask'
55
+ Rake::RDocTask.new do |rdoc|
56
+ if File.exist?('VERSION')
57
+ version = File.read('VERSION')
58
+ else
59
+ version = ""
60
+ end
61
+
62
+ rdoc.rdoc_dir = 'rdoc'
63
+ rdoc.title = "Rack::GridFS #{version}"
64
+ rdoc.rdoc_files.include('README*')
65
+ rdoc.rdoc_files.include('lib/**/*.rb')
66
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'sinatra'
3
+
4
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'rack', 'gridfs')
5
+ use Rack::GridFS, :hostname => 'localhost', :port => 27017, :database => 'test', :prefix => 'gridfs'
6
+
7
+ get /.*/ do
8
+ "Whatchya talking about?"
9
+ end
@@ -0,0 +1,51 @@
1
+ require 'timeout'
2
+ require 'mongo'
3
+
4
+ module Rack
5
+ class GridFSConnectionError < StandardError ; end
6
+ class GridFS
7
+ attr_reader :hostname, :port, :database, :prefix, :db
8
+
9
+ def initialize(app, options = {})
10
+ options = {
11
+ :hostname => 'localhost',
12
+ :prefix => 'gridfs',
13
+ :port => Mongo::Connection::DEFAULT_PORT,
14
+ }.merge(options)
15
+
16
+ @app = app
17
+ @hostname = options[:hostname]
18
+ @port = options[:port]
19
+ @database = options[:database]
20
+ @prefix = options[:prefix]
21
+ @db = nil
22
+
23
+ connect!
24
+ end
25
+
26
+ def call(env)
27
+ request = Rack::Request.new(env)
28
+ if request.path_info =~ /^\/#{prefix}\/(.+)$/
29
+ gridfs_request($1)
30
+ else
31
+ @app.call(env)
32
+ end
33
+ end
34
+
35
+ def gridfs_request(id)
36
+ file = Mongo::Grid.new(db).get(Mongo::ObjectID.from_string(id))
37
+ [200, {'Content-Type' => file.content_type}, [file.read]]
38
+ rescue Mongo::GridError, Mongo::InvalidObjectID
39
+ [404, {'Content-Type' => 'text/plain'}, ['File not found.']]
40
+ end
41
+
42
+ private
43
+ def connect!
44
+ Timeout::timeout(5) do
45
+ @db = Mongo::Connection.new(hostname).db(database)
46
+ end
47
+ rescue Exception => e
48
+ raise Rack::GridFSConnectionError, "Unable to connect to the MongoDB server (#{e.to_s})"
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,71 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{nutheory_rack}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Blake Carlson"]
12
+ s.date = %q{2010-01-04}
13
+ s.email = %q{blake@coin-operated.net}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "example/gridfs_server.rb",
25
+ "lib/rack/gridfs.rb",
26
+ "rack-gridfs.gemspec",
27
+ "test/artifacts/3wolfmoon.jpg",
28
+ "test/artifacts/test.html",
29
+ "test/artifacts/test.txt",
30
+ "test/gridfs_test.rb",
31
+ "test/test_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/skinandbones/rack-gridfs}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubyforge_project = %q{rack-gridfs}
37
+ s.rubygems_version = %q{1.3.5}
38
+ s.summary = %q{Rack middleware for creating HTTP endpoints for files stored in MongoDB's GridFS}
39
+ s.test_files = [
40
+ "test/gridfs_test.rb",
41
+ "test/test_helper.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<rack>, [">= 0"])
50
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
51
+ s.add_runtime_dependency(%q<mongo>, ["= 0.18.2"])
52
+ s.add_development_dependency(%q<mocha>, ["= 0.9.4"])
53
+ s.add_development_dependency(%q<rack-test>, [">= 0"])
54
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
55
+ else
56
+ s.add_dependency(%q<rack>, [">= 0"])
57
+ s.add_dependency(%q<activesupport>, [">= 0"])
58
+ s.add_dependency(%q<mongo>, ["= 0.18.2"])
59
+ s.add_dependency(%q<mocha>, ["= 0.9.4"])
60
+ s.add_dependency(%q<rack-test>, [">= 0"])
61
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<rack>, [">= 0"])
65
+ s.add_dependency(%q<activesupport>, [">= 0"])
66
+ s.add_dependency(%q<mongo>, ["= 0.18.2"])
67
+ s.add_dependency(%q<mocha>, ["= 0.9.4"])
68
+ s.add_dependency(%q<rack-test>, [">= 0"])
69
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
70
+ end
71
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
2
+ "http://www.w3.org/TR/html4/strict.dtd">
3
+
4
+ <html lang="en">
5
+ <head>
6
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7
+ <title>test</title>
8
+ <meta name="generator" content="TextMate http://macromates.com/">
9
+ <meta name="author" content="Blake Carlson">
10
+ <!-- Date: 2009-09-20 -->
11
+ </head>
12
+ <body>
13
+ Test
14
+ </body>
15
+ </html>
@@ -0,0 +1 @@
1
+ Lorem ipsum dolor sit amet.
@@ -0,0 +1,140 @@
1
+ require 'test_helper'
2
+
3
+ class Rack::GridFSTest < Test::Unit::TestCase
4
+ include Rack::Test::Methods
5
+
6
+ def stub_mongodb_connection
7
+ Rack::GridFS.any_instance.stubs(:connect!).returns(true)
8
+ end
9
+
10
+ def test_database_options
11
+ { :hostname => 'localhost', :port => 27017, :database => 'test', :prefix => 'gridfs' }
12
+ end
13
+
14
+ def db
15
+ @db ||= Mongo::Connection.new(test_database_options[:hostname], test_database_options[:port]).db(test_database_options[:database])
16
+ end
17
+
18
+ def app
19
+ gridfs_opts = test_database_options
20
+ Rack::Builder.new do
21
+ use Rack::GridFS, gridfs_opts
22
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ["Hello, World!"]] }
23
+ end
24
+ end
25
+
26
+ def load_artifact(filename, key, content_type)
27
+ contents = File.read(File.join(File.dirname(__FILE__), 'artifacts', filename))
28
+ Mongo::Grid.new(db).put(contents, filename, :content_type => content_type)
29
+ end
30
+
31
+ context "Rack::GridFS" do
32
+ context "on initialization" do
33
+ setup do
34
+ stub_mongodb_connection
35
+ @options = { :hostname => 'myhostname.mydomain', :port => 8765, :database => 'mydatabase', :prefix => 'myprefix' }
36
+ end
37
+
38
+ should "have a hostname option" do
39
+ mware = Rack::GridFS.new(nil, @options)
40
+ assert_equal @options[:hostname], mware.hostname
41
+ end
42
+
43
+ should "have a default hostname" do
44
+ @options.delete(:hostname)
45
+ mware = Rack::GridFS.new(nil, @options)
46
+ assert_equal 'localhost', mware.hostname
47
+ end
48
+
49
+ should "have a port option" do
50
+ mware = Rack::GridFS.new(nil, @options)
51
+ assert_equal @options[:port], mware.port
52
+ end
53
+
54
+ should "have a default port" do
55
+ @options.delete(:port)
56
+ mware = Rack::GridFS.new(nil, @options)
57
+ assert_equal Mongo::Connection::DEFAULT_PORT, mware.port
58
+ end
59
+
60
+ should "have a database option" do
61
+ mware = Rack::GridFS.new(nil, @options)
62
+ assert_equal @options[:database], mware.database
63
+ end
64
+
65
+ should "not have a default database" do
66
+ @options.delete(:database)
67
+ mware = Rack::GridFS.new(nil, @options)
68
+ assert_nil mware.database
69
+ end
70
+
71
+ should "have a prefix option" do
72
+ mware = Rack::GridFS.new(nil, @options)
73
+ assert_equal mware.prefix, @options[:prefix]
74
+ end
75
+
76
+ should "have a default prefix" do
77
+ @options.delete(:prefix)
78
+ mware = Rack::GridFS.new(nil, @options)
79
+ assert_equal mware.prefix, 'gridfs'
80
+ end
81
+
82
+ should "connect to the MongoDB server" do
83
+ Rack::GridFS.any_instance.expects(:connect!).returns(true).once
84
+ Rack::GridFS.new(nil, @options)
85
+ end
86
+ end
87
+
88
+ should "delegate requests with a non-matching prefix" do
89
+ %w( / /posts /posts/1 /posts/1/comments ).each do |path|
90
+ get path
91
+ assert last_response.ok?
92
+ assert 'Hello, World!', last_response.body
93
+ end
94
+ end
95
+
96
+ context "with files in GridFS" do
97
+ setup do
98
+ @text_id = load_artifact('test.txt', 'test.txt', 'text/plain')
99
+ @html_id = load_artifact('test.html', 'test.html', 'text/html')
100
+ end
101
+
102
+ teardown do
103
+ db.collection('fs.files').remove
104
+ end
105
+
106
+ should "return TXT files stored in GridFS" do
107
+ get "/gridfs/#{@text_id}"
108
+ assert_equal "Lorem ipsum dolor sit amet.", last_response.body
109
+ end
110
+
111
+ should "return the proper content type for TXT files" do
112
+ get "/gridfs/#{@text_id}"
113
+ assert_equal 'text/plain', last_response.content_type
114
+ end
115
+
116
+ should "return HTML files stored in GridFS" do
117
+ get "/gridfs/#{@html_id}"
118
+ assert_match /html.*?body.*Test/m, last_response.body
119
+ end
120
+
121
+ should "return the proper content type for HTML files" do
122
+ get "/gridfs/#{@html_id}"
123
+ assert_equal 'text/html', last_response.content_type
124
+ end
125
+
126
+ should "return a not found for a unknown path" do
127
+ get '/gridfs/unknown'
128
+ assert last_response.not_found?
129
+ end
130
+
131
+ should "work for small images" do
132
+ image_id = load_artifact('3wolfmoon.jpg', 'images/3wolfmoon.jpg', 'image/jpeg')
133
+ get "/gridfs/#{image_id}"
134
+ assert last_response.ok?
135
+ assert_equal 'image/jpeg', last_response.content_type
136
+ end
137
+ end
138
+ end
139
+ end
140
+
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ require 'rack/builder'
7
+ require 'rack/mock'
8
+ require 'rack/test'
9
+
10
+ require 'mongo'
11
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'rack', 'gridfs')
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nutheory_rack
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Blake Carlson
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-01-04 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rack
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: activesupport
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :runtime
43
+ version_requirements: *id002
44
+ - !ruby/object:Gem::Dependency
45
+ name: mongo
46
+ prerelease: false
47
+ requirement: &id003 !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - "="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ - 18
54
+ - 2
55
+ version: 0.18.2
56
+ type: :runtime
57
+ version_requirements: *id003
58
+ - !ruby/object:Gem::Dependency
59
+ name: mocha
60
+ prerelease: false
61
+ requirement: &id004 !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - "="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ - 9
68
+ - 4
69
+ version: 0.9.4
70
+ type: :development
71
+ version_requirements: *id004
72
+ - !ruby/object:Gem::Dependency
73
+ name: rack-test
74
+ prerelease: false
75
+ requirement: &id005 !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ type: :development
83
+ version_requirements: *id005
84
+ - !ruby/object:Gem::Dependency
85
+ name: thoughtbot-shoulda
86
+ prerelease: false
87
+ requirement: &id006 !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ type: :development
95
+ version_requirements: *id006
96
+ description:
97
+ email: blake@coin-operated.net
98
+ executables: []
99
+
100
+ extensions: []
101
+
102
+ extra_rdoc_files:
103
+ - LICENSE
104
+ - README.rdoc
105
+ files:
106
+ - .gitignore
107
+ - LICENSE
108
+ - README.rdoc
109
+ - Rakefile
110
+ - VERSION
111
+ - example/gridfs_server.rb
112
+ - lib/rack/gridfs.rb
113
+ - rack-gridfs.gemspec
114
+ - test/artifacts/3wolfmoon.jpg
115
+ - test/artifacts/test.html
116
+ - test/artifacts/test.txt
117
+ - test/gridfs_test.rb
118
+ - test/test_helper.rb
119
+ has_rdoc: true
120
+ homepage: http://github.com/skinandbones/rack-gridfs
121
+ licenses: []
122
+
123
+ post_install_message:
124
+ rdoc_options:
125
+ - --charset=UTF-8
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ segments:
133
+ - 0
134
+ version: "0"
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ segments:
140
+ - 0
141
+ version: "0"
142
+ requirements: []
143
+
144
+ rubyforge_project: rack-gridfs
145
+ rubygems_version: 1.3.6
146
+ signing_key:
147
+ specification_version: 3
148
+ summary: Rack middleware for creating HTTP endpoints for files stored in MongoDB's GridFS
149
+ test_files:
150
+ - test/gridfs_test.rb
151
+ - test/test_helper.rb