class NS1::Client

Constants

API_ENDPOINT

Public Class Methods

new(api_key) click to toggle source
# File lib/NS1/client.rb, line 19
def initialize(api_key)
  @api_key = api_key
end

Public Instance Methods

request(http_method, endpoint, params = nil) click to toggle source
# File lib/NS1/client.rb, line 23
def request(http_method, endpoint, params = nil)
  response = client.public_send(http_method, endpoint, params)
  parsed_response = Oj.load(response.body)

  return parsed_response if response.status == HTTP_OK_CODE

  raise error_class(response.status), Oj.load(response.body)["message"]

rescue Oj::ParseError
  raise ResponseParseError, response.body
end

Private Instance Methods

client() click to toggle source
# File lib/NS1/client.rb, line 50
def client
  @client ||= Faraday.new(API_ENDPOINT) do |client|
    client.headers['X-NSONE-Key'] = @api_key
  end
end
error_class(status) click to toggle source
# File lib/NS1/client.rb, line 37
def error_class(status)
  case status
  when HTTP_BAD_REQUEST_CODE
    BadRequestError
  when HTTP_UNAUTHORIZED_CODE
    UnauthorizedError
  when HTTP_NOT_FOUND_CODE
    NotFoundError
  else
    ApiError
  end
end