rack-s3 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rack-s3.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rack-s3 (0.0.1)
5
+ aws-s3
6
+ rack
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ aws-s3 (0.6.2)
12
+ builder
13
+ mime-types
14
+ xml-simple
15
+ builder (3.0.0)
16
+ mime-types (1.16)
17
+ rack (1.2.1)
18
+ xml-simple (1.0.13)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ aws-s3
25
+ rack
26
+ rack-s3!
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011 Larry Marburger
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # Rack::S3
2
+
3
+ Serve files from an S3 bucket as if they were local assets similar to
4
+ Rack::Static. Stand up behind Rack::Thumb for fame and notoriety.
5
+
6
+
7
+ ## Usage
8
+
9
+ require 'myapp'
10
+ require 'rack/thumb'
11
+ require 'rack/s3'
12
+
13
+ use Rack::Thumb
14
+ use Rack::S3
15
+
16
+ run MyApp.new
17
+
18
+
19
+ ## Copyright
20
+
21
+ Copyright (c) 2011 Larry Marburger. See [LICENSE] for details.
22
+
23
+
24
+ [LICENSE]: http://github.com/lmarburger/rack-s3/blob/master/LICENSE
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ Rake::TestTask.new('test') do |t|
7
+ t.libs << 'test'
8
+ t.pattern = 'test/*_test.rb'
9
+ t.verbose = true
10
+ end
11
+
12
+ desc 'Default: run tests'
13
+ task :default => 'test'
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class S3
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/lib/rack/s3.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'rack'
2
+ require 'aws/s3'
3
+
4
+ module Rack
5
+ class S3
6
+
7
+ def initialize(app, options={})
8
+ @app = app
9
+ @prefix = options[:prefix]
10
+ @bucket = options[:bucket]
11
+
12
+ AWS::S3::Base.establish_connection!(
13
+ :access_key_id => options[:access_key_id],
14
+ :secret_access_key => options[:secret_access_key])
15
+ end
16
+
17
+ def call(env)
18
+ dup._call env
19
+ end
20
+
21
+ def _call(env)
22
+ @env = env
23
+
24
+ if can_serve?
25
+ [ 200, headers, object.value ]
26
+ else
27
+ @app.call env
28
+ end
29
+
30
+ rescue AWS::S3::NoSuchKey
31
+ not_found
32
+ end
33
+
34
+ def headers
35
+ about = object.about
36
+
37
+ { 'Content-Type' => about['content-type'],
38
+ 'Content-Length' => about['content-length'],
39
+ 'Etag' => about['etag'],
40
+ 'Last-Modified' => about['last-modified'],
41
+ 'Cache-Control' => 'public; max-age=2592000' # cache image for a month
42
+ }
43
+ end
44
+
45
+ def can_serve?
46
+ path_info.index(@prefix) == 0
47
+ end
48
+
49
+ def path_info
50
+ @env['PATH_INFO']
51
+ end
52
+
53
+ def path
54
+ path_info.split('/').last
55
+ end
56
+
57
+ def object
58
+ @object ||= AWS::S3::S3Object.find(path, @bucket)
59
+ end
60
+
61
+ def not_found
62
+ body = "File not found: #{ path_info }\n"
63
+
64
+ [ 404,
65
+ { 'Content-Type' => "text/plain",
66
+ 'Content-Length' => body.size.to_s },
67
+ [ body ]]
68
+ end
69
+
70
+ end
71
+ end
data/rack-s3.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rack/s3/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rack-s3"
7
+ s.version = Rack::S3::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Larry Marburger"]
10
+ s.email = ["larry@marburger.cc"]
11
+ s.homepage = "http://developmentastic.com"
12
+ s.summary = %q{A Rack::Static like middleware for serving assets from S3}
13
+ s.description = %q{Serve files from an S3 bucket as if they were local assets similar to Rack::Static.}
14
+
15
+ s.add_dependency 'aws-s3'
16
+ s.add_dependency 'rack'
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ end
@@ -0,0 +1,50 @@
1
+ require 'test_helper'
2
+ require 'rack/mock'
3
+
4
+ class DummyApp
5
+ def call(env)
6
+ [ 200, {}, [ "Hello World" ]]
7
+ end
8
+ end
9
+
10
+ class RackS3Test < Test::Unit::TestCase
11
+
12
+ def app
13
+ # HACK: Use your S3 credentials to run the test suite. Should use a
14
+ # recording library like VCR.
15
+ options = { :prefix => '/s3',
16
+ :bucket => 'rack-s3',
17
+ :access_key_id => 'insert_access_key_here',
18
+ :secret_access_key => 'insert_secret_here' }
19
+
20
+ Rack::MockRequest.new Rack::S3.new(DummyApp.new, options)
21
+ end
22
+
23
+ def test_serve_files_from_s3
24
+ response = app.get '/s3/clear.png'
25
+
26
+ assert_equal 200, response.status
27
+ assert_equal 'public; max-age=2592000', response.headers['Cache-Control']
28
+ assert_not_nil response.body
29
+
30
+ %w(Content-Type Last-Modified Last-Modified Etag).each do |header|
31
+ assert_not_nil response.headers[header]
32
+ end
33
+ end
34
+
35
+ def test_ignore_requests_outside_of_prefix
36
+ response = app.get '/ignore_me'
37
+
38
+ assert_equal 'Hello World', response.body
39
+ end
40
+
41
+ def test_return_not_found_for_nonexistent_files
42
+ response = app.get '/s3/nil.png'
43
+
44
+ assert_equal 404, response.status
45
+ assert_equal "File not found: /s3/nil.png\n", response.body
46
+ assert_equal 'text/plain', response.headers['Content-Type']
47
+ assert_equal '28', response.headers['Content-Length']
48
+ end
49
+
50
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ require 'bundler/setup'
5
+ Bundler.require :development
6
+
7
+ require 'rack/s3'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-s3
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Larry Marburger
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-21 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: aws-s3
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: rack
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ description: Serve files from an S3 bucket as if they were local assets similar to Rack::Static.
50
+ email:
51
+ - larry@marburger.cc
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - Gemfile
61
+ - Gemfile.lock
62
+ - LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/rack/s3.rb
66
+ - lib/rack/s3/version.rb
67
+ - rack-s3.gemspec
68
+ - test/rack_s3_test.rb
69
+ - test/test_helper.rb
70
+ has_rdoc: true
71
+ homepage: http://developmentastic.com
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options: []
76
+
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.4.1
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: A Rack::Static like middleware for serving assets from S3
104
+ test_files:
105
+ - test/rack_s3_test.rb
106
+ - test/test_helper.rb