Class: Echo_cli::Echo

Inherits:
Thor
  • Object
show all
Defined in:
lib/echo_cli/cli.rb

Instance Method Summary collapse

Instance Method Details

#post(ip, body, num = 0) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/echo_cli/cli.rb', line 20

def post(ip, body, num = 0)
  request_body = body.gsub(/'/,"")
  statsd_uri = "http://" + ip + ":8125/metrix"

  ENV["http_proxy"] = ENV["HTTP_PROXY"]
  ENV["https_proxy"] = ENV["HTTPS_PROXY"]

  uri = URI.parse(statsd_uri)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE if https.use_ssl?
  req = Net::HTTP::Post.new(uri.request_uri, initheader = {'Content-Type' => 'text/plain'} )
  req.body = request_body

  if(num.to_i > 0)

    begin
      time_start = Time.new.to_i.to_s

      for i in 1..num.to_i
        res = https.request(req)
        if res.code != '200'
          puts "Final run at: " + i
          raise
        end
      end

      time_end = Time.new.to_i.to_s

      puts "\n\tSuccessfully posted ".green + request_body.yellow + " to ".green + ip.blue + " " + num.green + " time(s)".green + "\n\tTime start: ".green + time_start.yellow + ", Time end: ".green + time_end.yellow
    rescue
      puts "\n\tFailed to post ".red + request_body.yellow + " to ".red + ip.blue + " " + num.red + " time(s)".red
    end

  else
    begin
      time_start = Time.new.to_i.to_s
      res = https.request(req)
      time_end = Time.new.to_i.to_s
      if res.code == '200'
        puts "\n\tSuccessfully posted ".green + request_body.yellow + " to ".green + ip.blue + "\n\tTime start: ".green + time_start.yellow + ", Time end: ".green + time_end.yellow
      end
    rescue
      puts "\n\tFailed to post ".red + request_body.yellow + " to ".red + ip.blue
    end
  end

  ENV["http_proxy"] = ""
  ENV["https_proxy"] = ""
end