This repository has been archived on 2023-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
coverage-check-action/lib/github_client.rb
Miguel Savignano b796f9a6dd Refactor files structure (#20)
add tests and fix #2
2019-10-28 20:16:37 -04:00

41 lines
880 B
Ruby

# frozen_string_literal: true
class GithubClient
def initialize(github_token, user_agent: 'ruby')
@github_token = github_token
@user_agent = user_agent
end
def patch(url, body = {})
request_http do |http|
http.patch(url, body.to_json, headers)
end
end
def post(url, body = {})
request_http do |http|
http.post(url, body.to_json, headers)
end
end
private
def headers
@headers ||= {
"Content-Type": 'application/json',
"Accept": 'application/vnd.github.antiope-preview+json',
"Authorization": "Bearer #{@github_token}",
"User-Agent": @user_agent
}
end
def request_http
http = Net::HTTP.new('api.github.com', 443)
http.use_ssl = true
response = yield(http)
raise "#{response.message}: #{response.body}" if response.code.to_i >= 300
JSON.parse(response.body)
end
end