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

Constant Summary collapse

UTF_8 =
Encoding::UTF_8

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



25
26
27
# File 'lib/berkeley_library/util/uris.rb', line 25

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

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

Performs a GET request and returns the response body as a string.

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 unsuccessful request.



36
37
38
# File 'lib/berkeley_library/util/uris.rb', line 36

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, even in the event of a failed 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:

  • (RestClient::Response)

    the body as a string.



59
60
61
# File 'lib/berkeley_library/util/uris.rb', line 59

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

#head(uri, params: {}, headers: {}) ⇒ Integer

Performs a HEAD request and returns the response status as an integer. Note that unlike BerkeleyLibrary::Util::URIs::Requester.get, this does not raise an error in the event of an unsuccessful request.

Parameters:

  • uri (URI, String)

    the URI to HEAD

  • 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:

  • (Integer)

    the response code as an integer.



48
49
50
# File 'lib/berkeley_library/util/uris.rb', line 48

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

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

Performs a HEAD request and returns the response, even in the event of a failed request.

Parameters:

  • uri (URI, String)

    the URI to HEAD

  • 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.



70
71
72
# File 'lib/berkeley_library/util/uris.rb', line 70

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

#path_escape(s) ⇒ Object

Escapes the specified string so that it can be used as a URL path segment, replacing disallowed characters (including /) with percent-encodings as needed.

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
# File 'lib/berkeley_library/util/uris.rb', line 85

def path_escape(s)
  raise ArgumentError, "Can't escape #{s.inspect}: not a string" unless s.respond_to?(:encoding)
  raise ArgumentError, "Can't escape #{s.inspect}: expected #{UTF_8}, was #{s.encoding}" unless s.encoding == UTF_8

  ''.tap do |escaped|
    s.bytes.each do |b|
      escaped << (should_escape?(b, :path_segment) ? '%%%02X' % b : b.chr)
    end
  end
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.



100
101
102
103
104
105
106
# File 'lib/berkeley_library/util/uris.rb', line 100

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.



79
80
81
# File 'lib/berkeley_library/util/uris.rb', line 79

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