Class: RkCucumber::ResultsSender

Inherits:
Object
  • Object
show all
Defined in:
lib/results_keeper/results_sender.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResultsSender

Returns a new instance of ResultsSender.



13
14
15
16
17
# File 'lib/results_keeper/results_sender.rb', line 13

def initialize
  @data_generator = RkCucumber::TestDataGenerator.new
  @tmp_data ||= {}
  @tmp_data[:tests] ||= []
end

Instance Attribute Details

#tmp_dataObject (readonly)

Returns the value of attribute tmp_data.



11
12
13
# File 'lib/results_keeper/results_sender.rb', line 11

def tmp_data
  @tmp_data
end

Instance Method Details

#add_test_to_tmp(scenario, test_started_at) ⇒ Object



91
92
93
94
# File 'lib/results_keeper/results_sender.rb', line 91

def add_test_to_tmp(scenario, test_started_at)
  @tmp_data[:tests] << @data_generator.test_data(scenario, @revision, test_started_at)
  RkCucumber::Messages.error_not_sent_scenario(@tmp_data[:tests].count)
end

#send_json(body, path, message_to_client = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/results_keeper/results_sender.rb', line 56

def send_json(body, path, message_to_client=nil)
  body['secret_key'] = RkCucumber::Config.config[:secret_key]
  @path = "/api/#{path}"
  @body = body.to_json
  begin
    uri = URI("#{RkCucumber::Config.config[:report_server_host]}:#{RkCucumber::Config.config[:report_server_port]}#{@path}")
    response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
      request = Net::HTTP::Post.new(@path, initheader = {'Content-Type' => 'application/json'})
      request.body = @body
      http.request request
    end
    RkCucumber::Messages.info_message_to_client(response, message_to_client)
    JSON.parse(response.body)
  rescue
    RkCucumber::Messages.warn_problem_with_rk
    false
  end
end

#send_not_sent_scenariosObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/results_keeper/results_sender.rb', line 44

def send_not_sent_scenarios
  if @revision
    @tmp_data[:tests].each do |s|
      s[:revision_id] = @revision['revision_id'] unless s[:revision_id]
      RkCucumber::Messages.info_trying_resend(s[:name])
      send_json(s, RkCucumber::Config.default[:api][:test_path]) if @revision
    end
  else
    false
  end
end

#send_revisionObject



19
20
21
22
23
# File 'lib/results_keeper/results_sender.rb', line 19

def send_revision
  @revision = send_json(@data_generator.revision_data,
                        RkCucumber::Config.default[:api][:revision_path],
                        "#{RkCucumber::Config.config[:project_name]} > #{RkCucumber::Config.config[:revision_name]} started")
end

#send_revision_completeObject



25
26
27
28
29
30
# File 'lib/results_keeper/results_sender.rb', line 25

def send_revision_complete
  send_not_sent_scenarios
  send_json(@data_generator.revision_completed_data,
            RkCucumber::Config.default[:api][:revision_path],
            "#{RkCucumber::Config.config[:revision_name]} completed")
end

#send_screenshot(screenshot_path) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/results_keeper/results_sender.rb', line 75

def send_screenshot(screenshot_path)
  begin
    params = {project: @revision['project_id'], revision: @revision['revision_id'], test: @test['id']}
    RestClient::Request.execute(:url => "#{RkCucumber::Config.config[:report_server_host]}:"+
        "#{RkCucumber::Config.config[:report_server_port]}/api/tests/upload_screenshot",
                                :method => :post,
                                :verify_ssl => false,
                                :name_of_file_param => File.new(screenshot_path),
                                :payload => {:multipart => true, :name_of_file_param => File.new(screenshot_path)},
                                :headers => {:accept => :json, params: {body: params}})
    File.delete(screenshot_path)
  rescue
    RkCucumber::Messages.error_send_screenshot
  end
end

#send_test(scenario, test_started_at) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/results_keeper/results_sender.rb', line 32

def send_test(scenario, test_started_at)
  # In case if the host is not available we need to send revision once more (when it comes back)
  send_revision unless @revision
  return nil unless @revision
  screenshot_path = @data_generator.save_screenshot(scenario) if ENV['RK_SEND_SCREENSHOTS']
  @test = send_json(@data_generator.test_data(scenario, @revision, test_started_at),
                    RkCucumber::Config.default[:api][:test_path])
  return nil unless @test
  send_screenshot(screenshot_path) if screenshot_path
  @test
end