Class: Msgr::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/msgr/railtie.rb

Class Method Summary collapse

Class Method Details

.load(rails_config) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/msgr/railtie.rb', line 25

def load(rails_config)
  cfg = parse_config load_config rails_config.rabbitmq_config.to_s
  return unless cfg # no config given -> does not load Msgr

  Msgr.config = cfg
  Msgr.client.connect if cfg[:checkcredentials]
  Msgr.start if cfg[:autostart]
end

.load_config(file) ⇒ Object



72
73
74
# File 'lib/msgr/railtie.rb', line 72

def load_config(file)
  YAML.load ERB.new(File.read(file)).result
end

.parse_config(cfg) ⇒ Object



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
67
68
69
70
# File 'lib/msgr/railtie.rb', line 34

def parse_config(cfg)
  unless cfg.is_a? Hash
    Rails.logger.warn '[Msgr] Could not load rabbitmq config: Config must be a Hash'
    return nil
  end

  unless cfg[Rails.env].is_a?(Hash)
    Rails.logger.warn "Could not load rabbitmq config for environment \"#{Rails.env}\": is not a Hash"
    return nil
  end

  cfg = HashWithIndifferentAccess.new cfg[Rails.env]
  unless cfg[:uri]
    raise ArgumentError, 'Could not load rabbitmq environment config: URI missing.'
  end

  case cfg[:autostart]
    when true, 'true', 'enabled'
      cfg[:autostart] = true
    when false, 'false', 'disabled', nil
      cfg[:autostart] = false
    else
      raise ArgumentError, "Invalid value for rabbitmq config autostart: \"#{cfg[:autostart]}\""
  end

  case cfg[:checkcredentials]
    when true, 'true', 'enabled', nil
      cfg[:checkcredentials] = true
    when false, 'false', 'disabled'
      cfg[:checkcredentials] = false
    else
      raise ArgumentError, "Invalid value for rabbitmq config checkcredentials: \"#{cfg[:checkcredentials]}\""
  end

  cfg[:routing_file] ||= Rails.root.join('config/msgr.rb').to_s
  cfg
end