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) ⇒ Hash

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

Examples:

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

Parameters:

  • id (String)

    the parent Community ID.

Returns:

  • (Hash)

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



74
75
76
# File 'lib/atlas_rb/community.rb', line 74

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 = 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)
  JSON.parse(connection({}).get(ROUTE + id)&.body)["community"]
end

.metadata(id, values) ⇒ Hash

Patch individual metadata fields without uploading a full MODS document.

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.



102
103
104
# File 'lib/atlas_rb/community.rb', line 102

def self.(id, values)
  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).



117
118
119
120
121
122
# File 'lib/atlas_rb/community.rb', line 117

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



86
87
88
89
90
91
# File 'lib/atlas_rb/community.rb', line 86

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