Skip to content

Instantly share code, notes, and snippets.

@jambun

jambun/enumangler.rb

Last active Aug 29, 2015
Embed
What would you like to do?
enumangler
require 'net/http'
require 'json'
require 'uri'
require 'optparse'
class Enumangler
def initialize(opts)
@backend_url = URI.parse(opts[:backend_url])
@username = opts[:username]
@password = opts[:password]
@commit = opts[:commit] || false
@session = nil
end
def list
enums = get_enums
enums.each do |enum|
if enum['editable']
puts enum['name']
else
puts "*#{enum['name']}*"
end
end
end
def show(name)
enum = get_enum(name)
display(enum)
end
def add(name, values)
enum = get_enum(name)
enum['values'] = enum['values'] + values
post_request(enum)
display(enum)
end
def set_editable(name, editable)
enum = get_enum(name)
enum['editable'] = editable
post_request(enum)
enum = get_enum(name)
if enum['editable'] == editable
display(enum)
else
puts "Unable to set a value for editable."
puts "To make editable editable (!), edit schemas/enumerations.rb as follows:"
puts '- "editable" => {"type" => "boolean", "readonly" => true},'
puts '+ "editable" => {"type" => "boolean"},'
puts "Then restart your backend."
end
end
private
def get_enums
ensure_session
response = get_request("/config/enumerations")
raise "Error: #{response.body}" unless response.code == '200'
JSON.parse(response.body)
end
def get_enum(enum)
get_enums.select { |e| e['name'] == enum }.first
end
def display(enum)
if enum['editable']
puts enum['name']
else
puts "#{enum['name']} *not editable*"
end
enum['values'].each do |v|
puts " #{v}"
end
end
def ensure_session
return if @session
response = Net::HTTP.post_form(URI.join(@backend_url, "/users/#{@username}/login"),
'password' => @password)
raise "Login failed" unless response.code == '200'
@session = JSON.parse(response.body)['session']
end
def get_request(uri, data = nil)
http = Net::HTTP.new(@backend_url.host, @backend_url.port)
request = Net::HTTP::Get.new(uri)
request['X-ArchivesSpace-Session'] = @session
request.set_form_data(data) if data
http.request(request)
end
def post_request(record)
http = Net::HTTP.new(@backend_url.host, @backend_url.port)
request = Net::HTTP::Post.new(record['uri'])
request['X-ArchivesSpace-Session'] = @session
request.body = record.to_json
response = http.request(request)
raise "Enumeration is not editable" if response.code == '403'
raise "Save failed" unless response.code == '200'
end
end
###
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: enumangler.rb [options]"
opts.on('-a', '--backendurl URL', 'ArchivesSpace backend URL') { |v| options[:backend_url] = v }
opts.on('-u', '--username USERNAME', 'Username for backend session') { |v| options[:username] = v }
opts.on('-p', '--password PASSWORD', 'Password for backend session') { |v| options[:password] = v }
opts.on('-l', '--list', 'List all enumerations') { |v| options[:list] = v }
opts.on('-s', '--select ENUM', 'Select an enumeration') { |v| options[:select] = v }
opts.on('-e', '--edit', 'Make the selected enumeration editable') { |v| options[:editable] = v }
opts.on('-u', '--unedit', 'Make the selected enumeration uneditable') { |v| options[:uneditable] = v }
opts.on('-v', '--value VALUES', 'Add comma separated values to selected enumeration') { |v| options[:values] = v }
opts.on("-h", "--help", "Prints this help") { puts opts; exit }
end.parse!
default_options = {
:backend_url => 'http://localhost:4567',
:username => 'admin',
:password => 'admin',
}
options = default_options.merge(options)
unless options[:backend_url]
puts("Please specify a backend_url")
exit
end
unless options[:username]
puts("Please specify a username")
exit
end
unless options[:password]
puts("Please specify a password")
exit
end
enumangler = Enumangler.new(options)
if options[:select]
if options[:editable]
enumangler.set_editable(options[:select], true)
elsif options[:uneditable]
enumangler.set_editable(options[:select], false)
elsif options[:values]
enumangler.add(options[:select], options[:values].split(','))
else
enumangler.show(options[:select])
end
else
enumangler.list
end
{
"editable" => {"type" => "boolean", "readonly" => false}
}
@jambun

This comment has been minimized.

Copy link
Owner Author

@jambun jambun commented May 8, 2015

Drop the enumeration_ext.rb file in plugins/local/schemas/ and add local to your plugins list in config.rb to enable the -e and -u options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.