-
Notifications
You must be signed in to change notification settings - Fork 52
/
scan_coverity_api.rb
59 lines (49 loc) · 1.38 KB
/
scan_coverity_api.rb
1
2
3
4
5
6
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true
class ScanCoverityApi
URL = ENV['COVERITY_SCAN_URL']
class << self
def resource_uri(path = nil, _query = {})
URI("#{URL}/#{path}.json")
end
def get_response(path = nil, query = {})
uri = resource_uri(path, query)
response = Net::HTTP.get_response(uri)
handle_errors(response) { JSON.parse(response.body) }
end
def save(path = nil, query = {})
uri = resource_uri(path, query)
response = Net::HTTP.post_form(uri, query)
handle_errors(response) do
hsh = JSON.parse(response.body)
set_attributes_or_errors(response, hsh)
end
rescue JSON::ParserError
response.body
end
private
def handle_errors(response)
case response
when Net::HTTPServerError
raise ScanCoverityApiError, "#{response.message} => #{response.body}"
else
yield
end
end
def save_success?(response)
response.is_a?(Net::HTTPSuccess)
end
def set_errors(hsh)
@errors = hsh.key?('error') ? hsh['error'].with_indifferent_access : hsh
false
end
def set_attributes(hsh)
@attributes = hsh
hsh.each do |key, value|
instance_variable_set("@#{key}", value)
end
end
def set_attributes_or_errors(response, hsh)
save_success?(response) ? set_attributes(hsh) : set_errors(hsh)
end
end
end