jnunemaker-rack-gridfs 0.3.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,65 @@
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
+ gem.rubyforge_project = "rack-gridfs"
13
+
14
+ gem.add_dependency('rack')
15
+ gem.add_dependency('mongo', '0.19.1')
16
+
17
+ gem.add_development_dependency('mocha', '0.9.4')
18
+ gem.add_development_dependency('rack-test')
19
+ gem.add_development_dependency('shoulda')
20
+ end
21
+ Jeweler::GemcutterTasks.new
22
+ Jeweler::RubyforgeTasks.new do |rubyforge|
23
+ rubyforge.doc_task = "rdoc"
24
+ end
25
+ rescue LoadError
26
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
27
+ end
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/*_test.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ begin
37
+ require 'rcov/rcovtask'
38
+ Rcov::RcovTask.new do |test|
39
+ test.libs << 'test'
40
+ test.pattern = 'test/**/*_test.rb'
41
+ test.verbose = true
42
+ end
43
+ rescue LoadError
44
+ task :rcov do
45
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
46
+ end
47
+ end
48
+
49
+ task :test => :check_dependencies
50
+
51
+ task :default => :test
52
+
53
+ require 'rake/rdoctask'
54
+ Rake::RDocTask.new do |rdoc|
55
+ if File.exist?('VERSION')
56
+ version = File.read('VERSION')
57
+ else
58
+ version = ""
59
+ end
60
+
61
+ rdoc.rdoc_dir = 'rdoc'
62
+ rdoc.title = "Rack::GridFS #{version}"
63
+ rdoc.rdoc_files.include('README*')
64
+ rdoc.rdoc_files.include('lib/**/*.rb')
65
+ 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{rack-gridfs}
8
+ s.version = "0.2.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-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,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jnunemaker-rack-gridfs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Blake Carlson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-03-10 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: mongo
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.19.1
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: mocha
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.9.4
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: rack-test
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ - !ruby/object:Gem::Dependency
56
+ name: shoulda
57
+ type: :development
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ description:
66
+ email: blake@coin-operated.net
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files:
72
+ - LICENSE
73
+ - README.rdoc
74
+ files:
75
+ - .gitignore
76
+ - LICENSE
77
+ - README.rdoc
78
+ - Rakefile
79
+ - VERSION
80
+ - example/gridfs_server.rb
81
+ - lib/rack/gridfs.rb
82
+ - rack-gridfs.gemspec
83
+ - test/artifacts/3wolfmoon.jpg
84
+ - test/artifacts/test.html
85
+ - test/artifacts/test.txt
86
+ - test/gridfs_test.rb
87
+ - test/test_helper.rb
88
+ has_rdoc: true
89
+ homepage: http://github.com/skinandbones/rack-gridfs
90
+ licenses: []
91
+
92
+ post_install_message:
93
+ rdoc_options:
94
+ - --charset=UTF-8
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: "0"
102
+ version:
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: "0"
108
+ version:
109
+ requirements: []
110
+
111
+ rubyforge_project: rack-gridfs
112
+ rubygems_version: 1.3.5
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Rack middleware for creating HTTP endpoints for files stored in MongoDB's GridFS
116
+ test_files:
117
+ - test/gridfs_test.rb
118
+ - test/test_helper.rb