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

Instance Method Summary collapse

Instance Method Details

#batch_post_results(files) ⇒ Object



17
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
# File 'lib/code_climate/test_reporter/client.rb', line 17

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.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



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

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

#post_results(result) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/code_climate/test_reporter/client.rb', line 47

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

  request = Net::HTTP::Post.new(uri.path)
  request["Content-Type"] = "application/json"
  request.body = result.to_json

  response = http.request(request)

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