Module: CloudfrontAssetHost

Defined in:
lib/cloudfront_asset_host.rb,
lib/cloudfront_asset_host/uploader.rb,
lib/cloudfront_asset_host/css_rewriter.rb

Defined Under Namespace

Modules: CssRewriter, Uploader

Class Method Summary collapse

Class Method Details

.asset_host(source = nil, request = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cloudfront_asset_host.rb', line 66

def asset_host(source = nil, request = nil)
  if cname.present?
    if cname.is_a?(Proc) 
      host = cname.call(source, request)
    else
      host = (cname =~ /%d/) ? cname % (source.hash % 4) : "#{cname}"
    end
  else
    host = "http://#{self.bucket_host}"
  end

  if source && request && CloudfrontAssetHost.gzip
    gzip_allowed  = CloudfrontAssetHost.gzip_allowed_for_source?(source)

    # Only Netscape 4 does not support gzip
    # IE masquerades as Netscape 4, so we check that as well
    user_agent = request.headers['User-Agent'].to_s
    gzip_accepted = !(user_agent =~ /^Mozilla\/4/) || user_agent =~ /\bMSIE/
    gzip_accepted &&= request.headers['Accept-Encoding'].to_s.include?('gzip')

    if gzip_accepted && gzip_allowed
      host << "/#{CloudfrontAssetHost.gzip_prefix}"
    else
      host << "/#{CloudfrontAssetHost.plain_prefix}"
    end
  else
    host << "/#{CloudfrontAssetHost.plain_prefix}"
  end

  host
end

.bucket_hostObject



98
99
100
# File 'lib/cloudfront_asset_host.rb', line 98

def bucket_host
  "#{self.bucket}.s3.amazonaws.com"
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cloudfront_asset_host.rb', line 44

def configure
  # default configuration
  self.bucket     = nil
  self.cname      = nil
  self.key_prefix = ""
  self.s3_config  = "#{RAILS_ROOT}/config/s3.yml"
  self.s3_logging = false
  self.enabled    = false

  self.gzip            = true
  self.gzip_extensions = %w(js css)
  self.gzip_prefix     = "gz"
  
  self.image_extensions = %w(jpg jpeg gif png)

  yield(self)

  if properly_configured? && enabled
    enable!
  end
end

.css?(path) ⇒ Boolean



128
129
130
# File 'lib/cloudfront_asset_host.rb', line 128

def css?(path)
  File.extname(path) == '.css'
end

.disable!Object



108
109
110
111
112
# File 'lib/cloudfront_asset_host.rb', line 108

def disable!
  ActionController::Base.asset_host = nil
  ActionView::Helpers::AssetTagHelper.send(:alias_method, :rewrite_asset_path, :rewrite_asset_path_without_cloudfront)
  ActionView::Helpers::AssetTagHelper.send(:alias_method, :rails_asset_id, :rails_asset_id_without_cloudfront)
end

.enable!Object



102
103
104
105
106
# File 'lib/cloudfront_asset_host.rb', line 102

def enable!
  ActionController::Base.asset_host = Proc.new { |source, request| CloudfrontAssetHost.asset_host(source, request) }
  ActionView::Helpers::AssetTagHelper.send(:alias_method_chain, :rewrite_asset_path, :cloudfront)
  ActionView::Helpers::AssetTagHelper.send(:alias_method_chain, :rails_asset_id, :cloudfront)
end

.gzip_allowed_for_source?(source) ⇒ Boolean



118
119
120
121
# File 'lib/cloudfront_asset_host.rb', line 118

def gzip_allowed_for_source?(source)
  extension = source.split('.').last
  CloudfrontAssetHost.gzip_extensions.include?(extension)
end

.image?(path) ⇒ Boolean



123
124
125
126
# File 'lib/cloudfront_asset_host.rb', line 123

def image?(path)
  extension = path.split('.').last
  CloudfrontAssetHost.image_extensions.include?(extension)
end

.key_for_path(path) ⇒ Object



114
115
116
# File 'lib/cloudfront_asset_host.rb', line 114

def key_for_path(path)
  key_prefix + md5sum(path)[0..8]
end