Skip to content

Instantly share code, notes, and snippets.

@pgwillia
Last active October 6, 2021 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgwillia/1116c38a0c1c28969c19f2007a2ace08 to your computer and use it in GitHub Desktop.
Save pgwillia/1116c38a0c1c28969c19f2007a2ace08 to your computer and use it in GitHub Desktop.
Datacite API planning
require "ostruct"
@item = OpenStruct.new(creators: ["Joe Shmoe"], title: "Test title", descripton: "Test description", sort_year: 2021)
def datacite_metadata
{
data: {
attributes: {
creators: @item.creators.map{|creator| {name: creator}},
titles: [{
title: @item.title
}],
descriptions: [{
description: @item.description
}],
publisher: "University of Alberta Library",
publicationYear: @item.sort_year,
types: {
resourceType: "Text/Book",
resourceTypeGeneral: "Text"
},
url: "http://era.ualberta.localhost/items/bc74bb99-f25c-43bf-80b1-35bfeaac39c2",
schemaVersion: "http://datacite.org/schema/kernel-4"
}
}
}
end
require 'uri'
require 'net/http'
require 'openssl'
url = URI("https://api.test.datacite.org/dois")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/vnd.api+json'
request.basic_auth(username, password)
request.body = dm.to_json
response = http.request(request)
data = JSON[response.read_body]
require 'uri'
require 'net/http'
require 'openssl'
url = URI("https://api.test.datacite.org/dois/" + doi)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Put.new(url)
request["Content-Type"] = 'application/vnd.api+json'
request.body = du.to_json
response = http.request(request)
data = JSON[response.read_body]
dm = datacite_metadata
dm[:data][:attributes][:event] = "publish"
dm[:data][:attributes][:prefix] = "10.80243"
dm.to_json
"{\"data\":{\"attributes\":{\"creators\":[{\"name\":\"Joe Shmoe\"}],\"titles\":[{\"title\":\"Test title\"}],\"descriptions\":[{\"description\":null}],\"publisher\":\"University of Alberta Library\",\"publicationYear\":2021,\"types\":{\"resourceType\":\"Text/Book\",\"resourceTypeGeneral\":\"Text\"},\"url\":\"http://era.ualberta.localhost/items/bc74bb99-f25c-43bf-80b1-35bfeaac39c2\",\"schemaVersion\":\"http://datacite.org/schema/kernel-4\",\"event\":\"publish\",\"prefix\":\"10.80243\"}}}"
du = datacite_metadata
du[:data][:attributes][:titles] = [{title: "Updated title"}]
du
{:data=>{:attributes=>{:creators=>[{:name=>"Joe Shmoe"}], :titles=>[{:title=>"Updated title"}], :descriptions=>[{:description=>nil}], :publisher=>"University of Alberta Library", :publicationYear=>2021, :types=>{:resourceType=>"Text/Book", :resourceTypeGeneral=>"Text"}, :url=>"http://era.ualberta.localhost/items/bc74bb99-f25c-43bf-80b1-35bfeaac39c2", :schemaVersion=>"http://datacite.org/schema/kernel-4"}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment