Class: Knocker::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/knocker/pattern.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, text) ⇒ Pattern

Returns a new instance of Pattern.



5
6
7
8
# File 'lib/knocker/pattern.rb', line 5

def initialize(name, text)
  @name = name
  @text = text
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/knocker/pattern.rb', line 3

def name
  @name
end

Instance Method Details

#hostObject



18
19
20
21
22
# File 'lib/knocker/pattern.rb', line 18

def host
  host = @text.scan(/  h (.*)/).flatten.first
  raise Knocker::Errors::NoHostSpecified if host.nil? or host.empty?
  host
end

#knock_commandObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/knocker/pattern.rb', line 24

def knock_command
  patterns = []

  patterns = @text.scan(/  ([ut]) (\d+)/).collect do |proto, port|
    valid_port!(port)
    "#{port}:#{protocol!(proto)}"
  end

  raise Knocker::Errors::NoKnockSpecified if patterns.empty?

  "knock #{host} #{patterns.join(" ")}"
end

#post_commandsObject



37
38
39
40
41
42
# File 'lib/knocker/pattern.rb', line 37

def post_commands
  cmds = @text.scan(/^  c (.*)$/).flatten
  cmds.map! do |cmd|
    cmd.gsub(/\$host/, host)
  end
end

#to_hashObject



10
11
12
13
14
15
16
# File 'lib/knocker/pattern.rb', line 10

def to_hash
  {
    :host => host,
    :knock_command => knock_command,
    :name => @name
  }
end