Class: Kiosk::Cdn

Inherits:
Object
  • Object
show all
Defined in:
lib/kiosk/cdn.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Cdn

Returns a new instance of Cdn.



5
6
7
8
9
10
11
12
# File 'lib/kiosk/cdn.rb', line 5

def initialize(config = {})
  @host = config['host']

  unless @host.nil? || @host.empty?
    @uri = URI.parse(@host)
    @host_only = @uri.scheme.nil? && @uri.host.nil?
  end
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



3
4
5
# File 'lib/kiosk/cdn.rb', line 3

def host
  @host
end

Instance Method Details

#configured?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/kiosk/cdn.rb', line 14

def configured?
  !@uri.nil?
end

#rewrite_node(node) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/kiosk/cdn.rb', line 18

def rewrite_node(node)
  if configured?
    node.uri_attribute.content = rewrite_uri(URI.parse(node.uri_attribute.content)).to_s
  end
rescue URI::InvalidURIError
  nil
end

#rewrite_uri(uri) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kiosk/cdn.rb', line 26

def rewrite_uri(uri)
  if configured?
    if @host_only
      uri.host = @host
    else
      uri.scheme = @uri.scheme
      uri.host = @uri.host
      uri.path = "#{@uri.path.sub(/\/$/, '')}#{uri.path}" unless @uri.path.empty? || @uri.path == '/'
    end
  end
  uri
end