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
  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(options) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/msgr/railtie.rb', line 81

def load_config(options)
  if options.rabbitmq_config || !Rails.application.respond_to?(:config_for)
    load_file options.rabbitmq_config || Rails.root.join('config', 'rabbitmq.yml')
  else
    conf = Rails.application.config_for :rabbitmq

    {Rails.env.to_s => conf}
  end
end

.load_file(path) ⇒ Object



91
92
93
# File 'lib/msgr/railtie.rb', line 91

def load_file(path)
  YAML.safe_load ERB.new(File.read(path)).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
71
72
73
74
75
76
77
78
79
# 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.new('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.new("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.new("Invalid value for rabbitmq config checkcredentials: \"#{cfg[:checkcredentials]}\"")
  end

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

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