asset_s3 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "mime-types", ">= 1.16"
4
+ gem "aws-s3", ">= 0.6.2"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.1.0"
10
+ gem "yard", "~> 0.6.0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.5.1"
13
+ gem "rcov", ">= 0"
14
+ end
@@ -0,0 +1,39 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ aws-s3 (0.6.2)
5
+ builder
6
+ mime-types
7
+ xml-simple
8
+ builder (3.0.0)
9
+ diff-lcs (1.1.2)
10
+ git (1.2.5)
11
+ jeweler (1.5.1)
12
+ bundler (~> 1.0.0)
13
+ git (>= 1.2.5)
14
+ rake
15
+ mime-types (1.16)
16
+ rake (0.8.7)
17
+ rcov (0.9.9)
18
+ rspec (2.1.0)
19
+ rspec-core (~> 2.1.0)
20
+ rspec-expectations (~> 2.1.0)
21
+ rspec-mocks (~> 2.1.0)
22
+ rspec-core (2.1.0)
23
+ rspec-expectations (2.1.0)
24
+ diff-lcs (~> 1.1.2)
25
+ rspec-mocks (2.1.0)
26
+ xml-simple (1.0.12)
27
+ yard (0.6.3)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ aws-s3 (>= 0.6.2)
34
+ bundler (~> 1.0.0)
35
+ jeweler (~> 1.5.1)
36
+ mime-types (>= 1.16)
37
+ rcov
38
+ rspec (~> 2.1.0)
39
+ yard (~> 0.6.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Daniel Spangenberg
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,19 @@
1
+ = asset_s3
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to asset_s3
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Daniel Spangenberg. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "asset_s3"
16
+ gem.homepage = "https://github.com/neonlex/asset_s3"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Uploads static assets to Amaton S3}
19
+ gem.description = %Q{Rails 3 libary that uploads all static assets to amazon s3}
20
+ gem.email = "daniel.spangenberg@parcydo.com"
21
+ gem.authors = ["Daniel Spangenberg"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'yard'
43
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,74 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
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{asset_s3}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Daniel Spangenberg"]
12
+ s.date = %q{2010-12-11}
13
+ s.description = %q{Rails 3 libary that uploads all static assets to amazon s3}
14
+ s.email = %q{daniel.spangenberg@parcydo.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "asset_s3.gemspec",
29
+ "lib/asset_s3.rb",
30
+ "spec/asset_s3_spec.rb",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = %q{https://github.com/neonlex/asset_s3}
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.7}
37
+ s.summary = %q{Uploads static assets to Amaton S3}
38
+ s.test_files = [
39
+ "spec/asset_s3_spec.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
48
+ s.add_runtime_dependency(%q<mime-types>, [">= 1.16"])
49
+ s.add_runtime_dependency(%q<aws-s3>, [">= 0.6.2"])
50
+ s.add_development_dependency(%q<rspec>, ["~> 2.1.0"])
51
+ s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
52
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
53
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
54
+ s.add_development_dependency(%q<rcov>, [">= 0"])
55
+ else
56
+ s.add_dependency(%q<mime-types>, [">= 1.16"])
57
+ s.add_dependency(%q<aws-s3>, [">= 0.6.2"])
58
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
59
+ s.add_dependency(%q<yard>, ["~> 0.6.0"])
60
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
61
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
62
+ s.add_dependency(%q<rcov>, [">= 0"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<mime-types>, [">= 1.16"])
66
+ s.add_dependency(%q<aws-s3>, [">= 0.6.2"])
67
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
68
+ s.add_dependency(%q<yard>, ["~> 0.6.0"])
69
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
70
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
71
+ s.add_dependency(%q<rcov>, [">= 0"])
72
+ end
73
+ end
74
+
@@ -0,0 +1,114 @@
1
+ require 'mime/types'
2
+ require 'aws/s3'
3
+ require 'time'
4
+
5
+ module AssetS3
6
+ class Base
7
+ DEFAULT_ASSET_PATHS = ["favicon.ico", "images", "javascripts", "stylesheets"]
8
+ @@asset_paths = DEFAULT_ASSET_PATHS
9
+
10
+ def self.path_prefix
11
+ File.join(Rails.root, "public")
12
+ end
13
+
14
+ def self.asset_paths=(paths)
15
+ @@asset_paths = paths
16
+ end
17
+
18
+ def self.asset_paths
19
+ @@asset_paths
20
+ end
21
+
22
+ def self.absolute_path(path)
23
+ File.join(path_prefix, path)
24
+ end
25
+
26
+ def self.assets
27
+ asset_paths.inject([]) do |assets, path|
28
+ path = absolute_path(path)
29
+ assets << path if File.exists?(path) && !File.directory?(path)
30
+ assets += Dir.glob("#{path}/**/*").inject([]) do |m, file|
31
+ m << file unless File.directory?(file)
32
+ m
33
+ end
34
+ end
35
+ end
36
+
37
+ def self.fingerprint(path)
38
+ path.gsub(path_prefix, "")
39
+ end
40
+ end
41
+
42
+ class S3 < AssetS3::Base
43
+ DEFAULT_GZIP_TYPES = ["text/css", "application/javascript"]
44
+ @@gzip_types = DEFAULT_GZIP_TYPES
45
+
46
+ def self.gzip_types=(types)
47
+ @@gzip_types = types
48
+ end
49
+
50
+ def self.gzip_types
51
+ @@gzip_types
52
+ end
53
+
54
+ def self.s3_config
55
+ @@config ||= YAML.load_file(File.join(Rails.root, "config/asset_s3.yml"))[Rails.env] rescue nil || {}
56
+ end
57
+
58
+ def self.connect_to_s3
59
+ AWS::S3::Base.establish_connection!(
60
+ :access_key_id => s3_config["access_key_id"],
61
+ :secret_access_key => s3_config["secret_access_key"]
62
+ )
63
+ end
64
+
65
+ def self.cache_headers
66
+ {"Expires" => (Time.now + (60*60*24*365)).httpdate, "Cache-Control" => "public"} # 1 year expiry
67
+ end
68
+
69
+ def self.gzip_headers
70
+ {"Content-Encoding" => "gzip", "Vary" => "Accept-Encoding"}
71
+ end
72
+
73
+ def self.s3_permissions
74
+ :public_read
75
+ end
76
+
77
+ def self.s3_bucket
78
+ s3_config["bucket"]
79
+ end
80
+
81
+ def self.fingerprint(path)
82
+ super(path)
83
+ end
84
+
85
+ def self.upload(options={})
86
+ connect_to_s3
87
+ assets.each do |asset|
88
+ puts "asset_id: Uploading #{asset} as #{fingerprint(asset)}" if options[:debug]
89
+ mime_type = MIME::Types.of(asset).first.to_s
90
+
91
+ headers = {
92
+ :content_type => mime_type,
93
+ :access => s3_permissions,
94
+ }.merge(cache_headers)
95
+
96
+ if gzip_types.include?(mime_type)
97
+ data = `gzip -c #{asset}`
98
+ headers.merge!(gzip_headers)
99
+ else
100
+ data = File.read(asset)
101
+ end
102
+
103
+ puts "asset_id: headers: #{headers.inspect}" if options[:debug]
104
+
105
+ AWS::S3::S3Object.store(
106
+ fingerprint(asset),
107
+ data,
108
+ s3_bucket,
109
+ headers
110
+ ) unless options[:dry_run]
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "AssetS3" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'asset_s3'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,179 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: asset_s3
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Daniel Spangenberg
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-11 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: mime-types
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 16
30
+ version: "1.16"
31
+ type: :runtime
32
+ prerelease: false
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: aws-s3
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 6
44
+ - 2
45
+ version: 0.6.2
46
+ type: :runtime
47
+ prerelease: false
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 2
58
+ - 1
59
+ - 0
60
+ version: 2.1.0
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: *id003
64
+ - !ruby/object:Gem::Dependency
65
+ name: yard
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ~>
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ - 6
74
+ - 0
75
+ version: 0.6.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: *id004
79
+ - !ruby/object:Gem::Dependency
80
+ name: bundler
81
+ requirement: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ segments:
87
+ - 1
88
+ - 0
89
+ - 0
90
+ version: 1.0.0
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: *id005
94
+ - !ruby/object:Gem::Dependency
95
+ name: jeweler
96
+ requirement: &id006 !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ segments:
102
+ - 1
103
+ - 5
104
+ - 1
105
+ version: 1.5.1
106
+ type: :development
107
+ prerelease: false
108
+ version_requirements: *id006
109
+ - !ruby/object:Gem::Dependency
110
+ name: rcov
111
+ requirement: &id007 !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: *id007
122
+ description: Rails 3 libary that uploads all static assets to amazon s3
123
+ email: daniel.spangenberg@parcydo.com
124
+ executables: []
125
+
126
+ extensions: []
127
+
128
+ extra_rdoc_files:
129
+ - LICENSE.txt
130
+ - README.rdoc
131
+ files:
132
+ - .document
133
+ - .rspec
134
+ - Gemfile
135
+ - Gemfile.lock
136
+ - LICENSE.txt
137
+ - README.rdoc
138
+ - Rakefile
139
+ - VERSION
140
+ - asset_s3.gemspec
141
+ - lib/asset_s3.rb
142
+ - spec/asset_s3_spec.rb
143
+ - spec/spec_helper.rb
144
+ has_rdoc: true
145
+ homepage: https://github.com/neonlex/asset_s3
146
+ licenses:
147
+ - MIT
148
+ post_install_message:
149
+ rdoc_options: []
150
+
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ hash: -3071146034089066419
159
+ segments:
160
+ - 0
161
+ version: "0"
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ none: false
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ segments:
168
+ - 0
169
+ version: "0"
170
+ requirements: []
171
+
172
+ rubyforge_project:
173
+ rubygems_version: 1.3.7
174
+ signing_key:
175
+ specification_version: 3
176
+ summary: Uploads static assets to Amaton S3
177
+ test_files:
178
+ - spec/asset_s3_spec.rb
179
+ - spec/spec_helper.rb