Class: Pingify::Runnable

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = { :timeout => 2.0, :open_timeout => 2.0 }) ⇒ Runnable

Returns a new instance of Runnable.



10
11
12
13
# File 'lib/pingify.rb', line 10

def initialize(options={ :timeout => 2.0, :open_timeout => 2.0 })      @uri = options.delete :uri
  @times = options.delete(:times) || 4
  @http_opts = { :timeout => options.delete(:timeout), :open_timeout => options.delete(:open_timeout) }
end

Instance Attribute Details

#uriObject

Returns the value of attribute uri.



8
9
10
# File 'lib/pingify.rb', line 8

def uri
  @uri
end

Instance Method Details

#runObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pingify.rb', line 15

def run
  res = ping!
  ping_headers = Hash[res.headers.select{ |k,_| k.match(/^x_ping_/) }.map{ |k,v| [k.to_s.sub('x_ping_', '').to_sym, v] }]

  runtimes = @times - 1
  start = Time.now
  runtimes.times { ping! }
  total_time = Time.now - start

  avg = total_time / runtimes

  data = { :average => avg, :body => res.body }.merge(ping_headers)
  return Result.new(data)
rescue
  return ErrorResult.new({}, [$!])
end