class RIPE::HTTPRequest

Public Class Methods

new(client, object_type) click to toggle source
# File lib/ripe/http_request.rb, line 9
def initialize(client, object_type)
  @client = client
  @object_type = object_type
end

Public Instance Methods

create(password, object_hash) click to toggle source
# File lib/ripe/http_request.rb, line 19
def create(password, object_hash)
  json = request(Net::HTTP::Post, '', :query => {:password => password}, :json => object_hash.to_json)
  json.dig('objects', 'object').first
end
delete(password, key, reason = nil) click to toggle source
# File lib/ripe/http_request.rb, line 29
def delete(password, key, reason = nil)
  request(Net::HTTP::Delete, "/#{key}", :query => {:password => password, :reason => reason})
end
lookup(key) click to toggle source
# File lib/ripe/http_request.rb, line 14
def lookup(key)
  json = request(Net::HTTP::Get, "/#{key}")
  json.dig('objects', 'object').first
end
update(password, key, object_hash) click to toggle source
# File lib/ripe/http_request.rb, line 24
def update(password, key, object_hash)
  json = request(Net::HTTP::Put, "/#{key}", :query => {:password => password}, :json => object_hash.to_json)
  json.dig('objects', 'object').first
end

Private Instance Methods

request(request_type, path, options = {}) click to toggle source
# File lib/ripe/http_request.rb, line 35
def request(request_type, path, options = {})
  http = Net::HTTP.new(@client.mode == :test ? 'rest-test.db.ripe.net' : 'rest.db.ripe.net', 443)
  http.use_ssl = true
  path = "/#{@client.mode == :test ? 'test' : 'ripe'}/#{@object_type}#{path}"

  if options[:query]
    path << "?"
    path << URI.encode_www_form(options[:query])
  end

  request = request_type.new(path)
  request['Accept'] = 'application/json'

  if options[:json]
    request.add_field 'Content-Type', 'application/json'
    request.body = options[:json]
  end

  response = http.request(request)
  case response
  when Net::HTTPOK
    JSON.parse(response.body)
  when Net::HTTPNotFound
    raise RIPE::NotFound, "No resource found at #{path}"
  when Net::HTTPUnauthorized
    raise RIPE::AccessDenied, "Access denied to #{path}"
  when Net::HTTPBadRequest
    if response['Content-Type'] == 'application/json'
      body = JSON.parse(response.body)
      errors = body.dig('errormessages', 'errormessage')
      raise ValidationError.new(errors)
    else
      puts response.body
      raise RIPE::BadRequest, "Invalid request to API #{path}"
    end
  else
    puts response.inspect
    false
  end
end