Class: Vanagon::Component::Source::Rewrite

Inherits:
Object
  • Object
show all
Defined in:
lib/vanagon/component/source/rewrite.rb

Overview

This class has been extracted from Vanagon::Component::Source for the sake of isolation and in service of its pending removal. Rewrite rules should be considered deprecated. The removal will be carried out before Vanagon 1.0.0 is released.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.rewrite_rulesObject (readonly)

Returns the value of attribute rewrite_rules.



14
15
16
# File 'lib/vanagon/component/source/rewrite.rb', line 14

def rewrite_rules
  @rewrite_rules
end

Class Method Details

.parse_and_rewrite(uri) ⇒ Object

Deprecated.

Please use the component DSL method #mirror(<URI>) instead. This method will be removed before Vanagon 1.0.0.



76
77
78
79
80
# File 'lib/vanagon/component/source/rewrite.rb', line 76

def parse_and_rewrite(uri)
  url = URI.parse(uri)
  return url unless url.scheme
  rewrite(url.to_s, url.scheme)
end

.proc_rewrite(rule, url) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/vanagon/component/source/rewrite.rb', line 55

def proc_rewrite(rule, url)
  if rule.arity == 1
    rule.call(url)
  else
    raise Vanagon::Error, "Unable to use provided rewrite rule. Expected Proc with one argument, Proc has #{rule.arity} arguments"
  end
end

.register_rewrite_rule(protocol, rule) ⇒ Object

Deprecated.

Please use the component DSL method #mirror(<URI>) instead. This method will be removed before Vanagon 1.0.0.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vanagon/component/source/rewrite.rb', line 18

def register_rewrite_rule(protocol, rule)
  warn <<-HERE.undent
    rewrite rule support is deprecated and will be removed before Vanagon 1.0.0.
    Rewritten URLs will be automatically converted into mirror URLs for now but
    please use the component DSL method '#mirror url' to define new mirror URL
    sources for a given component.
  HERE
  if rule.is_a?(String) || rule.is_a?(Proc)
    if Vanagon::Component::Source::SUPPORTED_PROTOCOLS.include?(protocol)
      @rewrite_rules[protocol] = rule
    else
      raise Vanagon::Error, "#{protocol} is not a supported protocol for rewriting"
    end
  else
    raise Vanagon::Error, "String or Proc is required as a rewrite_rule."
  end
end

.rewrite(url, protocol) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vanagon/component/source/rewrite.rb', line 36

def rewrite(url, protocol)
  # Vanagon did not originally distinguish between http and https
  # when looking up rewrite rules; this is no longer true, but it
  # means that we should try to preserve old, dumb behavior until
  # the rewrite engine is removed.
  return rewrite(url, "http") if protocol == "https"

  rule = @rewrite_rules[protocol]
  if rule
    if rule.is_a?(Proc)
      return proc_rewrite(rule, url)
    elsif rule.is_a?(String)
      return string_rewrite(rule, url)
    end
  end

  return url
end

.string_rewrite(rule, original_url) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/vanagon/component/source/rewrite.rb', line 63

def string_rewrite(rule, original_url)
  url = original_url.to_s
  target_match = url.match(/.*\/([^\/]*)$/)
  if target_match
    target = target_match[1]
    return File.join(rule, target)
  else
    raise Vanagon::Error, "Unable to apply url rewrite to '#{url}', expected to find at least one '/' in the url."
  end
end