Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yulgit1/6f70a961956a90dbec4d975da731f024 to your computer and use it in GitHub Desktop.
Save yulgit1/6f70a961956a90dbec4d975da731f024 to your computer and use it in GitHub Desktop.
ruby semantic handle example
#from diggit-hydra
#use gem "savon"
handles_enabled: true
handle_wsdl: http://link.odai.yale.edu/ypls-ws/PersistentLinking?wsdl
handle_prefix: 10079/digcoll
handle_group: 10079/DIGCOLL
handle_user: 10079/DIGCOLL
handle_credential: ****
handle_base: http://findit.library.yale.edu/catalog/
...
@handles_enabled = config.fetch("handles_enabled")
@handle_wsdl = config.fetch("handle_wsdl")
@handle_prefix = config.fetch("handle_prefix")
@handle_group = config.fetch("handle_group")
@handle_user = config.fetch("handle_user")
@handle_credential = config.fetch("handle_credential")
@handle_base = config.fetch("handle_base")
...
def create_handle(pid)
if @handles_enabled == true
logger.info("Creating handle for pid: " + pid)
semantic_handle = pid_to_semantic_handle(pid)
client = Savon.client(wsdl: @handle_wsdl)
envelope = handle_soap_envelope(pid,semantic_handle)
response = client.call(:create_batch_semantic, xml: envelope)
if response.success? == true
logger.info("Created handle: http://hdl.handle.net/"+semantic_handle)
logger.info("envelope: " + envelope)
logger.info("response: " + response.to_xml.to_s)
else
logger.error("Error creating handle for pid: "+pid)
logger.error("envelope: " + envelope)
logger.error("response: " + response.to_xml.to_s)
end
else
logger.info("Handles disabled, skipping call to handle service")
end
end
def pid_to_semantic_handle(pid)
ns = @handle_prefix[@handle_prefix.index("/")+1,@handle_prefix.size]+":"
if pid.include?(ns)
return @handle_prefix + "/" + pid.gsub(ns,"")
else
logger.error("Handle prefix namespace "+ns+" doesn't match pid namespace of pid "+pid)
raise "Handle prefix namespace "+ns+" doesn't match pid namespace of pid "+pid
end
end
def handle_soap_envelope(pid,semantic_handle)
envelope =
"<env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
"<env:Body>"+
"<tns:createBatchSemantic xmlns:tns=\"http://ws.ypls.odai.yale.edu/\">"+
"<handlesToValues><map><entry>"+
"<key>"+semantic_handle+"</key>"+
"<value>"+@handle_base+pid+"</value>"+
"</entry></map></handlesToValues>"+
"<group>"+@handle_group+"</group>"+
"<user>"+@handle_user+"</user>"+
"<credential>"+@handle_credential+"</credential>"+
"</tns:createBatchSemantic></env:Body></env:Envelope>"
envelope
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment