Class: DevDNSd::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/devdnsd/rule.rb

Overview

This class encapsulate a rule for matching an hostname.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(match = /.+/, reply = "127.0.0.1", type = :A, options = {}, &block) ⇒ Rule

Creates a new rule.

Parameters:

  • match (String|Regexp) (defaults to: /.+/)

    The pattern to match.

  • reply (String) (defaults to: "127.0.0.1")

    The IP or hostname to reply back to the client.

  • type (Symbol) (defaults to: :A)

    The type of request to match.

  • options (Hash) (defaults to: {})

    A list of options for the request.

  • block (Proc)

    An optional block to compute the reply instead of using the reply parameter.

Raises:

See Also:



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/devdnsd/rule.rb', line 39

def initialize(match = /.+/, reply = "127.0.0.1", type = :A, options = {}, &block)
  reply ||= "127.0.0.1"
  type ||= :A
  @match = match
  @type = type
  @reply = block.blank? ? reply : nil
  @options = options
  @block = block

  raise(DevDNSd::Errors::InvalidRule.new("You must specify at least a rule and a host (also via a block). Optionally you can add a record type (default: A) and the options.")) if @reply.blank? && @block.nil?
  raise(DevDNSd::Errors::InvalidRule.new("You can only use hashs for options.")) if !@options.is_a?(::Hash)
end

Instance Attribute Details

#blockObject

An optional block to compute the reply instead of using the reply parameter.

See Also:



29
30
31
# File 'lib/devdnsd/rule.rb', line 29

def block
  @block
end

#matchObject

The pattern to match. Default: /.+/.



11
12
13
# File 'lib/devdnsd/rule.rb', line 11

def match
  @match
end

#optionsObject

A list of options for the request. Default is an empty hash.



24
25
26
# File 'lib/devdnsd/rule.rb', line 24

def options
  @options
end

#replyObject

The IP or hostname to reply back to the client. Default: 127.0.0.1.

See Also:



21
22
23
# File 'lib/devdnsd/rule.rb', line 21

def reply
  @reply
end

#typeObject

The type of request to match. Default: :A.

See Also:



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

def type
  @type
end

Class Method Details

.create(match, reply_or_type = nil, type = nil, options = {}, &block) ⇒ Object

Creates a new rule.

Parameters:

  • match (String|Regexp)

    The pattern to match.

  • reply_or_type (String|Symbol) (defaults to: nil)

    The IP or hostname to reply back to the client (or the type of request to match, if a block is provided).

  • type (Symbol) (defaults to: nil)

    The type of request to match. This is ignored if a block is provided.

  • options (Hash) (defaults to: {})

    A list of options for the request.

  • block (Proc)

    An optional block to compute the reply instead of using the reply_or_type parameter. In this case reply_or_type is used for the type of the request and type is ignored.

Raises:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/devdnsd/rule.rb', line 89

def self.create(match, reply_or_type = nil, type = nil, options = {}, &block)
  raise(DevDNSd::Errors::InvalidRule.new("You must specify at least a rule and a host (also via a block). Optionally you can add a record type (default: A) and the options.")) if reply_or_type.blank? && block.nil?
  raise(DevDNSd::Errors::InvalidRule.new("You can only use hashs for options.")) if !options.is_a?(::Hash)

  rv = self.new(match)
  rv.options = options

  if block.present? then # reply_or_type acts like a type, type is ignored
    rv.type = reply_or_type || :A
    rv.reply = nil
    rv.block = block
  else # reply_or_type acts like a reply
    rv.reply = reply_or_type || "127.0.0.1"
    rv.type = type || :A
  end

  rv
end

.resource_class_to_symbol(klass) ⇒ Symbol

Converts a class to the correspondent symbol.

Parameters:

  • klass (Class)

    The class to convert.

Returns:

  • (Symbol)

    The symbol representation of the class.



112
113
114
# File 'lib/devdnsd/rule.rb', line 112

def self.resource_class_to_symbol(klass)
  klass.to_s.gsub(/(.+::)?(.+)/, "\\2").to_sym
end

.symbol_to_resource_class(symbol) ⇒ Symbol

Converts a symbol to the correspondent DNS resource class.

Parameters:

  • symbol (Symbol)

    The symbol to convert.

Returns:

  • (Symbol)

    The class associated to the symbol.



120
121
122
123
124
125
126
127
128
# File 'lib/devdnsd/rule.rb', line 120

def self.symbol_to_resource_class(symbol)
  symbol = symbol.to_s.upcase

  begin
    "Resolv::DNS::Resource::IN::#{symbol}".constantize
  rescue ::NameError
    raise(DevDNSd::Errors::InvalidRule.new("Invalid resource class #{symbol}."))
  end
end

Instance Method Details

#has_block?Boolean

Checks if the rule is a regexp.

Returns:

  • (Boolean)

    true if the rule has a block, false otherwise.



70
71
72
# File 'lib/devdnsd/rule.rb', line 70

def has_block?
  self.block.present?
end

#is_regexp?Boolean

Checks if the rule is a regexp.

Returns:

  • (Boolean)

    true if the rule is a Regexp, false otherwise.



63
64
65
# File 'lib/devdnsd/rule.rb', line 63

def is_regexp?
  self.match.is_a?(::Regexp)
end

#match_host(hostname) ⇒ MatchData|Boolean|Nil

Matches a hostname to the rule.

Parameters:

  • hostname (String)

    The hostname to match.

Returns:

  • (MatchData|Boolean|Nil)

    Return true or MatchData (if the pattern is a regexp) if the rule matches, false or nil otherwise.



78
79
80
# File 'lib/devdnsd/rule.rb', line 78

def match_host(hostname)
  self.is_regexp? ? self.match.match(hostname) : (self.match == hostname)
end

#resource_classArray|Class

Returns the resource class(es) for the current rule.

Returns:

  • (Array|Class)

    The class(es) for the current rule.



55
56
57
58
# File 'lib/devdnsd/rule.rb', line 55

def resource_class
  classes = self.type.ensure_array.collect {|cls| self.class.symbol_to_resource_class(cls) }.compact.uniq
  classes.length == 1 ? classes.first : classes
end