mujhs-rack-gridfs 0.4.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.
@@ -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,64 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "jnunemaker-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
+
13
+ gem.add_dependency('rack')
14
+ gem.add_dependency('mongo', '0.19.1')
15
+
16
+ gem.add_development_dependency('mocha', '0.9.4')
17
+ gem.add_development_dependency('rack-test')
18
+ gem.add_development_dependency('shoulda')
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ Jeweler::RubyforgeTasks.new do |rubyforge|
22
+ rubyforge.doc_task = "rdoc"
23
+ end
24
+ rescue LoadError
25
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
26
+ end
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ begin
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/*_test.rb'
40
+ test.verbose = true
41
+ end
42
+ rescue LoadError
43
+ task :rcov do
44
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
45
+ end
46
+ end
47
+
48
+ task :test => :check_dependencies
49
+
50
+ task :default => :test
51
+
52
+ require 'rake/rdoctask'
53
+ Rake::RDocTask.new do |rdoc|
54
+ if File.exist?('VERSION')
55
+ version = File.read('VERSION')
56
+ else
57
+ version = ""
58
+ end
59
+
60
+ rdoc.rdoc_dir = 'rdoc'
61
+ rdoc.title = "Rack::GridFS #{version}"
62
+ rdoc.rdoc_files.include('README*')
63
+ rdoc.rdoc_files.include('lib/**/*.rb')
64
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'sinatra'
3
+
4
+ gem 'jnunemaker-rack-gridfs'
5
+ require 'rack/gridfs'
6
+
7
+ use Rack::GridFS, :hostname => 'localhost', :port => 27017, :database => 'testing', :prefix => 'gridfs'
8
+
9
+ db = Mongo::Connection.new.db('testing')
10
+ id = Mongo::Grid.new(db).put(File.read('mr_t.jpg'), 'mr_t.jpg')
11
+
12
+ get /.*/ do
13
+ %Q(Maybe you should look at <a href="/gridfs/#{id}">this file</a>)
14
+ end
@@ -0,0 +1,52 @@
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
+ @prefix = options[:prefix]
18
+ @db = nil
19
+
20
+ @hostname, @port, @database, @username, @password =
21
+ options.values_at(:hostname, :port, :database, :username, :password)
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, port).db(database)
46
+ @db.authenticate(@username, @password) if @username
47
+ end
48
+ rescue Exception => e
49
+ raise Rack::GridFSConnectionError, "Unable to connect to the MongoDB server (#{e.to_s})"
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mujhs-rack-gridfs}
8
+ s.version = "0.4.0"
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-03-10}
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
+ "mujhs-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<mongo>, ["= 0.19.3"])
51
+ s.add_development_dependency(%q<mocha>, ["= 0.9.4"])
52
+ s.add_development_dependency(%q<rack-test>, [">= 0"])
53
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
54
+ else
55
+ s.add_dependency(%q<rack>, [">= 0"])
56
+ s.add_dependency(%q<mongo>, ["= 0.19.3"])
57
+ s.add_dependency(%q<mocha>, ["= 0.9.4"])
58
+ s.add_dependency(%q<rack-test>, [">= 0"])
59
+ s.add_dependency(%q<shoulda>, [">= 0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<rack>, [">= 0"])
63
+ s.add_dependency(%q<mongo>, ["= 0.19.3"])
64
+ s.add_dependency(%q<mocha>, ["= 0.9.4"])
65
+ s.add_dependency(%q<rack-test>, [">= 0"])
66
+ s.add_dependency(%q<shoulda>, [">= 0"])
67
+ end
68
+ end
69
+
@@ -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,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mujhs-rack-gridfs
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
+ platform: ruby
12
+ authors:
13
+ - Blake Carlson
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-03-10 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rack
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: mongo
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - "="
42
+ - !ruby/object:Gem::Version
43
+ hash: 85
44
+ segments:
45
+ - 0
46
+ - 19
47
+ - 3
48
+ version: 0.19.3
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: mocha
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - "="
58
+ - !ruby/object:Gem::Version
59
+ hash: 51
60
+ segments:
61
+ - 0
62
+ - 9
63
+ - 4
64
+ version: 0.9.4
65
+ type: :development
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: rack-test
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ type: :development
80
+ version_requirements: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ name: shoulda
83
+ prerelease: false
84
+ requirement: &id005 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ type: :development
94
+ version_requirements: *id005
95
+ description:
96
+ email: blake@coin-operated.net
97
+ executables: []
98
+
99
+ extensions: []
100
+
101
+ extra_rdoc_files:
102
+ - LICENSE
103
+ - README.rdoc
104
+ files:
105
+ - .gitignore
106
+ - LICENSE
107
+ - README.rdoc
108
+ - Rakefile
109
+ - VERSION
110
+ - example/gridfs_server.rb
111
+ - lib/rack/gridfs.rb
112
+ - mujhs-rack-gridfs.gemspec
113
+ - test/artifacts/3wolfmoon.jpg
114
+ - test/artifacts/test.html
115
+ - test/artifacts/test.txt
116
+ - test/gridfs_test.rb
117
+ - test/test_helper.rb
118
+ has_rdoc: true
119
+ homepage: http://github.com/skinandbones/rack-gridfs
120
+ licenses: []
121
+
122
+ post_install_message:
123
+ rdoc_options:
124
+ - --charset=UTF-8
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ hash: 3
133
+ segments:
134
+ - 0
135
+ version: "0"
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ hash: 3
142
+ segments:
143
+ - 0
144
+ version: "0"
145
+ requirements: []
146
+
147
+ rubyforge_project: rack-gridfs
148
+ rubygems_version: 1.3.7
149
+ signing_key:
150
+ specification_version: 3
151
+ summary: Rack middleware for creating HTTP endpoints for files stored in MongoDB's GridFS
152
+ test_files:
153
+ - test/gridfs_test.rb
154
+ - test/test_helper.rb