Class: Rack::Grid
- Inherits:
-
Object
- Object
- Rack::Grid
- Defined in:
- lib/rack_grid.rb
Defined Under Namespace
Classes: ConnectionError
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
-
#call(env) ⇒ Object
Strip the _id out of the path.
- #db ⇒ Object
-
#grid_request(id) ⇒ Object
Get file from GridFS or return a 404.
-
#initialize(app, options = {}) ⇒ Grid
constructor
A new instance of Grid.
Constructor Details
#initialize(app, options = {}) ⇒ Grid
Returns a new instance of Grid.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rack_grid.rb', line 9 def initialize(app, = {}) opts = {} .each { |k,v| opts[k.to_s] = v } = { 'host' => 'localhost', 'prefix' => 'grid', 'port' => Mongo::Connection::DEFAULT_PORT }.merge(opts) @app = app @host = ['host'] @port = ['port'] @database = ['database'] @prefix = ['prefix'] @username = ['username'] @password = ['password'] @db = ['db'] @cache_control = ['cache_control'] end |
Instance Attribute Details
#database ⇒ Object (readonly)
Returns the value of attribute database.
7 8 9 |
# File 'lib/rack_grid.rb', line 7 def database @database end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
7 8 9 |
# File 'lib/rack_grid.rb', line 7 def host @host end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
7 8 9 |
# File 'lib/rack_grid.rb', line 7 def password @password end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
7 8 9 |
# File 'lib/rack_grid.rb', line 7 def port @port end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
7 8 9 |
# File 'lib/rack_grid.rb', line 7 def prefix @prefix end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
7 8 9 |
# File 'lib/rack_grid.rb', line 7 def username @username end |
Instance Method Details
#call(env) ⇒ Object
Strip the _id out of the path. This allows the user to send something like /grid/4ba69fde8c8f369a6e000003/filename.jpg to find the file with an id of 4ba69fde8c8f369a6e000003.
40 41 42 43 44 45 46 47 48 |
# File 'lib/rack_grid.rb', line 40 def call(env) @env = env request = Rack::Request.new(@env) if request.path_info =~ /^\/#{prefix}\/(\w+).*$/ grid_request($1) else @app.call(env) end end |
#db ⇒ Object
30 31 32 33 34 |
# File 'lib/rack_grid.rb', line 30 def db @db = @db.call() if Proc === @db connect! unless @db @db end |
#grid_request(id) ⇒ Object
Get file from GridFS or return a 404
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rack_grid.rb', line 52 def grid_request(id) file = Mongo::Grid.new(db).get(BSON::ObjectId.from_string(id)) etag, last_modified = file.instance_variable_get(:@md5), Time.at( file.upload_date.to_i ) headers = { 'ETag' => "\"#{etag}\"", 'Last-Modified' => last_modified.httpdate, 'Cache-Control' => cache_control_header, } if not_modified?( etag, last_modified ) [304, headers, ['Not Modified']] else [200, headers.update('Content-Type' => file.content_type), [file.read]] end rescue Mongo::GridError, BSON::InvalidObjectId [404, {'Content-Type' => 'text/plain'}, ['File not found.']] end |