Module: BerkeleyLibrary::Util::URIs

Extended by:
URIs
Includes:
Logging
Included in:
URIs
Defined in:
lib/berkeley_library/util/uris.rb,
lib/berkeley_library/util/uris/appender.rb,
lib/berkeley_library/util/uris/requester.rb,
lib/berkeley_library/util/uris/validator.rb

Defined Under Namespace

Modules: Requester, Validator Classes: Appender

Instance Method Summary collapse

Instance Method Details

#append(uri, *elements) ⇒ URI

Appends the specified paths to the path of the specified URI, removing any extraneous slashes and merging additional query parameters, and returns a new URI with that path and the same scheme, host, query, fragment, etc. as the original.

Parameters:

  • uri (URI, String)

    the original URI

  • elements (Array<String, Symbol>)

    the URI elements to join.

Returns:

  • (URI)

    a new URI appending the joined path elements.

Raises:

  • URI::InvalidComponentError if appending the specified elements would create an invalid URI



23
24
25
# File 'lib/berkeley_library/util/uris.rb', line 23

def append(uri, *elements)
  Appender.new(uri, *elements).to_uri
end

#get(uri, params: {}, headers: {}) ⇒ String

Performs a GET request.

Parameters:

  • uri (URI, String)

    the URI to GET

  • params (Hash) (defaults to: {})

    the query parameters to add to the URI. (Note that the URI may already include query parameters.)

  • headers (Hash) (defaults to: {})

    the request headers.

Returns:

  • (String)

    the body as a string.

Raises:

  • (RestClient::Exception)

    in the event of an error.



34
35
36
# File 'lib/berkeley_library/util/uris.rb', line 34

def get(uri, params: {}, headers: {})
  Requester.get(uri, params: params, headers: headers)
end

#get_response(uri, params: {}, headers: {}) ⇒ RestClient::Response

Performs a GET request and returns the response.

Parameters:

  • uri (URI, String)

    the URI to GET

  • params (Hash) (defaults to: {})

    the query parameters to add to the URI. (Note that the URI may already include query parameters.)

  • headers (Hash) (defaults to: {})

    the request headers.

Returns:

  • (RestClient::Response)

    the body as a string.



44
45
46
# File 'lib/berkeley_library/util/uris.rb', line 44

def get_response(uri, params: {}, headers: {})
  Requester.get_response(uri, params: params, headers: headers)
end

#safe_parse_uri(url) ⇒ URI?

Returns the specified URL as a URI, or nil if the URL cannot be parsed.

Parameters:

  • url (Object, nil)

    the URL.

Returns:

  • (URI, nil)

    the URI, or nil.



61
62
63
64
65
66
67
# File 'lib/berkeley_library/util/uris.rb', line 61

def safe_parse_uri(url)
  # noinspection RubyMismatchedArgumentType
  uri_or_nil(url)
rescue URI::InvalidURIError => e
  logger.warn("Error parsing URL #{url.inspect}", e)
  nil
end

#uri_or_nil(url) ⇒ URI

Returns the specified URL as a URI, or nil if the URL is nil.

Parameters:

  • url (String, URI, nil)

    the URL.

Returns:

  • (URI)

    the URI, or nil.

Raises:

  • (URI::InvalidURIError)

    if url is not nil and cannot be parsed as a URI.



53
54
55
# File 'lib/berkeley_library/util/uris.rb', line 53

def uri_or_nil(url)
  Validator.uri_or_nil(url)
end