This is the fourth and final post in a series (1, 2, 3, 4) of providing background and tutorial material about the British National Bibliography. The tutorials were written as part of some freelance work I did for the British Library at the end of 2012. The material was used as input to creating the new documentation for their Linked Data platform but hasn’t been otherwise published. They are now published here with permission of the BL.
The British National Bibliography (BNB) is a bibliographic database that contains data on a wide range of books and serial publications published in the UK and Ireland since the 1950s. The database is available under a public domain license and can be accessed via an online API which supports the SPARQL query language.
This tutorial provides an example of building a simple web application using the BNB SPARQL endpoint using Ruby and various open source libraries. The tutorial includes:
- a description of the application and its intended behaviour
- a summary of the various open source components used to build the application
- a description of how SPARQL is used to implement the application functionality
The “Find Me A Book!” Application
The Find Me a Book! demonstration application illustrates how to use the data in the BNB to recommend books to readers. The following design brief describes the intended behaviour.
The application will allow a user to provide an ISBN which is used to query the BNB in order find other books that the user might potentially want to read. The application will also confirm the book title to the user to ensure that it has found the right information.
Book recommendations will be made in two ways:
- More By The Author: will provide a list of 10 other books by the same author(s)
- More From Reading Lists: will attempt to suggest 10 books based on series or categories in the BNB data
The first use case is quite straight-forward and should generate some “safe” recommendations: it’s likely that the user will like other works by the author.
The second attempts will use the BNB data a little more creatively and so the suggestions are likely to be a little more varied.
Related books will be found by looking to see if the user’s book is in a series. If it is then the application will recommend other books from that series. If the book is not included in any series, then recommendations will be driven off the standard subject classifications. The idea is that series present ready made reading lists that are a good source of suggestions. By falling back to a broader categorisation, the user should always be presented with some recommendations.
To explore the recommended books further, the user will be provided with links to LibraryThing.com.
The Application Code
The full source code of the application is available on github.com. The code has been placed into the Public Domain so can be freely reused or extended.
The application is written in Ruby and should run on Ruby 1.8.7 or higher. Several open source frameworks were used to build the application:
- Sinatra — a light-weight Ruby web application framework
- SPARQL Client — an client library for accessing SPARQL endpoints from Ruby
- The Boostrap CSS framework is used to build the basic page layout
The application code is very straight-forward and can be separated into server-side and client-side components.
The server side implementation can be found in
app.rb. The Ruby application delivers the application assets (CSS, images, etc) and also exposes several web services that act as proxies for the BNB dataset. These services submit SPARQL queries to the BNB SPARQL endpoint and then process the results to generate a custom JSON output.
The three services, which each accept an
isbn parameter are:
/title— find a book title from an ISBN. Example output and implementation
/by-author— uses an ISBN to find more books by an author. Example output and implementation
/related— uses an ISBN to related books based on either series or category relationships. Example output and implementation
Each of the services works in essentially the same way:
isbnparameter is extracted from the request. If the parameter is not found then an error is returned to the client. The ISBN value is also normalised to remove any spaces or dashes
- A SPARQL client object is created to provide a way to interact with the SPARQL endpoint
- The ISBN parameter is injected into the SPARQL query that will be run against the BNB, using the
- The final query is then submitted to the SPARQL endpoint and the results used to build the JSON response
/related service may actually makes two calls to the endpoint. If the first query doesn’t return any results then a fallback query is used instead.
find-me-a-book.js. It uses the JQuery library to trigger custom code to be executed when the user submits the search form with an ISBN.
findTitle function calls the
/title service to attempt to resolve the ISBN into the title of a book. This checks that the ISBN is in the BNB and provides useful feedback for the user.
If this initial call succeeds then the
find function is called twice to submit parallel AJAX requests. One to the
/by-author service, and one to the
/related service. The function accepts two parameters, the first parameter identifies the service to call, the second provides a name that is used to guide the processing of the results.
The HTML markup uses a naming convention to allow the
find function to write the results of the request into the correct parts of the page, depending on its second parameter.
The ISBN and title information found in the results from the AJAX requests are used to build links to the LibraryThing website. But these could also be processed in other ways, e.g. to provide multiple links or invoke other APIs.
Installing and Running the Application
A live instance of the application has been deployed to allow the code to be tested without having to install and run it locally. The application can be found at:
For readers interested in customising the application code, this section provides instructions on how to access the source code and run the application.
The instructions have been tested on Ubuntu. Follow the relevant documentation links for help with installation of the various dependencies on other systems.
The application source code is available on Github and is organised into several directories:
views— the templates used in the application
src— the application source code, which is contained in
The additional files in the project directory provide support for deploying the application and installing the dependencies.
Running the Application
To download all of the source code and asserts, clone the git repository:
git clone https://github.com/ldodds/bnb-example-app.git
This will create a
bnb-example-app directory. To simplify the installation of further dependencies, the project uses the Bundler dependency management tool. This must be installed first:
sudo gem install bundle
Bundler can then be run to install the additional Ruby Gems required by the project:
cd bnb-example-app sudo bundle install
Once complete the application can be run as follows:
rackup application will then start the application as defined in
config.ru. By default the application will launch on port 9292 and should be accessible from:
This tutorial has introduced a simple demonstration application that illustrates one way of interacting with the BNB SPARQL endpoint. The application uses SPARQL queries to build a very simple book recommendation tool. The logic used to build the recommendations is deliberately simple to help illustrate the basic principles of working with the dataset and the API.
The source code for the application is available under a public domain license so can be customised or reused as necessary. A live instance provides a way to test the application against the real data.