Skip to content

Instantly share code, notes, and snippets.

@pbinkley
Created February 22, 2016 17:59
Show Gist options
  • Select an option

  • Save pbinkley/6a55cbb00da21b660880 to your computer and use it in GitHub Desktop.

Select an option

Save pbinkley/6a55cbb00da21b660880 to your computer and use it in GitHub Desktop.
Fedora4 REST API demo
require 'rest-client'
# we use active_fedora/noid to generate pairtrees from noids
require 'active_fedora/noid'
filename = ARGV[0]
raise "missing argument: filename" unless filename
instance = 'dev'
$user = 'fedoraAdmin'
$password = 'fedoraAdmin'
$rest = 'http://@192.168.33.17:8983/fedora/rest'
$originalroot = $rest
def patch(path, payload)
puts 'Patching ' + path + ': ' + payload
response = RestClient::Request.execute(
method: :patch,
url: $rest + path,
user: $user,
password: $password,
payload: payload,
headers: {:content_type => "application/sparql-update", cache_control: :no_cache})
return response.code
end
File.readlines(filename).each do |noid|
# start a transaction
transaction = RestClient::Request.execute method: :post, url: $originalroot + '/fcr:tx', user: $user, password: $password
puts 'Create transaction: ' + transaction.code.to_s
# use the transaction URI for all further operations
$rest = transaction.headers[:location]
# note: treeify adds a trailing CR
pairtree = ActiveFedora::Noid.treeify(noid).strip
# the pairtree path of the item in question
$path = "/" + instance + "/" + pairtree
# delete all creators
payload = 'PREFIX ns006: <http://purl.org/dc/terms/> DELETE WHERE {<> ns006:creator ?x}'
puts 'Result: ' + patch($path, payload).to_s
# add new creator
payload = 'PREFIX ns006: <http://purl.org/dc/terms/> INSERT DATA {<> ns006:creator "Lovelace, Ada"}'
puts 'Result: ' + patch($path, payload).to_s
# commit the transaction
transaction = RestClient::Request.execute method: :post, url: $rest + '/fcr:tx/fcr:commit', user: $user, password: $password
puts 'Commit transaction: ' + transaction.code.to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment