class GOVUK::Client::URLArbiter

Constants

VERSION

Public Class Methods

new(base_url = nil) click to toggle source

@param base_url [String] the base URL for the service (eg

https://url-arbiter.example.com).  If unspecified, this will be
looked up with {https://github.com/alphagov/plek Plek}.
# File lib/govuk/client/url_arbiter.rb, line 16
def initialize(base_url = nil)
  base_url ||= Plek.new.find('url-arbiter')
  @base_url = URI.parse(base_url)
end

Public Instance Methods

path(path) click to toggle source

Fetch details of a path

@param path [String] the path to fetch @return [Response, nil] Details of the reserved path, or nil if the path wasn't found. @raise [Errors::InvalidPath] when called with an invalid path.

# File lib/govuk/client/url_arbiter.rb, line 26
def path(path)
  check_path(path)
  get_json("/paths#{path}")
end
reserve_path(path, details) click to toggle source

Reserve a path

@param path [String] the path to reserve. @param details [Hash] the request data to be sent to url-arbiter. @return [Response] Details of the reserved path. @raise [Errors::Conflict] if the path is already reserved by another app. @raise [Errors::UnprocessableEntity] for any validation errors. @raise [Errors::InvalidPath] when called with an invalid path.

# File lib/govuk/client/url_arbiter.rb, line 39
def reserve_path(path, details)
  check_path(path)
  put_json!("/paths#{path}", details)
end

Private Instance Methods

check_path(path) click to toggle source
# File lib/govuk/client/url_arbiter.rb, line 46
def check_path(path)
  unless path && path.start_with?("/")
    raise Errors::InvalidPath, "Path must start with a '/'"
  end
end
get_json(path) click to toggle source
# File lib/govuk/client/url_arbiter.rb, line 52
def get_json(path)
  response = RestClient.get(@base_url.merge(path).to_s)
  Response.new(response.code, response.body)
rescue RestClient::ResourceNotFound, RestClient::Gone
  nil
rescue RestClient::Exception => e
  raise Errors.create_for(e)
end
put_json!(path, data) click to toggle source
# File lib/govuk/client/url_arbiter.rb, line 61
def put_json!(path, data)
  json = MultiJson.dump(data)
  response = RestClient.put(@base_url.merge(path).to_s, json, {:content_type => 'application/json'})
  Response.new(response.code, response.body)
rescue RestClient::Exception => e
  raise Errors.create_for(e)
end