This gem provides a Ruby API for back-end data services used in the HMLR linked data applications. Specifically, it allows a simple expression language to be used to specify queries into an RDF data cube, in which a collection of data readings, known as measures are organised into a hyper-cube of two or more dimensions.
Originally, the expression language used by this gem was interpreted directly by the DsAPI. DsAPI presented a RESTful API in which data expressions could be translated systematically into SPARQL expressions, executed against a remote SPARQL endpoint, and then results returned to the caller in a compact JSON format.
In 2021, we took the decision to retire the DsAPI codebase, which has not been actively maintained for some time. In its place, we now expect to use Sapi-NT. Sapi-NT performs a similar function, in that it provides a RESTful API in which compact queries are translated into SPARQL expressions, and the results are available in (amongst other formats) JSON encoding. However, the input to Sapi-NT, in which we articulate the projection of the underlying hypercube that we require is encoded as URL parameters in an HTTP GET request. DsAPI, in contrast, expects the input query to be POSTed as a JSON expression.
To minimise changes to the client applications in which this gem is used, we have implemented a shim layer that accepts DsAPI expressions and re-codes them as Sapi-NT URLs. Similarly, differences in the returned JSON results formats are also ironed out by this shim layer. It is possible to do this because, once the designs of the applications had settled, the HMLR apps only use a subset of the expressive power of the DsAPI expression language.
It would be possible to simplify this code further, at the expense of needing to make changes to the calling application code. At the time, we did not believe this to be a cost-effective change, and no benefit to end-users (the internals of the query language are not exposed to end-users). This calculation may be different in future.
This gem requires Ruby >= 3.4.
To add this gem as a dependency to another Ruby project, add this line to your application's Gemfile:
source 'https://rubygems.pkg.github.com/epimorphics' do
gem 'data_services_api'
endN.B. An API URL needs to be provided by that project for the Service class in
order for the gem to work.
require 'data_services_api'
service = DataServicesApi::Service.new(url: 'https://example.landregistry.gov.uk')
dataset = service.dataset('ukhpi')
# A query is any object that responds to `terms` (a Hash of DsAPI expression
# terms) and `to_json`
query = Class.new do
def terms
{ '@and' => [
{ 'ukhpi:refMonth' => { '@ge' => { :@value => '2019-01', :@type => 'http://www.w3.org/2001/XMLSchema#gYearMonth' } } },
{ 'ukhpi:refRegion' => { '@eq' => { :@id => 'http://landregistry.data.gov.uk/id/region/united-kingdom' } } }
], '@sort' => [
{ '@down' => 'ukhpi:refMonth' }
], '@limit' => 1 }
end
def to_json(*_args)
terms.to_json
end
end.new
result = dataset.query(query)dataset.query translates the DsAPI-style expression into a Sapi-NT URL,
executes it against the configured API, and returns the result re-shaped back
into the legacy DsAPI JSON format.
DataServicesApi::Service.new accepts a config hash with the following keys,
all optional except url:
url- the base URL of the Sapi-NT API to query againstinstrumenter- an object responding toinstrument(name, payload), used to emit theresponse.api,connection_failure.apiandservice_exception.apinotifications described below. Defaults toActiveSupport::Notificationswhen running under Rails, otherwisenillogger- an object responding to the standardLoggerlevels (info,warn,error,debug), used to log request/response details. Defaults toRails.loggerwhen running under Rails, otherwisenilconnection_failed_retry_options- a hash offaraday-retryoptions (max,interval,interval_randomness,backoff_factor) applied toFaraday::ConnectionFailederrors (e.g. a colocated service restarting). Merged over the default ofmax: 4, interval: 0.5, interval_randomness: 0.25, backoff_factor: 2timeout_retry_options- same shape as above, applied toFaraday::TimeoutErrorerrors. Merged over the default ofmax: 2, interval: 0.25, interval_randomness: 0.5, backoff_factor: 2. Kept more conservative than the connection-failure retry budget since retrying an overloaded upstream aggressively can make things worse
Faraday::ResourceNotFound (404) responses are not retried, since a 404 is
not a transient failure.
Rubocop should not report any warnings:
$ bundle exec rubocop
Inspecting 21 files
.....................
21 files inspected, no offenses detectedYou will need to have started the HMLR Data API locally. To do so follow the instructions in the repository's README
Once the API is started you can invoke the tests with the simple command below:
bundle exec rake testYou can also set the environment variable API_URL to point to a running
instance of the HMLR Data API from a non-default port:
API_URL=http://localhost:8080 bundle exec rake testN.B If API_URL environment variable is not set it will default to
http://localhost:8888
This gem is published to the Epimorphics section of the GitHub Package
Registry (GPR). Previously we linked directly to the GitHub repo in the
Gemfiles of applications consuming this library, but this practice is now
anti-preferred.
The process is:
- Make the required code changes, and have them reviewed by other members of the team
- Before creating a release, you must:
- Bump
DataServicesApi::VERSIONinlib/data_services_api/version.rbfollowing semantic version principles - Update
CHANGELOG.md, moving theUnreleasedsection's contents under a new heading for the version being released - Run
bundle lock --local(orbundle install) to regenerateGemfile.lockwith the new version and commit the result — the release workflow runsbundle installin frozen/deployment mode, which fails if the lockfile still references the previous gem version
- Bump
- Check that the gem builds correctly by running
gem build data_services_api.gemspec- The local gem file will be ignored by the
.gitignorefile and not included in the recorded code changes in the repository.
- The local gem file will be ignored by the
- Push the changes to the
mainbranch via a pull request - On PR merge, create a GitHub Release (via the UI or
gh release create X.Y.Z --repo epimorphics/data_services_api)
Publishing the GitHub Release triggers the release workflow, which builds the gem and publishes it to the Epimorphics GitHub Package Registry automatically — no local credentials required.
This gem integrates with Prometheus monitoring by emitting the following
ActiveSupport::Notifications:
response.api- API response, including status code and durationconnection_failure.api- failure to connect to the API, with exception detailservice_exception.api- failure to process the API response