Class: CodeClimate::TestReporter::API

Inherits:
Object
  • Object
show all
Defined in:
lib/codeclimate-test-reporter.rb

Class Method Summary collapse

Class Method Details

.hostObject



11
12
13
14
# File 'lib/codeclimate-test-reporter.rb', line 11

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

.post_results(result) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/codeclimate-test-reporter.rb', line 16

def self.post_results(result)
  uri = URI.parse("#{host}/test_reports")
  http = Net::HTTP.new(uri.host, uri.port)

  if uri.scheme == "https"
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  end

  http.open_timeout = 5 # in seconds
  http.read_timeout = 5 # in seconds

  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