Class: CoverallsMulti::API

Inherits:
Object
  • Object
show all
Defined in:
lib/coveralls-multi/api.rb

Overview

handles the post request to the Coveralls API

Class Method Summary collapse

Class Method Details

.add_api_config(hash) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/coveralls-multi/api.rb', line 48

def add_api_config(hash)
  config = CoverallsMulti::Config.api_config

  if CoverallsMulti::Config.debug_mode
    puts '[CoverallsMulti] Submitting with config:'
    output = JSON.pretty_generate(config).gsub(/"repo_token": ?"(.*?)"/, '"repo_token": "[secure]"')
    puts output
  end

  hash.merge(config)
end

.json_file(hash) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/coveralls-multi/api.rb', line 27

def json_file(hash)
  hash = add_api_config(hash)
  write_to_file(hash)
  file = nil

  Tempfile.open(['coveralls-upload', 'json']) do |f|
    f.write(hash.to_json)
    file = f
  end

  File.new(file.path, 'rb')
end

.post_json(hash) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/coveralls-multi/api.rb', line 9

def post_json(hash)
  url = CoverallsMulti::Config.api_endpoint
  puts "[CoverallsMulti] Submitting to #{url}"

  response = HTTParty.post(
    url,
    body: {
      json_file: json_file(hash),
    },
  )

  puts "[CoverallsMulti] #{response['message']}" if response['message']
  puts "[CoverallsMulti] #{response['url']}" if response['url']
  puts "[CoverallsMulti] Error: #{response['error']}" if response['error']

  response
end

.write_to_file(hash) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/coveralls-multi/api.rb', line 40

def write_to_file(hash)
  return unless CoverallsMulti::Config.debug_mode

  output_file_path = "#{CoverallsMulti::Config.root}/coveralls.json"
  puts "[CoverallsMulti] Debug mode on - writing results to #{output_file_path}"
  File.write(output_file_path, JSON.pretty_generate(hash))
end