class RkRspec::ResultsSender

Attributes

tmp_data[R]

Public Class Methods

new() click to toggle source
# File lib/results_keeper_rspec/results_sender.rb, line 13
def initialize
  @data_generator = RkRspec::TestDataGenerator.new
  @tmp_data ||= {}
  @tmp_data[:tests] ||= []
end

Public Instance Methods

add_test_to_tmp(scenario) click to toggle source
# File lib/results_keeper_rspec/results_sender.rb, line 83
def add_test_to_tmp(scenario)
  @tmp_data[:tests] << @data_generator.test_data(scenario, @revision)
end
send_json(body, path, message_to_client=nil) click to toggle source
# File lib/results_keeper_rspec/results_sender.rb, line 54
def send_json(body, path, message_to_client=nil)
  body['secret_key'] = RkRspec::Config.config[:secret_key]
  @path = "/api/#{path}"
  @body = body.to_json
  begin
    request = Net::HTTP::Post.new(@path, initheader = {'Content-Type' => 'application/json'})
    request.body = @body
    response = Net::HTTP.new(RkRspec::Config.config[:report_server_host],
                             RkRspec::Config.config[:report_server_port]).start { |http| http.request(request) }

    RkRspec::Messages.info_message_to_client(response, message_to_client) if message_to_client
    JSON.parse(response.body)
  rescue
    false
  end
end
send_not_sent_scenarios() click to toggle source
# File lib/results_keeper_rspec/results_sender.rb, line 71
def send_not_sent_scenarios
  if @revision
    @tmp_data[:tests].each do |s|
      s[:revision_id] = @revision['revision_id'] unless s[:revision_id]
      RkRspec::Messages.info_trying_resend(s[:name])
      send_json(s, RkRspec::Config.default[:api][:test_path]) if @revision
    end
  else
    false
  end
end
send_revision() click to toggle source
# File lib/results_keeper_rspec/results_sender.rb, line 19
def send_revision
  @revision = send_json(@data_generator.revision_data,
                        RkRspec::Config.default[:api][:revision_path],
                        "#{RkRspec::Config.config[:project_name]} > #{RkRspec::Config.config[:revision_name]} started")
end
send_revision_complete() click to toggle source
# File lib/results_keeper_rspec/results_sender.rb, line 25
def send_revision_complete
  send_not_sent_scenarios
  send_json(@data_generator.revision_completed_data,
            RkRspec::Config.default[:api][:revision_path],
            "#{RkRspec::Config.config[:revision_name]} completed")
end
send_screenshot(screenshot_path) click to toggle source
# File lib/results_keeper_rspec/results_sender.rb, line 43
def send_screenshot(screenshot_path)
  begin
    params = { project: @revision['project_id'], revision: @revision['revision_id'], test: @test['id'] }
    RestClient.post("http://#{RkRspec::Config.config[:report_server_host]}:#{RkRspec::Config.config[:report_server_port]}/api/tests/upload_screenshot",
                    :name_of_file_param => File.new(screenshot_path), :body => params)
    File.delete(screenshot_path)
  rescue
    RkRspec::Messages.error_send_screenshot
  end
end
send_test(scenario) click to toggle source
# File lib/results_keeper_rspec/results_sender.rb, line 32
def send_test(scenario)
  # In case if the host is not available we need to send revision once more (when it comes back)
  send_revision unless @revision
  return unless @revision
  screenshot_path = @data_generator.save_screenshot(scenario) if ENV['RK_SEND_SCREENSHOTS']
  @test = send_json(@data_generator.test_data(scenario, @revision),
                    RkRspec::Config.default[:api][:test_path], nil)
  send_screenshot(screenshot_path) if screenshot_path
  @test
end