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.



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

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.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/vanagon/component/source/rewrite.rb', line 77

def parse_and_rewrite(uri)
  return uri if rewrite_rules.empty?
  if !!uri.match(/^git:http/)
    VanagonLogger.info "      `fustigit` parsing doesn't get along with how we specify the source\n      type by prefixing `git`. As `rewrite_rules` are deprecated, we'll\n      replace `git:http` with `http` in your uri. At some point this will\n      break.\n    HERE\n    uri.sub!(/^git:http/, 'http')\n  end\n  url = URI.parse(uri)\n  return url unless url.scheme\n  rewrite(url.to_s, url.scheme)\nend\n".undent

.proc_rewrite(rule, url) ⇒ Object



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

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.



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

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

.rewrite(url, protocol) ⇒ Object



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

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



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

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