Module: BerkeleyLibrary::Util::URIs::Requester

Extended by:
Logging
Defined in:
lib/berkeley_library/util/uris/requester.rb

Class Method Summary collapse

Class Method Details

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



20
21
22
23
# File 'lib/berkeley_library/util/uris/requester.rb', line 20

def get(uri, params: {}, headers: {})
  resp = make_request(:get, uri, params, headers)
  resp.body
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.



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

def get_response(uri, params: {}, headers: {})
  make_request(:get, uri, params, headers)
rescue RestClient::Exception => e
  e.response
end

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

Performs a HEAD request and returns the response status as an integer. Note that unlike 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.



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

def head(uri, params: {}, headers: {})
  head_response(uri, params: params, headers: headers).code
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.



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

def head_response(uri, params: {}, headers: {})
  make_request(:head, uri, params, headers)
rescue RestClient::Exception => e
  e.response
end