Class: AtlasRb::Community

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

Overview

A top-level grouping in the Atlas hierarchy.

Communities are organizational containers — they hold Collections and, optionally, sub-Communities. Most institutional structure (departments, programs, projects) is modeled as a tree of Communities with Collections at the leaves.

See also: Collection, 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.

"/communities/"

Class Method Summary collapse

Methods inherited from Resource

permissions, preview

Methods included from FaradayHelper

#connection, #multipart

Class Method Details

.children(id) ⇒ Array<String>

List the immediate children (sub-Communities and Collections) of a Community.

The endpoint returns just the noids; resolve each through Resource.find (which dispatches by type) when richer payloads are needed.

Examples:

AtlasRb::Community.children("c-123")
# => ["fn106x926", "kw52j804p"]

Parameters:

  • id (String)

    the parent Community ID.

Returns:

  • (Array<String>)

    child noids from GET /communities/<id>/children.



114
115
116
# File 'lib/atlas_rb/community.rb', line 114

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

.create(id = nil, xml_path = nil) ⇒ Hash

Create a new Community, optionally seeded with MODS metadata.

Pass id = nil to create a top-level Community; pass a Community ID to nest the new Community beneath an existing one.

Examples:

Top-level community, no metadata

AtlasRb::Community.create(nil)

Sub-community seeded from MODS

AtlasRb::Community.create("c-parent", "/tmp/dept-mods.xml")

Parameters:

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

    the parent Community ID, or nil for a top-level Community.

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

    optional path to a MODS XML file. When given, the Community is created and immediately patched with the metadata in the file; the returned Hash reflects the patched state.

Returns:

  • (Hash)

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



48
49
50
51
52
53
54
# File 'lib/atlas_rb/community.rb', line 48

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

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

.destroy(id) ⇒ Faraday::Response

Delete a Community.

Examples:

AtlasRb::Community.destroy("c-123")

Parameters:

  • id (String)

    the Community ID.

Returns:

  • (Faraday::Response)

    the raw delete response.



63
64
65
# File 'lib/atlas_rb/community.rb', line 63

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

.find(id) ⇒ Hash

Fetch a single Community by ID.

Examples:

AtlasRb::Community.find("c-123")
# => { "id" => "c-123", "title" => "College of Engineering", ... }

Parameters:

  • id (String)

    the Community ID.

Returns:

  • (Hash)

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



26
27
28
# File 'lib/atlas_rb/community.rb', line 26

def self.find(id)
  AtlasRb::Mash.new(JSON.parse(connection({}).get(ROUTE + id)&.body))["community"]
end

.metadata(id, values) ⇒ Hash

Patch individual descriptive-metadata fields without uploading a full MODS document.

Scoped to user-authored descriptive metadata only. Programmatic writes of machine-set Delegate URIs (thumbnails) have their own purpose-specific endpoint — see set_thumbnails.

Examples:

AtlasRb::Community.("c-123", title: "New Name")

Parameters:

  • id (String)

    the Community ID.

  • values (Hash)

    field-level metadata updates (shape determined by the Atlas server, typically a mapping from MODS field name to value).

Returns:

  • (Hash)

    the parsed JSON response.



147
148
149
# File 'lib/atlas_rb/community.rb', line 147

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

.mods(id, kind = nil) ⇒ String

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

Examples:

HTML rendering for display

AtlasRb::Community.mods("c-123", "html")

Parameters:

  • id (String)

    the Community ID.

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

    one of "json" (default when omitted), "html", or "xml". When nil, Atlas returns its default representation.

Returns:

  • (String)

    the raw response body (JSON, HTML, or XML serialized as a string).



189
190
191
192
193
194
# File 'lib/atlas_rb/community.rb', line 189

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

.restore(id, nuid:) ⇒ Faraday::Response

Restore a previously tombstoned Community.

Operator-only. Restoration is intentionally not exposed in any end-user UI; call this from a Rails console session (or a future admin panel) when the library has decided an object should come back.

Examples:

Operator restoring from bundle exec rails console

AtlasRb::Community.restore("c-123", nuid: "000000002")

Parameters:

  • id (String)

    the Community ID.

  • nuid (String)

    the acting user's NUID.

Returns:

  • (Faraday::Response)

    the raw response.



98
99
100
# File 'lib/atlas_rb/community.rb', line 98

def self.restore(id, nuid:)
  connection({}, nuid).post(ROUTE + id + '/restore')
end

.set_thumbnails(id, thumbnail: nil, thumbnail_2x: nil, preview: nil) ⇒ AtlasRb::Mash

Attach the three thumbnail/preview Delegate URIs to a Community.

Community-level mirror of Work.set_thumbnails. Atlas dispatches each non-blank URI to its matching Delegate role (thumbnail_image / thumbnail_image_2x / preview_image) via DelegateUpdater. Missing keys are left untouched.

Examples:

AtlasRb::Community.set_thumbnails(
  "c-123",
  thumbnail:    "https://iiif.example.edu/iiif/3/m.jp2/full/!85,85/0/default.jpg",
  thumbnail_2x: "https://iiif.example.edu/iiif/3/m.jp2/full/!170,170/0/default.jpg",
  preview:      "https://iiif.example.edu/iiif/3/m.jp2/full/500,/0/default.jpg"
)

Parameters:

  • id (String)

    the Community ID.

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

    IIIF URI for the ~85² thumbnail.

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

    IIIF URI for the ~170² 2x thumbnail.

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

    IIIF URI for the ~500w preview image.

Returns:



171
172
173
174
175
176
# File 'lib/atlas_rb/community.rb', line 171

def self.set_thumbnails(id, thumbnail: nil, thumbnail_2x: nil, preview: nil)
  body = { thumbnail: thumbnail, thumbnail_2x: thumbnail_2x, preview: preview }.compact
  AtlasRb::Mash.new(JSON.parse(
    connection({}).patch(ROUTE + id + '/thumbnails', JSON.dump(body))&.body
  ))
end

.tombstone(id, nuid:) ⇒ Faraday::Response

Tombstone (withdraw) a Community.

The Community remains in Atlas storage but is marked as withdrawn: search and show pages return a withdrawn stub for every user. Atlas rejects the request with 422 has_live_children if the Community still has live (non-tombstoned) members.

Examples:

AtlasRb::Community.tombstone("c-123", nuid: "000000002")

Parameters:

  • id (String)

    the Community ID.

  • nuid (String)

    the acting user's NUID, stamped on the resource as tombstoned_by for audit purposes.

Returns:

  • (Faraday::Response)

    the raw response. 200/204 on success; 422 with {"code":"has_live_children"} if the Community is not empty.



82
83
84
# File 'lib/atlas_rb/community.rb', line 82

def self.tombstone(id, nuid:)
  connection({}, nuid).post(ROUTE + id + '/tombstone')
end

.update(id, xml_path) ⇒ Hash

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

Examples:

AtlasRb::Community.update("c-123", "/tmp/community-mods.xml")

Parameters:

  • id (String)

    the Community ID.

  • xml_path (String)

    path to a MODS XML file on disk.

Returns:

  • (Hash)

    the parsed JSON response from the patch.



126
127
128
129
130
131
# File 'lib/atlas_rb/community.rb', line 126

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