Module: Huck

Defined in:
lib/huck.rb,
lib/huck/util.rb,
lib/huck/sender.rb,
lib/huck/handler.rb,
lib/huck/version.rb,
lib/huck/receiver.rb,
lib/huck/generator.rb,
lib/huck/senders/sqs.rb,
lib/huck/handlers/echo.rb,
lib/huck/handlers/exec.rb,
lib/huck/receivers/sqs.rb,
lib/huck/generators/json.rb,
lib/huck/generators/ohai.rb,
lib/huck/generators/yaml.rb,
lib/huck/generators/facter.rb

Defined Under Namespace

Modules: Generators, Handlers, Receivers, Senders Classes: Generator, Handler, Receiver, Sender

Constant Summary collapse

Version =
'0.2.0'

Class Method Summary collapse

Class Method Details

.config(kwargs = {}) ⇒ Object

Read configuration out of a file on the filesystem.

Parameters:

path

The path to the config file to read

Returns:

A hash containing the configuration



29
30
31
32
33
# File 'lib/huck/util.rb', line 29

def self.config kwargs = {}
  path = self.getarg kwargs, :path, nil
  path = File.join(Dir.home, 'huck.conf') if path.nil?
  YAML.load_file path
end

.getarg(args, arg, default) ⇒ Object

Retrieve the value of an “emulated” kwarg easily

Parameters:

args

A hash of kwargs

arg

The name of the arg desired

default

If arg does not exist in args, return this

Returns:

The value of the arg, or the default value



16
17
18
# File 'lib/huck/util.rb', line 16

def self.getarg args, arg, default
  return args.has_key?(arg) ? args[arg] : default
end

.run(kwargs = {}) ⇒ Object

Main method to run Huck and dump info

Parameters:

generator

The name of the generator to use (default=facter)



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/huck.rb', line 27

def self.run kwargs = {}
  conf_file = Huck::getarg kwargs, :config, nil
  config = Huck::config :path => conf_file

  if config.has_key? 'generator'
    gen_name = config['generator']
  end
  gen_arg = Huck::getarg kwargs, :generator, nil
  gen_name = gen_arg if !gen_arg.nil?
  g = Generator::factory :name => gen_name, :config => config

  if config.has_key? 'sender'
    send_name = config['sender']
  end
  send_arg = Huck::getarg kwargs, :sender, nil
  send_name = send_arg if !send_arg.nil?
  s = Sender::factory :name => send_name, :config => config

  s.send g.dump
end

.serve(kwargs = {}) ⇒ Object

Main method to receive messages from a Huck client

Parameters:

receiver

The receiver to use (default=sqs)



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/huck.rb', line 54

def self.serve kwargs = {}
  conf_file = Huck::getarg kwargs, :config, nil
  config = Huck::config :path => conf_file

  if config.has_key? 'handler'
    hand_name = config['handler']
  end
  hand_arg = Huck::getarg kwargs, :handler, nil
  hand_name = hand_arg if !hand_arg.nil?
  h = Handler::factory :name => hand_name, :config => config

  if config.has_key? 'receiver'
    recv_name = config['receiver']
  end
  recv_arg = Huck::getarg kwargs, :receiver, nil
  recv_name = recv_arg if !recv_arg.nil?
  r = Receiver::factory :name => recv_name, :config => config

  begin
    r.receive do |msg|
      h.handle msg
    end
  rescue Interrupt, SystemExit
    return
  end
end

.try_load(name) ⇒ Object

Attempts to load a given module by name

Parameters:

name

The name of the module

Returns:

bool



44
45
46
47
48
49
50
51
# File 'lib/huck/util.rb', line 44

def self.try_load name
  begin
    require name
  rescue LoadError
    return false
  end
  true
end