Class: Ping
Instance Attribute Summary collapse
-
#audible ⇒ Object
writeonly
Sets the attribute audible.
-
#count ⇒ Object
writeonly
Sets the attribute count.
-
#delay ⇒ Object
writeonly
Sets the attribute delay.
-
#flood ⇒ Object
writeonly
Sets the attribute flood.
-
#format ⇒ Object
writeonly
Sets the attribute format.
-
#referrer ⇒ Object
writeonly
Sets the attribute referrer.
- #uri ⇒ Object
-
#user_agent ⇒ Object
writeonly
Sets the attribute user_agent.
Instance Method Summary collapse
- #beep ⇒ Object
- #count_reached? ⇒ Boolean
- #http_header ⇒ Object
-
#initialize ⇒ Ping
constructor
A new instance of Ping.
- #interactive_results ⇒ Object
- #json_results ⇒ Object
- #ping ⇒ Object
- #ping_summary(response, data, duration) ⇒ Object
- #quick_results ⇒ Object
- #request ⇒ Object
- #results ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize ⇒ Ping
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
4 5 6 |
# File 'lib/httping/ping.rb', line 4 def audible=(value) @audible = value end |
#count=(value) ⇒ Object (writeonly)
Sets the attribute count
4 5 6 |
# File 'lib/httping/ping.rb', line 4 def count=(value) @count = value end |
#delay=(value) ⇒ Object (writeonly)
Sets the attribute delay
4 5 6 |
# File 'lib/httping/ping.rb', line 4 def delay=(value) @delay = value end |
#flood=(value) ⇒ Object (writeonly)
Sets the attribute flood
4 5 6 |
# File 'lib/httping/ping.rb', line 4 def flood=(value) @flood = value end |
#format=(value) ⇒ Object (writeonly)
Sets the attribute format
4 5 6 |
# File 'lib/httping/ping.rb', line 4 def format=(value) @format = value end |
#referrer=(value) ⇒ Object (writeonly)
Sets the attribute referrer
4 5 6 |
# File 'lib/httping/ping.rb', line 4 def referrer=(value) @referrer = value end |
#uri ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/httping/ping.rb', line 37 def uri if @uri.query "#{@uri.path}?#{@uri.query}" else "#{@uri.path}" end end |
#user_agent=(value) ⇒ Object (writeonly)
Sets the attribute user_agent
4 5 6 |
# File 'lib/httping/ping.rb', line 4 def user_agent=(value) @user_agent = value end |
Instance Method Details
#beep ⇒ Object
57 58 59 |
# File 'lib/httping/ping.rb', line 57 def beep 7.chr end |
#count_reached? ⇒ Boolean
90 91 92 |
# File 'lib/httping/ping.rb', line 90 def count_reached? @ping_results.size == @count end |
#http_header ⇒ Object
45 46 47 48 49 50 |
# File 'lib/httping/ping.rb', line 45 def http_header header = {} header['User-Agent'] = @user_agent if @user_agent header['Referrer'] = @referrer if @referrer header end |
#interactive_results ⇒ Object
71 72 73 74 75 76 |
# File 'lib/httping/ping.rb', line 71 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_results ⇒ Object
78 79 80 81 82 83 |
# File 'lib/httping/ping.rb', line 78 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 |
#ping ⇒ Object
19 20 21 22 23 24 |
# File 'lib/httping/ping.rb', line 19 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
52 53 54 55 |
# File 'lib/httping/ping.rb', line 52 def ping_summary(response, data, duration) print beep if @audible puts "#{data.length.to_human_size} from #{@uri}: code=#{response.code} msg=#{response.} time=#{duration.to_human_time}" end |
#quick_results ⇒ Object
85 86 87 88 |
# File 'lib/httping/ping.rb', line 85 def quick_results duration = @ping_results.first.to_human_time puts "OK [#{duration}]" end |
#request ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/httping/ping.rb', line 26 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 |
#results ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/httping/ping.rb', line 61 def results if @format.nil? interactive_results else send("#{@format}_results") end exit end |
#run ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/httping/ping.rb', line 10 def run trap("INT") { results } loop do ping results if count_reached? sleep @delay unless @flood end end |