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


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cloudfront_asset_host.rb', line 78

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.to_s
      host = "http://#{host}"
    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}" if CloudfrontAssetHost.plain_prefix.present?
    end
  else
    host << "/#{CloudfrontAssetHost.plain_prefix}" if CloudfrontAssetHost.plain_prefix.present?
  end

  host
end

.bucket_hostObject


111
112
113
# File 'lib/cloudfront_asset_host.rb', line 111

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

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

Yields:

  • (_self)

Yield Parameters:


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cloudfront_asset_host.rb', line 51

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.asset_dirs = %w(images javascripts stylesheets)
  self.exclude_pattern = nil

  self.gzip            = true
  self.gzip_extensions = %w(js css)
  self.gzip_prefix     = "gz"

  self.plain_prefix = ""

  self.image_extensions = %w(jpg jpeg gif png)

  yield(self)

  if properly_configured?
    enable!
  end
end

.css?(path) ⇒ Boolean

Returns:

  • (Boolean)

137
138
139
# File 'lib/cloudfront_asset_host.rb', line 137

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

.disable_cdn_for_source?(source) ⇒ Boolean

Returns:

  • (Boolean)

141
142
143
# File 'lib/cloudfront_asset_host.rb', line 141

def disable_cdn_for_source?(source)
  source.match(exclude_pattern) if exclude_pattern.present?
end

.enable!Object


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

def enable!
  if enabled
    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
end

.gzip_allowed_for_source?(source) ⇒ Boolean

Returns:

  • (Boolean)

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

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

.image?(path) ⇒ Boolean

Returns:

  • (Boolean)

132
133
134
135
# File 'lib/cloudfront_asset_host.rb', line 132

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

.key_for_path(path) ⇒ Object


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

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