asset_hash 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,8 +1,14 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- gem "rails", "3.0.5"
4
- gem "capybara", ">= 0.4.0"
5
- gem "sqlite3"
3
+ group :development do
4
+ gem 'aws-s3'
5
+ end
6
+
7
+ group :test do
8
+ gem "rails", "3.0.5"
9
+ gem "capybara", ">= 0.4.0"
10
+ gem "sqlite3"
11
+ end
6
12
 
7
13
  # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
8
14
  # gem 'ruby-debug'
data/README.textile CHANGED
@@ -17,7 +17,13 @@ Add the gem to your Gemfile
17
17
 
18
18
  Set up a CloudFront distribution with a custom origin that points to your website's URL.
19
19
 
20
- Check the following website as well as the Amazon CloudFront documentation for help on setting up a custom origin CloudFront distribution:
20
+ There is a rake task included in the gem that guides you through creating CloudFront distribution easily, to run it just the following command from your app directory.
21
+
22
+ <code>rake asset:hash:create_cloudfront_distribution</code>
23
+
24
+ It is an interactive rake task that will ask you for your Amazon credentials and the domain you want to point the custom origin to.
25
+
26
+ Alternatively you can check the following website as well as the Amazon CloudFront documentation for help on setting up a custom origin CloudFront distribution:
21
27
  http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?DistributionConfigDatatype.html#CustomOriginChildElements
22
28
 
23
29
  Configure your environment file to use this CloudFront distribution as your asset host and the hashed filenames in the helpers.
data/lib/asset_hash.rb CHANGED
@@ -55,14 +55,17 @@ module AssetHash
55
55
 
56
56
  def self.fingerprint(path)
57
57
  path = File.join path_prefix, path unless path =~ /#{path_prefix}/
58
- d = Digest::MD5.file(path).hexdigest
59
- path = path.gsub(path_prefix, '')
60
- extension = (path =~ /\.gz$/ ? File.extname(File.basename(path, ".gz")) + ".gz" : File.extname(path))
61
- File.join File.dirname(path), "#{File.basename(path, extension)}-#{d}#{extension}"
58
+ begin
59
+ d = Digest::MD5.file(path).hexdigest
60
+ path = path.gsub(path_prefix, '')
61
+ extension = (path =~ /\.gz$/ ? File.extname(File.basename(path, ".gz")) + ".gz" : File.extname(path))
62
+ File.join File.dirname(path), "#{File.basename(path, extension)}-#{d}#{extension}"
63
+ rescue Errno::ENOENT
64
+ return original_path(path)#path.gsub(path_prefix, '')
65
+ end
62
66
  end
63
67
 
64
68
  def self.original_path(path)
65
- path = File.join path_prefix, path unless path =~ /#{path_prefix}/
66
69
  path = path.gsub(path_prefix, '')
67
70
  extension = (path =~ /\.gz$/ ? File.extname(File.basename(path, ".gz")) + ".gz" : File.extname(path))
68
71
  File.join File.dirname(path), "#{File.basename(path, extension)}#{extension}"
@@ -6,5 +6,55 @@ namespace :asset do
6
6
  task :generate do
7
7
  AssetHash.process!
8
8
  end
9
+
10
+ desc "Create a custom origin cloudfront distribution"
11
+ task :create_cloudfront_distribution do
12
+ begin
13
+ require 'aws/s3'
14
+ require 'time'
15
+ require 'digest/sha1'
16
+ require 'net/https'
17
+ require 'base64'
18
+ STDOUT.puts "Enter your AWS Access ID (Don't forget to sign up for Cloudfront)"
19
+ access_key_id = STDIN.gets.strip
20
+ STDOUT.puts "Enter your AWS Secret Key"
21
+ secret_access_key = STDIN.gets.strip
22
+ STDOUT.puts "Enter the url of the domain to distribute on cloudfront (eg. google.com)"
23
+ cloudfront_distribution_url = STDIN.gets.strip
24
+ STDOUT.puts "Enter a description for your cloudfront distribution"
25
+ cloudfront_distribution_description = STDIN.gets.strip
26
+ AWS::S3::Base.establish_connection!(
27
+ :access_key_id => access_key_id,
28
+ :secret_access_key => secret_access_key
29
+ )
30
+ digest = OpenSSL::Digest.new('sha1')
31
+ digest = OpenSSL::HMAC.digest(digest, secret_access_key, date = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S %Z"))
32
+ uri = URI.parse("https://cloudfront.amazonaws.com/2010-11-01/distribution")
33
+ req = Net::HTTP::Post.new(uri.path)
34
+ req.initialize_http_header({
35
+ 'x-amz-date' => date,
36
+ 'Content-Type' => 'text/xml',
37
+ 'Authorization' => "AWS %s:%s" % [access_key_id, Base64.encode64(digest)]
38
+ })
39
+ req.body = <<EOF
40
+ <DistributionConfig xmlns="http://cloudfront.amazonaws.com/doc/2010-11-01/">
41
+ <CustomOrigin>
42
+ <DNSName>#{cloudfront_distribution_url}</DNSName>
43
+ <OriginProtocolPolicy>http-only</OriginProtocolPolicy>
44
+ </CustomOrigin>
45
+ <Comment>#{cloudfront_distribution_description}</Comment>
46
+ <Enabled>true</Enabled>
47
+ <CallerReference>#{Time.now.utc.to_i}</CallerReference>
48
+ </DistributionConfig>
49
+ EOF
50
+ http = Net::HTTP.new(uri.host, uri.port)
51
+ http.use_ssl = true
52
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
53
+ res = http.request(req)
54
+ STDOUT.puts res.code == '201' ? "Distribution created: #{res.body.match(/\<Id\>(.+)\<\/Id\>/)[1]}" : "Distribution failed: #{res.body}"
55
+ rescue LoadError => e
56
+ STDERR.puts "Could not find the aws-s3 gem, Run `gem install aws-s3` to install s3"
57
+ end
58
+ end
9
59
  end
10
60
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: asset_hash
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Donald Piret
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-29 00:00:00 +08:00
13
+ date: 2011-04-28 00:00:00 +08:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency