class Echo_cli::Echo

Public Instance Methods

post(ip, body, num = 0) click to toggle source
# 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