class RbRotate::Hook

Represents hook.

Public Class Methods

new(name, arguments = nil, variables = { }) click to toggle source

Constructor.

# File lib/rb.rotate/hook.rb, line 40
def initialize(name, arguments = nil, variables = { })
    @name = name
    @arguments = self.parse_arguments(arguments)
    @variables = variables
end

Public Instance Methods

command() click to toggle source

Gets command.

# File lib/rb.rotate/hook.rb, line 126
def command
    command = Configuration::get.hooks[@name]
    if command.nil?
        raise Exception::new("Invalid hook: " << @name.to_s)
    end
end
expand_arguments(command) click to toggle source

Expands arguments.

# File lib/rb.rotate/hook.rb, line 87
def expand_arguments(command)
    @arguments.each_index do |i|
        arg = arguments[i]
        command.gsub! "%" << i.to_s, '"' << arg << '"'
    end            
end
expand_variables(command) click to toggle source

Expands variables.

# File lib/rb.rotate/hook.rb, line 98
def expand_variables(command)
    @variables.each_pair do |name, value|
        command.gsub! "%" << name, '"' << value << '"'
    end
end
parse_arguments(string) click to toggle source

Parses “arguments line”.

# File lib/rb.rotate/hook.rb, line 50
def parse_arguments(string)
    if not string.nil?
        string.split(":")
    else
        []
    end
end
parse_result(result) click to toggle source

Parses result.

# File lib/rb.rotate/hook.rb, line 108
def parse_result(result)
    if result.strip.empty?
        return { }
    end

    result = YAML.load(result)
    if not result.kind_of? Hash
        result = { }
        log "Warning: result of hook '" << @name.to_s << "' wasn't YAML collection. Ignored."
    end
    
    return result
end
run!() click to toggle source

Runs hook.

# File lib/rb.rotate/hook.rb, line 62
def run!
    # Gets commans
    command = self.command.dup
    
    # Adds arguments
    self.expand_arguments(command)
    
    # Adds variables
    self.expand_variables(command)
    
    # Runs it
    pipe = ::File.popen(command)
    pipe.eof?       # ask for EOF causes waiting for terminating the pipe process
    
    result = pipe.read
    pipe.close()

    # Parses and returns result
    return self.parse_result(result)
end