Class: Ping

Inherits:
Object
  • Object
show all
Includes:
Net
Defined in:
lib/httping/ping.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePing

Returns a new instance of Ping.



6
7
8
# File 'lib/httping/ping.rb', line 6

def initialize
  @ping_results = []
end

Instance Attribute Details

#audible=(value) ⇒ Object (writeonly)

Sets the attribute audible

Parameters:

  • value

    the value to set the attribute audible to.



4
5
6
# File 'lib/httping/ping.rb', line 4

def audible=(value)
  @audible = value
end

#count=(value) ⇒ Object (writeonly)

Sets the attribute count

Parameters:

  • value

    the value to set the attribute count to.



4
5
6
# File 'lib/httping/ping.rb', line 4

def count=(value)
  @count = value
end

#delay=(value) ⇒ Object (writeonly)

Sets the attribute delay

Parameters:

  • value

    the value to set the attribute delay to.



4
5
6
# File 'lib/httping/ping.rb', line 4

def delay=(value)
  @delay = value
end

#flood=(value) ⇒ Object (writeonly)

Sets the attribute flood

Parameters:

  • value

    the value to set the attribute flood to.



4
5
6
# File 'lib/httping/ping.rb', line 4

def flood=(value)
  @flood = value
end

#format=(value) ⇒ Object (writeonly)

Sets the attribute format

Parameters:

  • value

    the value to set the attribute format to.



4
5
6
# File 'lib/httping/ping.rb', line 4

def format=(value)
  @format = value
end

#referrer=(value) ⇒ Object (writeonly)

Sets the attribute referrer

Parameters:

  • value

    the value to set the attribute referrer to.



4
5
6
# File 'lib/httping/ping.rb', line 4

def referrer=(value)
  @referrer = value
end

#uriObject



40
41
42
43
44
45
46
# File 'lib/httping/ping.rb', line 40

def uri
  if @uri.query
    "#{@uri.path}?#{@uri.query}"
  else
    "#{@uri.path}"
  end
end

#user_agent=(value) ⇒ Object (writeonly)

Sets the attribute user_agent

Parameters:

  • value

    the value to set the attribute user_agent to.



4
5
6
# File 'lib/httping/ping.rb', line 4

def user_agent=(value)
  @user_agent = value
end

Instance Method Details

#beepObject



60
61
62
# File 'lib/httping/ping.rb', line 60

def beep
  7.chr
end

#count_reached?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/httping/ping.rb', line 88

def count_reached?
  @ping_results.size == @count
end

#http_headerObject



48
49
50
51
52
53
# File 'lib/httping/ping.rb', line 48

def http_header
  header = {}
  header['User-Agent'] = @user_agent if @user_agent
  header['Referer'] = @referrer if @referrer 
  header   
end

#interactive_resultsObject



69
70
71
72
73
74
# File 'lib/httping/ping.rb', line 69

def interactive_results
  puts
  puts "--- #{@uri} httping.rb statistics ---"
  puts "#{@ping_results.size} GETs transmitted"
  puts "round-trip min/avg/max = #{@ping_results.min.to_human_time}/#{@ping_results.mean.to_human_time}/#{@ping_results.max.to_human_time}"
end

#json_resultsObject



76
77
78
79
80
81
# File 'lib/httping/ping.rb', line 76

def json_results
  results = "{\"max\": #{@ping_results.max}, \"avg\": #{@ping_results.mean}, \"min\": #{@ping_results.min}}"
  sent = @ping_results.size
  uri = @uri.to_s
  puts "{\"results\": #{results}, \"sent\": #{sent}, \"uri\": \"#{uri}\"}"
end

#pingObject



22
23
24
25
26
27
# File 'lib/httping/ping.rb', line 22

def ping
  start_time = Time.now
  response, data = request.get(uri, http_header)
  @ping_results << duration = Time.now - start_time
  ping_summary(response, data, duration) if @format == :interactive
end

#ping_summary(response, data, duration) ⇒ Object



55
56
57
58
# File 'lib/httping/ping.rb', line 55

def ping_summary(response, data, duration)
  print beep if @audible
  puts "#{data.length.to_human_size} from #{@uri}: code=#{response.code} msg=#{response.message} time=#{duration.to_human_time}"
end

#quick_resultsObject



83
84
85
86
# File 'lib/httping/ping.rb', line 83

def quick_results
  duration = @ping_results.first.to_human_time
  puts "OK [#{duration}]"
end

#requestObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/httping/ping.rb', line 29

def request
  request = Net::HTTP.new(@uri.host, @uri.port)

  if @uri.scheme == "https"
    request.use_ssl = true
    request.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  
  request
end

#resultsObject



64
65
66
67
# File 'lib/httping/ping.rb', line 64

def results
  send("#{@format}_results")
  exit
end

#runObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/httping/ping.rb', line 10

def run
  trap("INT") { results }
  
  loop do 
    ping
    break if count_reached?
    sleep @delay unless @flood
  end    
  
  results
end