Module: BerkeleyLibrary::TIND::API

Extended by:
Logging, Util
Defined in:
lib/berkeley_library/tind/api/api.rb,
lib/berkeley_library/tind/api/format.rb,
lib/berkeley_library/tind/api/search.rb,
lib/berkeley_library/tind/api/collection.rb,
lib/berkeley_library/tind/api/date_range.rb,
lib/berkeley_library/tind/api/api_exception.rb

Defined Under Namespace

Classes: APIException, APIKeyNotSet, BaseURINotSet, Collection, DateRange, Format, Search

Class Method Summary collapse

Class Method Details

.api_base_uriURI

Gets the API base URI.

Returns:

  • (URI)

    the API base URI



30
31
32
33
34
# File 'lib/berkeley_library/tind/api/api.rb', line 30

def api_base_uri
  return if Config.blank?((base_uri = Config.base_uri))

  URIs.append(base_uri, '/api/v1')
end

.api_keyString?

Gets the TIND API key.

Returns:

  • (String, nil)

    the TIND API key, or nil if not set.



18
19
20
# File 'lib/berkeley_library/tind/api/api.rb', line 18

def api_key
  BerkeleyLibrary::TIND::Config.api_key
end

.format_query(params) ⇒ Object

Raises:

  • (ArgumentError)


98
99
100
101
102
103
# File 'lib/berkeley_library/tind/api/api.rb', line 98

def format_query(params)
  return unless params
  return URI.encode_www_form(params.to_hash) if params.respond_to?(:to_hash)

  raise ArgumentError, "Argument #{params.inspect} does not appear to be a set of query parameters"
end

.format_request(uri, params = nil, method = 'GET') ⇒ Object

Returns a formatted string version of the request, suitable for logging or error messages.

Parameters:

  • uri (URI, String)

    the URI

  • params (Hash, nil) (defaults to: nil)

    the query parameters

  • method (String) (defaults to: 'GET')

    the request method



91
92
93
94
95
96
# File 'lib/berkeley_library/tind/api/api.rb', line 91

def format_request(uri, params = nil, method = 'GET')
  query_string = format_query(params)
  uri = URIs.append(uri, '?', query_string) if query_string

  "#{method} #{uri}"
end

.get(endpoint, **params) ⇒ String .get(endpoint, **params) {|body| ... } ⇒ Object

Makes a GET request.

Overloads:

  • .get(endpoint, **params) ⇒ String

    Makes a GET request to the specified endpoint with the specified parameters, and returns the response body as a string. Example:

    marc_xml = API.get(:search, c: 'The Bancroft Library')
    XMLReader.new(marc_xml).each { |record| ... }
    

    Parameters:

    • endpoint (Symbol)

      the API endpoint, e.g. :search or :collection

    • **params (Hash)

      the query parameters

    Returns:

    • (String)

      the response body

  • .get(endpoint, **params) {|body| ... } ⇒ Object

    Makes a GET request to the specified endpoint with the specified parameters, and yields an IO that streams the response body. Example:

    API.get(:search, c: 'The Bancroft Library') do |body|
      XMLReader.new(body).each { |record| ... }
    end
    

    Parameters:

    • endpoint (Symbol, String)

      the API endpoint, e.g. :search or :collections

    • **params (Hash)

      the query parameters

    Yield Parameters:

    • body (IO)

      the response body, as an IO stream

Raises:



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/berkeley_library/tind/api/api.rb', line 73

def get(endpoint, **params, &block)
  endpoint_uri = uri_for(endpoint)
  raise BaseURINotSet.new(endpoint, params) if Config.blank?(endpoint_uri)

  logger.debug(format_request(endpoint_uri, params))

  body = do_get(endpoint_uri, params)
  return body unless block_given?

  stream_response_body(body, &block)
end

.uri_for(endpoint) ⇒ URI

Gets the URI for the specified API endpoint.

Parameters:

  • endpoint (Symbol, String)

    the endpoint (e.g. :search or :collection)

Returns:

  • (URI)

    the URI for the specified endpoint

Raises:



40
41
42
43
44
# File 'lib/berkeley_library/tind/api/api.rb', line 40

def uri_for(endpoint)
  return if Config.blank?(api_base_uri)

  URIs.append(api_base_uri, endpoint)
end

.user_agentString

Gets the value to send in the User-Agent header

Returns:

  • (String)

    the user agent



24
25
26
# File 'lib/berkeley_library/tind/api/api.rb', line 24

def user_agent
  BerkeleyLibrary::TIND::Config.user_agent
end