lib/capistrano/ghostinspector/api.rb in capistrano-ghostinspector-0.2.0 vs lib/capistrano/ghostinspector/api.rb in capistrano-ghostinspector-0.3.0

- old
+ new

@@ -1,76 +1,94 @@ -require "capistrano/ghostinspector/analytics" require 'net/https' require 'json' module Capistrano module Ghostinspector - module Api - def self.executeApi(type, test, gi_api_key, domain, rollback, ga_property, current_revision) + class Api - # Determine if we should get results to - # check for any failed tests - immediate = self.includeResults(rollback, ga_property) - - # Default all tests pass - passing = true + def initialize(gi_api_key, domain, rollback, ga_property) + @apiKey = gi_api_key + @domain = domain + @rollback = rollback + @ga_property = ga_property - # Lets push the deployment in GA if the configuration allows it. - Capistrano::Ghostinspector::Analytics.pushDeployment(ga_property, current_revision) + # Determine if we should get results to + # check for any failed tests + @immediate = includeResults() + end - # # Perform the API request and get the results - results = self.sendRequest(type, test, gi_api_key, domain, immediate) - # Check the data returned for failed tests - if (rollback == true) - passing = self.getPassing(type, results, ga_property) - end + def executeApi(type, test) - if (passing == false && ga_property != "") - Capistrano::Ghostinspector::Analytics.pushErrors(ga_property, current_revision, results['data']) - end + # Default all tests pass + passing = true - return passing - - end + # ------ TESTING ONLY ------ + # results = JSON.parse(File.read("gitestresults.json")) + # results = JSON.parse(File.read("suiteresults.json")) + # ------ TESTING ONLY ------ - def self.includeResults(rollback, ga_property) - # Determine if we should get results to - # check for any failed tests - if (rollback == false && ga_property == "") - immediate = "&immediate=1" - else - immediate = "" - puts "* * * Gathering results. This could take a few minutes. * * *" - end + # # Perform the API request and get the results + results = sendRequest(type, test) - return immediate - end + # Check the data returned for failed tests + if (@rollback == true) + passing = getPassing(type, results) + end - def self.sendRequest(type, test, gi_api_key, domain, immediate) - - # execute the Ghost Inspector API call - uri = URI("https://api.ghostinspector.com/v1/#{type}/#{test}/execute/?apiKey=#{gi_api_key}&startUrl=http://#{domain}/#{immediate}") - data = Net::HTTP.get(uri) + data = Array.new + data << passing + data << results - results = JSON.parse(data) + return data - return results - end + end - def self.getPassing(type, results, ga_property) - - if (type == "suite") - results['data'].each do |testItem| - passing = testItem['passing'] - end - else - passing = results['data']['passing'] - end + private - return passing - - end + def includeResults() - end + # Determine if we should get results to + # check for any failed tests + if (@rollback == false && @ga_property == "") + immediate = "&immediate=1" + else + immediate = "" + puts "* * * Gathering results. This could take a few minutes. * * *" + end + + return immediate + end + + def sendRequest(type, test) + + uri = URI("https://api.ghostinspector.com/v1/#{type}/#{test}/execute/?apiKey=#{@apiKey}&startUrl=http://#{@domain}/#{@immediate}") + + Net::HTTP.start(uri.host, uri.port, + :use_ssl => uri.scheme == 'https') do |http| + request = Net::HTTP::Get.new uri + http.read_timeout = 600 + @response = http.request request + end + + results = JSON.parse(@response.body) + + return results + end + + def getPassing(type, results) + + if (type == "suites") + results["data"].each do |testItem| + passing = testItem["passing"] + end + else + passing = results["data"]["passing"] + end + + return passing + + end + + end end -end \ No newline at end of file +end