Skip to content

Instantly share code, notes, and snippets.

@jweisman
Last active October 9, 2020 18:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jweisman/43eb094e44f6b00e8c10e5eb14e28ca9 to your computer and use it in GitHub Desktop.
Save jweisman/43eb094e44f6b00e8c10e5eb14e28ca9 to your computer and use it in GitHub Desktop.
Linked Data example - Alma JSONLD and BibCard
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title><%= viewmodel[:title] %></title>
</head>
<body>
<div class="container">
<h1>Author Card for <%= viewmodel[:title] %></h1>
<p>&nbsp;</p>
<div class="card" style="width: 18rem;">
<h5 class="card-header">Author</h5>
<img class="card-img-top" src="<%= viewmodel[:thumbnail] %>" alt="Card image cap">
<div class="card-body">
<h5 class="card-title"><%= viewmodel[:author_name] %></h5>
<h6 class="card-subtitle mb-2 text-muted"><%= viewmodel[:lifespan] %></h6>
<p class="card-text"><%= viewmodel[:desc] %></p>
<a href="<%= viewmodel[:url] %>" class="btn btn-primary" target="_new">More info</a>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
require 'bib_card'
require 'erb'
require 'rest-client'
JSONLD_URL = "https://open-na.hosted.exlibrisgroup.com/alma/TR_INTEGRATION_INST/bibs/"
LCNAF_PREFIX = "/id.loc.gov/"
abort 'Please provide MMS_ID' if !ARGV[0]
puts "Retrieving JSONLD for MMS_ID #{ARGV[0]}"
jsonld = JSON.parse(RestClient.get(JSONLD_URL + ARGV[0]), symbolize_names: true)
if jsonld[:creator].is_a?(Array)
creator = jsonld[:creator].find {|i| i[:@id].include?(LCNAF_PREFIX)}
else
creator = jsonld[:creator] if jsonld[:creator][:@id].include?(LCNAF_PREFIX)
end
if creator
lcnaf_uri = creator[:@id]
else
abort 'No LC Names URI found' if !lcnaf_uri
end
puts "Retrieving LC Names for #{lcnaf_uri}"
person = BibCard.person(lcnaf_uri)
viewmodel = {
title: jsonld[:title],
author_name: person.name(["en", "en-US"]),
lifespan: 'Born: ' + Date.parse(person.birth_date).strftime("%v") +
(person.death_date ? ', Deceased: ' + Date.parse(person.death_date).strftime("%v") : ''),
desc: person.wikidata_entity.description,
thumbnail: person.dbpedia_resource.thumbnail,
url: lcnaf_uri
}
filename = lcnaf_uri.split('/')[-1] + ".html"
puts "Writing author card file #{filename}"
renderer = ERB.new File.read("bibcard.html.erb")
File.write filename, renderer.result()
system("open #{filename}")
@jweisman
Copy link
Author

This Gist uses the BibCard gem from UW Madison. Installation instructions are available at the repository readme.

Note that the demo there didn't work because of this error downstream in Spira:

ruby/gems/2.4.0/gems/spira-3.0.0/lib/spira/base.rb:288:in `reset_changes': undefined method `clear' for nil:NilClass (NoMethodError)

For now I commented out this line in base.rb in my local gem:

#@changed_attributes.clear

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment