require "net/http"
require "uri"

sleep 5 # Wait a little bit for JSON artifacts to finish generating
# Create new JSON from Cucumber artifacts
new_json = "["
Dir.glob("./logs/cucumber/**/*.json").each do |f|
  new_json += File.read(f)[1..-2] + ","
end
new_json = new_json[0..-2] + "]"

# Write to local JSON file
# File.open('new_json.json', 'w') { |new_file| new_file.write new_json }

endpoint = "http://qa-dashboard-dev.us-east-1.elasticbeanstalk.com/qa_analytics/jenkins_ingestion/add_build"
uri = URI.parse(endpoint)
header = { "Content-Type" => "text/json" }

# Create the HTTP objects
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri, header)
request.body = new_json

# Send the request
http.request(request)
