Class: Xify

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

Class Method Summary collapse

Class Method Details

.debug(str) ⇒ Object



68
69
70
# File 'lib/xify.rb', line 68

def self.debug(str)
  puts str if @verbose
end

.error(e) ⇒ Object



72
73
74
75
76
# File 'lib/xify.rb', line 72

def self.error(e)
  return $stderr.puts e.message unless @verbose

  $stderr.puts "#{e.backtrace.first}: #{e.message} (#{e.class})", e.backtrace.drop(1).map{|s| "\t#{s}"}
end

.runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/xify.rb', line 14

def self.run
  working_dir = "#{ENV['HOME']}/.xify"
  Dir.mkdir working_dir rescue Errno::EEXIST
  config_file = "#{working_dir}/config.yml"
  files = []

  while arg = ARGV.shift do
    case arg
    when '-c', '--config'
      config_file = ARGV.shift
    when '-v', '--verbose'
      @verbose = true
    else
      files << arg
    end
  end

  ARGV.unshift(*files)

  debug "Loading config from #{config_file}"
  config = YAML::load_file config_file

  config.keys.each do |section|
    type = section[0...-1]
    config[section].map! do |handler|
      next unless handler['enabled']
      debug "Setting up #{handler['class']} as #{type}"
      Object.const_get("#{type.capitalize}::#{handler['class']}").new handler
    end.compact!
  end

  begin
    config['inputs'].each do |i|
      begin
        i.updates do |u|
          config['outputs'].each do |o|
            begin
              o.process u
            rescue => e
              error e
            end
          end
        end
      rescue => e
        error e
      end
    end

    Rufus::Scheduler.singleton.join
  rescue Interrupt => e
    $stderr.puts "\nExiting."
  end
end