Class: AtlasRb::Work

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

Constant Summary collapse

ROUTE =
"/works/"

Class Method Summary collapse

Methods inherited from Resource

preview

Methods included from FaradayHelper

#connection, #multipart

Class Method Details

.create(id, xml_path = nil) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/atlas_rb/work.rb', line 11

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



19
20
21
# File 'lib/atlas_rb/work.rb', line 19

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

.find(id) ⇒ Object



7
8
9
# File 'lib/atlas_rb/work.rb', line 7

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

.metadata(id, values) ⇒ Object



30
31
32
# File 'lib/atlas_rb/work.rb', line 30

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

.mods(id, kind = nil) ⇒ Object



34
35
36
37
38
39
# File 'lib/atlas_rb/work.rb', line 34

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

.update(id, xml_path) ⇒ Object



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

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