Class: CodeClimate::TestReporter::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/code_climate/test_reporter/client.rb

Constant Summary collapse

DEFAULT_TIMEOUT =

in seconds

5
USER_AGENT =
"Code Climate (Ruby Test Reporter v#{VERSION})"

Instance Method Summary collapse

Instance Method Details

#batch_post_results(files) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/code_climate/test_reporter/client.rb', line 18

def batch_post_results(files)
  uri = URI.parse("#{host}/test_reports/batch")
  http = http_client(uri)

  boundary = SecureRandom.uuid
  post_body = []
  post_body << "--#{boundary}\r\n"
  post_body << "Content-Disposition: form-data; name=\"repo_token\"\r\n"
  post_body << "\r\n"
  post_body << ENV["CODECLIMATE_REPO_TOKEN"]
  files.each_with_index do |file, index|
    post_body << "\r\n--#{boundary}\r\n"
    post_body << "Content-Disposition: form-data; name=\"coverage_reports[#{index}]\"; filename=\"#{File.basename(file)}\"\r\n"
    post_body << "Content-Type: application/json\r\n"
    post_body << "\r\n"
    post_body << File.read(file)
  end
  post_body << "\r\n--#{boundary}--\r\n"
  request = Net::HTTP::Post.new(uri.request_uri)
  request["User-Agent"] = USER_AGENT
  request.body = post_body.join
  request["Content-Type"] = "multipart/form-data, boundary=#{boundary}"
  response = http.request(request)

  if response.code.to_i >= 200 && response.code.to_i < 300
    response
  else
    raise "HTTP Error: #{response.code}"
  end
end

#hostObject



13
14
15
16
# File 'lib/code_climate/test_reporter/client.rb', line 13

def host
  ENV["CODECLIMATE_API_HOST"] ||
    "https://codeclimate.com"
end

#post_results(result) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/code_climate/test_reporter/client.rb', line 49

def post_results(result)
  uri = URI.parse("#{host}/test_reports")
  http = http_client(uri)

  request = Net::HTTP::Post.new(uri.path)
  request["User-Agent"] = USER_AGENT
  request["Content-Type"] = "application/json"

  if CodeClimate::TestReporter.configuration.gzip_request
    request["Content-Encoding"] = "gzip"
    request.body = compress(result.to_json)
  else
    request.body = result.to_json
  end

  response = http.request(request)

  if response.code.to_i >= 200 && response.code.to_i < 300
    response
  else
    raise "HTTP Error: #{response.code}"
  end
end