Class: NewRelicPing::Configuration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Configuration

Returns a new instance of Configuration.



6
7
8
9
10
11
# File 'lib/new_relic_ping/configuration.rb', line 6

def initialize(data={})
  @data = {}
  @monitors = {}
  update!(data)
  load_default_monitors
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/new_relic_ping/configuration.rb', line 27

def method_missing(sym, *args)
  if sym.to_s =~ /(.+)=$/
    self[$1] = args.first
  else
    self[sym]
  end
end

Instance Attribute Details

#monitorsObject

Returns the value of attribute monitors.



4
5
6
# File 'lib/new_relic_ping/configuration.rb', line 4

def monitors
  @monitors
end

Instance Method Details

#[](key) ⇒ Object



19
20
21
# File 'lib/new_relic_ping/configuration.rb', line 19

def [](key)
  @data[key.to_sym]
end

#[]=(key, value) ⇒ Object



23
24
25
# File 'lib/new_relic_ping/configuration.rb', line 23

def []=(key, value)
  @data[key.to_sym] = value
end

#load_default_monitorsObject



39
40
41
42
43
44
45
# File 'lib/new_relic_ping/configuration.rb', line 39

def load_default_monitors
  if defined?(ActiveRecord::Base)
    monitor('database') do
      ActiveRecord::Base.connection.select_values("select 1") == [1]
    end
  end
end

#monitor(label, &block) ⇒ Object



35
36
37
# File 'lib/new_relic_ping/configuration.rb', line 35

def monitor(label, &block)
  @monitors[label.to_s] = block
end

#status_checkObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/new_relic_ping/configuration.rb', line 47

def status_check
  responses = {}
  begin
    @monitors.each do |label, mon|
      return_value = nil
      time = Benchmark.realtime do
        responses["#{label}_response"] = (mon.call).to_s
      end
      responses["#{label}_time"] = "#{time.inspect} seconds"
    end
    return :ok, responses
  rescue => e
    responses['error_message'] = e.message
    return :error, responses
  end
end

#update!(data) ⇒ Object



13
14
15
16
17
# File 'lib/new_relic_ping/configuration.rb', line 13

def update!(data)
  data.each do |key, value|
    self[key] = value
  end
end