Class: Ipecache::Plugins::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/ipecache/plugins/plugin.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Plugin

Returns a new instance of Plugin.



31
32
33
34
35
# File 'lib/ipecache/plugins/plugin.rb', line 31

def initialize(options = {})
  @options = {
      :payload => {}
  }.merge(options)
end

Class Method Details

.hook(the_hook) ⇒ Object

When defining a hook, we define a method on the instance that corresponds to that hook. That will be fired when the hook is fired.



22
23
24
25
26
27
28
29
# File 'lib/ipecache/plugins/plugin.rb', line 22

def self.hook(the_hook)
  self.send(:define_method, the_hook.to_sym) do
    if !quiet_mode
      puts ""
    end
    perform
  end
end

.hooks(*the_hooks) ⇒ Object

This is a convenience method for defining multiple hooks in a single call.



16
17
18
# File 'lib/ipecache/plugins/plugin.rb', line 16

def self.hooks(*the_hooks)
  [the_hooks].flatten.each{ |the_hook| hook(the_hook) }
end

.name(name = nil) ⇒ Object

This is the name of the plugin. It must correspond to the name in the yaml configuration file in order to load this plugin. If an attribute is passed in, the name is set to that given value. Otherwise, the name is returned.



7
8
9
10
11
12
13
# File 'lib/ipecache/plugins/plugin.rb', line 7

def self.name(name = nil)
  if name.nil?
    class_variable_get(:@@name)
  else
    class_variable_set(:@@name, name)
  end
end

Instance Method Details

#continue_on_errorObject



49
50
51
# File 'lib/ipecache/plugins/plugin.rb', line 49

def continue_on_error
  @options[:continue_on_error]
end

#enabled?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/ipecache/plugins/plugin.rb', line 37

def enabled?
  !(config.nil? || config.enabled == false)
end

#log_fileObject



45
46
47
# File 'lib/ipecache/plugins/plugin.rb', line 45

def log_file
  @options[:log_file]
end

#nameObject



57
58
59
# File 'lib/ipecache/plugins/plugin.rb', line 57

def name
  self.class.to_s
end

#plugin_puts(message) ⇒ Object



61
62
63
64
65
# File 'lib/ipecache/plugins/plugin.rb', line 61

def plugin_puts(message)
  if !quiet_mode
    puts "#{name}: #{message}"
  end
end

#plugin_puts_error(url, message) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/ipecache/plugins/plugin.rb', line 67

def plugin_puts_error(url,message)
  if log_file
    File.open(log_file, 'a') { |file| file.write("#{Time.now.getutc} #{url} #{name}: #{message}\n") }
  end

  if !quiet_mode
    puts "#{url} #{name}: #{message}"
  end
end

#quiet_modeObject



53
54
55
# File 'lib/ipecache/plugins/plugin.rb', line 53

def quiet_mode
  @options[:quiet_mode]
end

#urlsObject



41
42
43
# File 'lib/ipecache/plugins/plugin.rb', line 41

def urls
  @options[:urls]
end