Class: Covered::Coveralls

Inherits:
Object
  • Object
show all
Defined in:
lib/covered/coveralls.rb

Defined Under Namespace

Classes: Wrapper

Constant Summary collapse

URL =
"https://coveralls.io/api/v1/jobs"

Instance Method Summary collapse

Constructor Details

#initialize(token: nil, service: nil, job_id: nil) ⇒ Coveralls

Returns a new instance of Coveralls.



50
51
52
# File 'lib/covered/coveralls.rb', line 50

def initialize(token: nil, service: nil, job_id: nil)
  @token = token
end

Instance Method Details

#call(wrapper, output = $stderr) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/covered/coveralls.rb', line 68

def call(wrapper, output = $stderr)
  if body = detect_service
    output.puts "Submitting data using #{body.inspect}..."
    
    source_files = []
    
    wrapper.each do |coverage|
      path = wrapper.relative_path(coverage.path)
      
      source_files << {
        name: path,
        source_digest: Digest::MD5.hexdigest(coverage.read),
        coverage: coverage.to_a,
      }
    end
    
    body[:source_files] = source_files
    
    Async do
      representation = Async::REST::Representation.new(
        Async::REST::Resource.for(URL),
        wrapper: Wrapper.new
      )
      
      begin
        response = representation.post(body)
        
        output.puts "Got response: #{response.read}"
        
      ensure
        representation.close
      end
    end
  end
end

#detect_serviceObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/covered/coveralls.rb', line 54

def detect_service
  if token = ENV.fetch('COVERALLS_REPO_TOKEN', @token)
    return {"repo_token" => token}
  elsif @service && @job_id
    return {"service_name" => @service, "service_job_id" => @job_id}
  elsif job_id = ENV['TRAVIS_JOB_ID']
    return {"service_name" => "travis-ci", "service_job_id" => job_id}
  else
    warn "#{self.class} can't detect service! Please specify COVERALLS_REPO_TOKEN."
  end
  
  return nil
end