Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get all the ASPACE resources and their children too
#!/usr/bin/env ruby
require 'json'
require 'rest_client'
# point this to your AS backend
BACKEND_URL = "http://localhost:8089"
TOKEN = { "X-ArchivesSpace-Session" => JSON.parse( RestClient.post "#{BACKEND_URL}/users/admin/login", { password: 'admin'} )["session"] }
# Directory to dump JSON files
OUTPUT_DIR = "/tmp"
# recursively loads the children
def rear_child( child )
child["children"].map! { |grandchild| rear_child(grandchild) }
# adds the extra json values from the childs full record
child.merge! JSON( RestClient.get( "#{BACKEND_URL}/#{ child["record_uri"]}", TOKEN ) )
child
end
# get a list of repos
repos = JSON.parse( RestClient.get "#{BACKEND_URL}/repositories" ).collect { |repo| repo["uri"] }
repos.each do |repo|
# iterate thru list of resources in the repo
JSON(RestClient.get( "#{BACKEND_URL}/#{repo}/resources?all_ids=true", TOKEN )).each do |resource_id|
# get the resource
resource = JSON(RestClient.get( "#{BACKEND_URL}/#{repo}/resources/#{resource_id}/tree", TOKEN ) )
resource["children"].map! { |child| rear_child( child ) }
outfile = File.join(OUTPUT_DIR, "#{resource["record_uri"].gsub("/","_")}.json")
puts "Exporting #{resource["record_uri"]} to #{outfile}"
File.open(outfile, "w") { |file| file << resource.to_json }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment