class Pingify::Runnable

Attributes

uri[RW]

Public Class Methods

new(options={ :timeout => 2.0, :open_timeout => 2.0 }) click to toggle source
# 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

Public Instance Methods

run() click to toggle source
# File lib/pingify.rb, line 16
def run
  res = ping!

  data = { :average => average_time(@times - 1), :body => res.body }.merge(ping_headers(res))

  return Result.new(data)
rescue
  return ErrorResult.new({}, [$!])
end

Private Instance Methods

average_time(runtimes) click to toggle source
# File lib/pingify.rb, line 28
def average_time(runtimes)
  start = Time.now
  runtimes.times { ping! }
  total_time = Time.now - start

  total_time / runtimes
end
ping!() click to toggle source
# File lib/pingify.rb, line 40
def ping!
  RestClient.get(uri, @http_opts)
end
ping_headers(result) click to toggle source
# File lib/pingify.rb, line 36
def ping_headers(result)
  Hash[result.headers.select{ |k,_| k.match(/^x_ping_/) }.map{ |k,v| [k.to_s.sub('x_ping_', '').to_sym, v] }]
end