Module: Codacy::ClientAPI

Defined in:
lib/codacy/client.rb

Class Method Summary collapse

Class Method Details

.loggerObject



53
54
55
# File 'lib/codacy/client.rb', line 53

def self.logger
  Codacy::Configuration.logger
end

.post_results(parsed_result) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/codacy/client.rb', line 7

def self.post_results(parsed_result)
  logger.info('Preparing payload...')
  logger.debug(parsed_result)

  project_token = ENV['CODACY_PROJECT_TOKEN']
  codacy_base_api = ENV['CODACY_BASE_API_URL'] || 'https://api.codacy.com'
  commit = Codacy::Git.commit_id
  url = codacy_base_api + '/2.0/coverage/' + commit + '/ruby'

  result = parsed_result.to_json

  if project_token.to_s == ''
    logger.error 'Could not find Codacy API token, make sure you have it configured in your environment.'
    false
  elsif commit.to_s == ''
    logger.error 'Could not find the current commit, make sure that you are running inside a git project.'
    false
  else
    logger.info('Posting ' + result.to_s.length.to_s + ' bytes to ' + url)
    VCR.turn_off! if Object.const_defined?('VCR') && VCR.respond_to?(:turn_off!)
    response = send_request(url, result, project_token)
    VCR.turn_on! if Object.const_defined?('VCR') && VCR.respond_to?(:turn_on!)

    logger.info(response)
    response.to_s.include? 'success'
  end
end

.send_request(url, content, project_token, redirects = 3) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/codacy/client.rb', line 35

def self.send_request(url, content, project_token, redirects = 3)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.path)
  http.use_ssl = uri.scheme == "https"
  request["project_token"] = project_token
  request["Content-Type"] = "application/json"
  request.body = content
  response = http.request(request)

  if [301, 302, 307].include? response.code.to_i and redirects > 0
    redirected_url = response.headers[:location]
    send_request(redirected_url, content, project_token, redirects - 1)
  else
    response.body
  end
end