Skip to content

Instantly share code, notes, and snippets.

@mejackreed
Forked from rosy1280/addTagScript.rb
Created January 16, 2016 00:02
Show Gist options
  • Save mejackreed/b295ba003ea1edbe8bff to your computer and use it in GitHub Desktop.
Save mejackreed/b295ba003ea1edbe8bff to your computer and use it in GitHub Desktop.
add new tag to a list of objects
#!/usr/bin/env ruby
# script should be placed in the common-accessioning/current/bin directory
# usage is ruby addTagScript.rb {env} druid-list.txt
# where {env} is the environment in which you want to run the script (production, test, development)
# and druid-list.txt is the list of druids you want to add tags to.
# NOTE: you need to change the tag, right now its set to add 'Project : Chinese Topographic Maps'
unless(ARGV.first.nil?)
ENV['ROBOT_ENVIRONMENT'] = ARGV.first
end
require File.expand_path(File.dirname(__FILE__) + '/../config/boot')
#pull the file from the commandline argument and read each line into the druids array
druids = []
druidlist = File.open(ARGV.second)
druidlist.each_line {|line|
druids.push line.chomp
}
druids.each do |druid|
begin
#format the druids
druid = "druid:#{druid}" unless druid.start_with?('druid')
#make sure that they exist in dor
@object=Dor::Item.find(druid)
#add the new tag and save
#TO DO: add the tag via a commandline argument, not hardcoded
@object.add_tag('Project : Chinese Topographic Maps')
@object.save
puts "completed #{druid}"
# TO DO: need to rescue error messages
# rescue
# RuntimeError => msg
# puts "#{druid}, #{msg}"
end
end
puts "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment