Module: CDN

Defined in:
lib/cdn.rb,
lib/cdn/version.rb,
lib/cdn/configuration.rb,
lib/cdn/providers/choopa.rb,
lib/cdn/providers/swiftwill.rb,
lib/cdn/providers/cloudfront.rb

Overview

CDN helper methods.

Defined Under Namespace

Classes: Choopa, Cloudfront, Configuration, Swiftwill

Constant Summary collapse

VERSION =
'0.1.3'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cdn_providerObject



25
26
27
# File 'lib/cdn.rb', line 25

def self.cdn_provider
  "CDN::#{self.configuration.provider}".constantize
end

.configurationObject



16
17
18
# File 'lib/cdn.rb', line 16

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:

  • (config)


20
21
22
23
# File 'lib/cdn.rb', line 20

def self.configure
  config = configuration
  yield(config)
end

Instance Method Details

#cdn_large_url(path, options = {}) ⇒ Object

Alias for cdn_url with :cdn_type set to large.

Returns a url.



65
66
67
# File 'lib/cdn.rb', line 65

def cdn_large_url(path, options = {})
  cdn_url(path, { :token => { :cdn_type => :large }}.merge(options))
end

#cdn_small_url(path, options = {}) ⇒ Object

Alias for cdn_url with :cdn_type set to small.

Returns a url.



73
74
75
76
77
# File 'lib/cdn.rb', line 73

def cdn_small_url(path, options = {})
  options[:domain] ||= CDN.configuration.small_domain
  options[:domain] ||= CDN.configuration.domain
  cdn_url(path, { token: { cdn_type: :small }}.merge(options))
end

#cdn_url(path, options = {}) ⇒ Object

Generates a CDN url for the path and options given. Uses the set CDN provider given in the configuration.

path - Relative path for file. options - Hash of options given to the generator.

Also takes a :token to customize the token generator.

Returns a url.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cdn.rb', line 38

def cdn_url(path, options = {})
  return path unless CDN.configuration.enabled

  options[:token] ||= {}
  options[:domain] ||= CDN.configuration.domain
  path = CDN.configuration.path_processor.call(path) if CDN.configuration.path_processor

  url = CDN.cdn_provider.generate_url(
    protocol: options[:protocol] || CDN.configuration.protocol || :http,
    domain: options[:domain],
    path: URI.parse(path).path,
    token: CDN.cdn_provider.generate_token(path, options))

  # Preserving any existing query params.
  if query = URI.parse(path).query
    query = CGI.parse(query)
    query = query.merge(CGI.parse(url.query.to_s))
    url.query = query.collect { |k,v| (v.empty?) ? k : "#{k}=#{v[0]}" }.join("&")
  end

  url.to_s
end