Skip to content

Instantly share code, notes, and snippets.

@LaurensRietveld
Last active August 29, 2015 14:07
Show Gist options
  • Save LaurensRietveld/eebde750f87c52cdfa58 to your computer and use it in GitHub Desktop.
Save LaurensRietveld/eebde750f87c52cdfa58 to your computer and use it in GitHub Desktop.
#YASQE: Update YASQE configuration via ajax call

In some use cases, it is useful to fetch the YASQE configuration from an external URL. This allows you to e.g. fetch the shown YASQE query from an externally hosted page. This might be interesting when you've published your SPARQL queries in separate files as part of your documentation, and you would like to use YASQE as a means of accessing it. This example gist does exactly that: when a 'queryRef' URL parameter is passed, it fetches the YASQE query with an asynchronous call from the URL.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Parsing URL arguments for YASQE</title>
<link href='http://cdn.jsdelivr.net/yasqe/2.0/yasqe.min.css' rel='stylesheet' type='text/css'/>
</head>
<body>
<div id="yasqe"></div>
<!-- include three libs in 1 request (see https://github.com/jsdelivr/jsdelivr#url-structure for more info) -->
<script src="http://cdn.jsdelivr.net/g/jquery@1.11.1,codemirror@4.5.0,yasqe@2.0"></script>
<script src="yasqeInit.js"></script>
</body>
var consumeUrl = function(yasqe, args) {
//change query and endpoint value if there are any
if (args.query) yasqe.setValue(args.query);
if (args.endpoint) yasqe.options.sparql.endpoint = args.endpoint;
//want to consume other arguments such as the request type (POST/GET), or arguments to send to endpoint
//feel free to add them in this function as well.
//as you see, all options you can specify in the default settings, are configurable via yasqe.options as well
//Or, if you want to configure yasqe via a remote url (e.g. a query in some file elsewhere),
//feel free to do so!
//This example uses a cors proxy to access a github file containing a query
if (args.queryRef) $.get(args.queryRef, function(result){yasqe.setValue(result);});
};
var yasqe = YASQE(document.getElementById("yasqe"), {
sparql: {
showQueryButton: true
},
consumeShareLink: consumeUrl
});
@LaurensRietveld
Copy link
Author

A note: As this is run from client-side, you might encounter CORS issues. Make sure the file is CORS-enabled (which e.g. GitHub is not)

For GitHub there is one workaround though. If you'd like to include a file from github, use their API:
$.get("https://api.github.com/repos/<account-name>/<repository>/contents/<file path in repo>", function(data) {yasqe.setValue(atob(data.content))});

What this does is:

  • issue a get request to the github API for this particular file
  • get the content info out of the json
  • this content is base64 encoded, so decode this info (supported in practically all modern browsers, except IE < 10 (about 8% of current web users))
  • set the yasqe value (considering this file contains a query string)

@jneubert
Copy link

Thanks a lot! With the kind help of Laurens, I've created a public hub for executing queries on GitHub at http://zbw.eu/beta/sparql-gui. It can be used (without any warranty) to execute arbitrary queries saved on GitHub via a query like this:

http://zbw.eu/beta/sparql-gui/?endpoint=http://zbw.eu/beta/sparql/stwv/query&queryRef=https://api.github.com/repos/jneubert/skos-history/contents/sparql/count_added_concepts.rq

to execute the query at https://github.com/jneubert/skos-history/blob/master/sparql/count_added_concepts.rq on the endpoint given in the URL. (For more stuff like this, see expample queries for the skos-history projekt.)

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