Class: CollinsNotify::Notifier

Inherits:
Object
  • Object
show all
Includes:
Collins::Util
Defined in:
lib/collins_notify/notifier.rb

Direct Known Subclasses

EmailAdapter, HipchatAdapter, IrcAdapter, SlackAdapter

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Notifier

Returns a new instance of Notifier.



56
57
58
59
# File 'lib/collins_notify/notifier.rb', line 56

def initialize app
  @config = app.config
  @logger = app.logger
end

Class Method Details

.adapter_nameObject



23
24
25
# File 'lib/collins_notify/notifier.rb', line 23

def adapter_name
  @adapter_name
end

.adaptersObject



32
33
34
35
36
# File 'lib/collins_notify/notifier.rb', line 32

def adapters
  ObjectSpace.each_object(Class).select { |klass| klass < self }.inject({}) do |ret,k|
    ret.update(k.adapter_name => k)
  end
end

.get_adapter(application) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/collins_notify/notifier.rb', line 37

def get_adapter application
  type = application.type
  config = application.config
  adapter_config = config.adapters[type]
  adapter = adapters[type]
  (raise CollinsNotifyException.new("Invalid notify type #{type}")) if adapter.nil?
  if adapter.requires_config? then
    (raise CollinsNotify::ConfigurationError.new "No config found for #{type}") if adapter_config.nil?
    adapter.required_config_keys.each do |key|
      unless adapter_config.key?(key) then
        raise CollinsNotify::ConfigurationError.new "Missing #{type}.#{key} config key"
      end
    end
  end
  (raise CollinsNotifyException.new("No config found for #{type}")) if adapter.requires_config? and adapter_config.nil?
  adapter.new application
end

.mimetypesObject



29
30
31
# File 'lib/collins_notify/notifier.rb', line 29

def mimetypes
  @supported_mimetypes || [:text]
end

.register_name(name) ⇒ Object



20
21
22
# File 'lib/collins_notify/notifier.rb', line 20

def register_name name
  @adapter_name = name
end

.require_config(*keys) ⇒ Object



11
12
13
# File 'lib/collins_notify/notifier.rb', line 11

def require_config *keys
  @requires_config = keys.map{|k| k.to_sym}
end

.required_config_keysObject



17
18
19
# File 'lib/collins_notify/notifier.rb', line 17

def required_config_keys
  @requires_config || []
end

.requires_config?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/collins_notify/notifier.rb', line 14

def requires_config?
  @requires_config && !@requires_config.empty?
end

.supports_mimetype(*mimetypes) ⇒ Object



26
27
28
# File 'lib/collins_notify/notifier.rb', line 26

def supports_mimetype *mimetypes
  @supported_mimetypes = mimetypes.map{|e| e.to_sym}
end

Instance Method Details

#configure!Object

Throw an exception if it cant be configured



62
63
64
# File 'lib/collins_notify/notifier.rb', line 62

def configure!
  raise NotImplementedError.new "CollinsNotify::Notifier#configure! must be implemented"
end

#notify!(message_obj = OpenStruct.new, to = nil) ⇒ Object

Return boolean indicating success/fail



67
68
69
# File 'lib/collins_notify/notifier.rb', line 67

def notify! message_obj = OpenStruct.new, to = nil
  raise NotImplementedError.new "CollinsNotify::Notifier#notify! must be implemented"
end

#supports_html?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/collins_notify/notifier.rb', line 74

def supports_html?
  self.class.mimetypes.include?(:html)
end

#supports_text?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/collins_notify/notifier.rb', line 71

def supports_text?
  self.class.mimetypes.include?(:text)
end