Class: AtlasRb::Resource
- Inherits:
-
Object
- Object
- AtlasRb::Resource
- Extended by:
- FaradayHelper
- Defined in:
- lib/atlas_rb/resource.rb
Overview
Abstract base for every Atlas resource type.
Subclasses define a ROUTE constant (e.g. "/communities/") and override
whichever of find / create / destroy / update / metadata / mods apply.
The Resource class itself ships four endpoints that are not
type-specific: a generic resolver, an XML preview helper, a permissions
lookup, and an audit-event history fetch.
The Atlas resource hierarchy is:
{Community} → {Collection} → {Work} → {FileSet} → {Blob}
Subclasses extend FaradayHelper so that connection(...) and
multipart(...) are available as class methods.
Class Method Summary collapse
-
.find(id, nuid: nil, on_behalf_of: nil) ⇒ Hash{String => String, Hash}
Resolve any Atlas resource by ID without knowing its type up front.
-
.find_many(ids, nuid: nil, on_behalf_of: nil) ⇒ Array<AtlasRb::Mash>
Resolve many resources by NOID in a single round-trip.
-
.history(id, nuid: nil, on_behalf_of: nil) ⇒ AtlasRb::Mash
Fetch the audit-event history for a resource.
-
.mods_version(id, version_id, kind: nil, nuid: nil, on_behalf_of: nil) ⇒ String
Fetch the MODS document as of a specific version.
-
.mods_versions(id, nuid: nil, on_behalf_of: nil) ⇒ AtlasRb::Mash
List the retained MODS versions for a resource.
-
.permissions(id, nuid: nil, on_behalf_of: nil) ⇒ Hash
Fetch the access-control entries for a resource.
-
.preview(xml_path, nuid: nil, on_behalf_of: nil) ⇒ String
Validate a MODS XML document against Atlas's schema without persisting it.
Methods included from FaradayHelper
connection, multipart, system_connection
Class Method Details
.find(id, nuid: nil, on_behalf_of: nil) ⇒ Hash{String => String, Hash}
Resolve any Atlas resource by ID without knowing its type up front.
The Atlas server returns a single-key JSON object whose key names the
resource type ("community", "collection", "work", etc.); this
method splits that into a normalized { "klass" => ..., "resource" => ... }
pair so callers can dispatch on type.
42 43 44 45 46 47 48 |
# File 'lib/atlas_rb/resource.rb', line 42 def self.find(id, nuid: nil, on_behalf_of: nil) result = JSON.parse( connection({}, nuid, on_behalf_of: on_behalf_of).get('/resources/' + id)&.body ) AtlasRb::Mash.new("klass" => result.first[0].capitalize, "resource" => result.first[1]) end |
.find_many(ids, nuid: nil, on_behalf_of: nil) ⇒ Array<AtlasRb::Mash>
Resolve many resources by NOID in a single round-trip.
Wraps Atlas's POST /resources/find_many, which returns one lightweight
digest per resolvable resource — { "id", "noid", "klass", "title",
"thumbnail", "tombstoned" } — rather than full typed payloads. Use it
anywhere a set of ids would otherwise be resolved with a find-per-id
fan-out (breadcrumb chains, linked-member lists, load-destination
pickers): one HTTP call instead of N.
The ids travel in the request body, so the list is not bounded by
URL length. The result is unordered and may be shorter than the
input — unresolvable ids are dropped silently, and tombstoned
resources come back flagged ("tombstoned" => true) rather than
omitted. Index the result by "noid"; do not assume positional
correspondence with ids.
81 82 83 84 85 86 |
# File 'lib/atlas_rb/resource.rb', line 81 def self.find_many(ids, nuid: nil, on_behalf_of: nil) JSON.parse( connection({}, nuid, on_behalf_of: on_behalf_of) .post('/resources/find_many', JSON.dump(ids: Array(ids)))&.body ).map { |node| AtlasRb::Mash.new(node) } end |
.history(id, nuid: nil, on_behalf_of: nil) ⇒ AtlasRb::Mash
Add pagination support once Atlas's history endpoint exposes page / per_page query params. Today the endpoint returns the full history in one shot.
Fetch the audit-event history for a resource.
Wraps Atlas's GET /resources/<id>/history endpoint, which returns the
full envelope (resource_id + reverse-chronological events array).
The whole envelope is preserved so callers can confirm the events
belong to the requested resource; access events as result["events"].
Authorization errors (401 / 403) are intentionally not caught
here — they surface as raw Faraday responses for the calling
application's rescue layer to translate.
163 164 165 166 167 168 |
# File 'lib/atlas_rb/resource.rb', line 163 def self.history(id, nuid: nil, on_behalf_of: nil) AtlasRb::Mash.new(JSON.parse( connection({}, nuid, on_behalf_of: on_behalf_of) .get('/resources/' + id + '/history')&.body )) end |
.mods_version(id, version_id, kind: nil, nuid: nil, on_behalf_of: nil) ⇒ String
Fetch the MODS document as of a specific version.
Wraps Atlas's GET /resources/<id>/mods/versions/<version_id> and
returns the raw response body (not parsed) — mirroring
Work.mods. Pass a version_id obtained from mods_versions (an
opaque OCFL vN label).
Only XML is version-recoverable: the JSON access copy is overwritten in
place, so the server serves historical XML (the default). kind: is
accepted for parity with Work.mods but XML is currently the only
supported format. An unknown version yields a 404 (raw Faraday
response).
239 240 241 242 243 244 |
# File 'lib/atlas_rb/resource.rb', line 239 def self.mods_version(id, version_id, kind: nil, nuid: nil, on_behalf_of: nil) connection({}, nuid, on_behalf_of: on_behalf_of).get( '/resources/' + id + '/mods/versions/' + version_id + (kind.present? ? ".#{kind}" : '') )&.body end |
.mods_versions(id, nuid: nil, on_behalf_of: nil) ⇒ AtlasRb::Mash
List the retained MODS versions for a resource.
Wraps Atlas's GET /resources/<id>/mods/versions, which returns the
full envelope — resource_id plus a reverse-chronological versions
array — as an AtlasRb::Mash. Each version descriptor mirrors the
audit-event shape (version_id, created, actor_nuid,
on_behalf_of_nuid, source, note) so the two streams render with
the same helpers; actor fields are correlated from the audit log and
may be null when a version has no matching edit event.
Type-agnostic: pass any Modsable resource ID (Community, Collection,
Work). A resource with no MODS comes back as { "versions" => [] }.
Version labels are opaque, sortable OCFL vN strings — not a 1-based
counter — so treat them as identifiers to feed back into
mods_version, not as ordinals. The server admin-gates this endpoint
(it exposes edit attribution); 401 / 403 surface as raw Faraday
responses, matching history.
203 204 205 206 207 208 |
# File 'lib/atlas_rb/resource.rb', line 203 def self.mods_versions(id, nuid: nil, on_behalf_of: nil) AtlasRb::Mash.new(JSON.parse( connection({}, nuid, on_behalf_of: on_behalf_of) .get('/resources/' + id + '/mods/versions')&.body )) end |
.permissions(id, nuid: nil, on_behalf_of: nil) ⇒ Hash
Fetch the access-control entries for a resource.
126 127 128 129 130 131 |
# File 'lib/atlas_rb/resource.rb', line 126 def self.(id, nuid: nil, on_behalf_of: nil) AtlasRb::Mash.new(JSON.parse( connection({}, nuid, on_behalf_of: on_behalf_of) .get('/resources/' + id + '/permissions')&.body ))["resource"] end |
.preview(xml_path, nuid: nil, on_behalf_of: nil) ⇒ String
Validate a MODS XML document against Atlas's schema without persisting it.
Useful for surfacing validation errors in UIs before the user commits.
104 105 106 107 108 109 |
# File 'lib/atlas_rb/resource.rb', line 104 def self.preview(xml_path, nuid: nil, on_behalf_of: nil) payload = { binary: Faraday::Multipart::FilePart.new(File.open(xml_path), "application/xml", File.basename(xml_path)) } multipart(nuid, on_behalf_of: on_behalf_of).post('/resources/preview', payload)&.body end |