Class: Knocker::Pattern
- Inherits:
-
Object
- Object
- Knocker::Pattern
- Defined in:
- lib/knocker/pattern.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #host ⇒ Object
-
#initialize(name, text) ⇒ Pattern
constructor
A new instance of Pattern.
- #knock_command ⇒ Object
- #post_commands ⇒ Object
- #to_hash ⇒ Object
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/knocker/pattern.rb', line 3 def name @name end |
Instance Method Details
#host ⇒ Object
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_command ⇒ Object
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_commands ⇒ Object
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_hash ⇒ Object
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 |