class Codeclimate::Collectors::Pagerduty::ApiClient
Constants
- BASE_URL
- NotFound
- ResponseError
- ServerError
Attributes
api_token[R]
Public Class Methods
new(api_token)
click to toggle source
# File lib/codeclimate/collectors/pagerduty/api_client.rb, line 15 def initialize(api_token) @api_token = api_token end
Public Instance Methods
get(path, params = {})
click to toggle source
# File lib/codeclimate/collectors/pagerduty/api_client.rb, line 19 def get(path, params = {}) uri = construct_uri(path, params) req = Net::HTTP::Get.new(uri) set_headers(req) make_request(req) end
Private Instance Methods
construct_uri(path, params = {})
click to toggle source
# File lib/codeclimate/collectors/pagerduty/api_client.rb, line 63 def construct_uri(path, params = {}) URI.parse(BASE_URL).tap do |uri| if path.start_with?("/") uri.path = path else uri.path = "/#{path}" end if params.any? uri.query = URI.encode_www_form(params) end end end
http_client()
click to toggle source
# File lib/codeclimate/collectors/pagerduty/api_client.rb, line 53 def http_client @http_client ||= begin base_uri = URI.parse(BASE_URL) Net::HTTP.new(base_uri.hostname, base_uri.port).tap do |c| c.use_ssl = true end end end
make_request(request)
click to toggle source
# File lib/codeclimate/collectors/pagerduty/api_client.rb, line 30 def make_request(request) resp = http_client.request(request) case resp when Net::HTTPSuccess JSON.parse(resp.body) when Net::HTTPNotFound raise NotFound, "Resource not found" when Net::HTTPUnauthorized raise Unauthorized, "Invalid credentials" when Net::HTTPServerError raise ServerError, "Server error" else raise ResponseError, "Unsuccessful request: status=#{resp.status}" end end
set_headers(req)
click to toggle source
# File lib/codeclimate/collectors/pagerduty/api_client.rb, line 47 def set_headers(req) req["Authorization"] = "Token token=#{api_token}" req["Content-Type"] = "application/json" req["Accept"] = "application/vnd.pagerduty+json;version=2" end