Class: AtlasRb::Collection

Inherits:
Resource show all
Defined in:
lib/atlas_rb/collection.rb

Overview

A grouping of Works within a Community.

Collections are the leaf containers in the organizational tree — they hold Works directly. Every Collection has exactly one parent Community.

See also: Community, Work.

Constant Summary collapse

ROUTE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Atlas REST endpoint prefix for this resource.

"/collections/"

Class Method Summary collapse

Methods inherited from Resource

permissions, preview

Methods included from FaradayHelper

#connection, #multipart

Class Method Details

.children(id) ⇒ Hash

List the Works in a Collection.

Examples:

AtlasRb::Collection.children("col-456")

Parameters:

  • id (String)

    the Collection ID.

Returns:

  • (Hash)

    the child listing from GET /collections/<id>/children.



70
71
72
# File 'lib/atlas_rb/collection.rb', line 70

def self.children(id)
  JSON.parse(connection({}).get(ROUTE + id + '/children')&.body)
end

.create(id, xml_path = nil) ⇒ Hash

Create a new Collection under an existing Community.

Note: unlike AtlasRb::Community.create, the id parameter here is the parent Community ID (not a parent Collection ID — Collections do not nest).

Examples:

AtlasRb::Collection.create("c-123", "/tmp/collection-mods.xml")

Parameters:

  • id (String)

    the parent Community ID.

  • xml_path (String, nil) (defaults to: nil)

    optional path to a MODS XML file used to seed metadata. When given, the Collection is created and immediately patched with the metadata in the file.

Returns:

  • (Hash)

    the created Collection payload (post-update if xml_path was supplied).



44
45
46
47
48
49
50
# File 'lib/atlas_rb/collection.rb', line 44

def self.create(id, xml_path = nil)
  result = JSON.parse(connection({ parent_id: id }).post(ROUTE)&.body)["collection"]
  return result unless xml_path.present?

  update(result["id"], xml_path)
  find(result["id"])
end

.destroy(id) ⇒ Faraday::Response

Delete a Collection.

Examples:

AtlasRb::Collection.destroy("col-456")

Parameters:

  • id (String)

    the Collection ID.

Returns:

  • (Faraday::Response)

    the raw delete response.



59
60
61
# File 'lib/atlas_rb/collection.rb', line 59

def self.destroy(id)
  connection({}).delete(ROUTE + id)
end

.find(id) ⇒ Hash

Fetch a single Collection by ID.

Examples:

AtlasRb::Collection.find("col-456")
# => { "id" => "col-456", "title" => "Faculty Publications", ... }

Parameters:

  • id (String)

    the Collection ID.

Returns:

  • (Hash)

    the "collection" object, already unwrapped from the JSON response.



25
26
27
# File 'lib/atlas_rb/collection.rb', line 25

def self.find(id)
  JSON.parse(connection({}).get(ROUTE + id)&.body)["collection"]
end

.metadata(id, values) ⇒ Hash

Patch individual metadata fields without uploading a full MODS document.

Examples:

AtlasRb::Collection.("col-456", title: "Renamed Collection")

Parameters:

  • id (String)

    the Collection ID.

  • values (Hash)

    field-level metadata updates.

Returns:

  • (Hash)

    the parsed JSON response.



97
98
99
# File 'lib/atlas_rb/collection.rb', line 97

def self.(id, values)
  JSON.parse(connection({ metadata: values }).patch(ROUTE + id)&.body)
end

.mods(id, kind = nil) ⇒ String

Fetch the Collection's MODS representation in the requested format.

Examples:

AtlasRb::Collection.mods("col-456", "xml")

Parameters:

  • id (String)

    the Collection ID.

  • kind (String, nil) (defaults to: nil)

    one of "json" (default), "html", or "xml".

Returns:

  • (String)

    the raw response body in the requested format.



110
111
112
113
114
115
# File 'lib/atlas_rb/collection.rb', line 110

def self.mods(id, kind = nil)
  # json default, html, xml
  connection({}).get(
    ROUTE + id + '/mods' + (kind.present? ? ".#{kind}" : '')
    )&.body
end

.update(id, xml_path) ⇒ Hash

Replace a Collection's metadata by uploading a MODS XML document.

Examples:

AtlasRb::Collection.update("col-456", "/tmp/collection-mods.xml")

Parameters:

  • id (String)

    the Collection ID.

  • xml_path (String)

    path to a MODS XML file on disk.

Returns:

  • (Hash)

    the parsed JSON response from the patch.



82
83
84
85
86
87
# File 'lib/atlas_rb/collection.rb', line 82

def self.update(id, xml_path)
  payload = { binary: Faraday::Multipart::FilePart.new(File.open(xml_path),
                                                       "application/xml",
                                                       File.basename(xml_path)) }
  JSON.parse(multipart({}).patch(ROUTE + id, payload)&.body)
end