Class: AtlasRb::Work

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

Overview

The bibliographic unit in Atlas — an article, thesis, dataset, image, etc.

A Work belongs to exactly one Collection and aggregates one or more FileSets, each of which holds binary content via a Blob. MODS metadata is attached at the Work level.

See also: Collection, FileSet, Blob.

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.

"/works/"

Class Method Summary collapse

Methods inherited from Resource

permissions, preview

Methods included from FaradayHelper

#connection, #multipart

Class Method Details

.create(id, xml_path = nil) ⇒ Hash

Create a new Work in an existing Collection.

Note: unlike Community.create and Collection.create, the id parameter here is the parent Collection ID. The underlying request uses the collection_id query param rather than parent_id.

Examples:

Empty work, metadata to be added later

AtlasRb::Work.create("col-456")

Work seeded from MODS

AtlasRb::Work.create("col-456", "/tmp/work-mods.xml")

Parameters:

  • id (String)

    the parent Collection ID.

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

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

Returns:

  • (Hash)

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



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

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

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

.destroy(id) ⇒ Faraday::Response

Delete a Work.

Examples:

AtlasRb::Work.destroy("w-789")

Parameters:

  • id (String)

    the Work ID.

Returns:

  • (Faraday::Response)

    the raw delete response.



62
63
64
# File 'lib/atlas_rb/work.rb', line 62

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

.files(id) ⇒ Hash

List the FileSets and Blobs attached to a Work.

Useful for building download UIs — the response includes enough to render each file's display name, size, and download URL.

Examples:

AtlasRb::Work.files("w-789")

Parameters:

  • id (String)

    the Work ID.

Returns:

  • (Hash)

    the listing from GET /works/<id>/files.



103
104
105
# File 'lib/atlas_rb/work.rb', line 103

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

.find(id) ⇒ Hash

Fetch a single Work by ID.

Examples:

AtlasRb::Work.find("w-789")
# => { "id" => "w-789", "title" => "An Article", ... }

Parameters:

  • id (String)

    the Work ID.

Returns:

  • (Hash)

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



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

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

.metadata(id, values) ⇒ Hash

Patch individual metadata fields without uploading a full MODS document.

Examples:

AtlasRb::Work.("w-789", title: "Revised Title")

Parameters:

  • id (String)

    the Work ID.

  • values (Hash)

    field-level metadata updates.

Returns:

  • (Hash)

    the parsed JSON response.



89
90
91
# File 'lib/atlas_rb/work.rb', line 89

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

.mods(id, kind = nil) ⇒ String

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

Examples:

AtlasRb::Work.mods("w-789", "html")

Parameters:

  • id (String)

    the Work ID.

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

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

Returns:

  • (String)

    the raw response body in the requested format.



116
117
118
119
120
121
# File 'lib/atlas_rb/work.rb', line 116

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 Work's metadata by uploading a MODS XML document.

Examples:

AtlasRb::Work.update("w-789", "/tmp/work-mods.xml")

Parameters:

  • id (String)

    the Work ID.

  • xml_path (String)

    path to a MODS XML file on disk.

Returns:

  • (Hash)

    the parsed JSON response from the patch.



74
75
76
77
78
79
# File 'lib/atlas_rb/work.rb', line 74

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