├── .babelrc ├── .gitignore ├── LICENSE ├── README.md ├── examples ├── json │ ├── ex.es6 │ └── ex.js └── xml │ ├── ex.es6 │ └── ex.js ├── index.js ├── lib └── index.js └── package.json /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015", "stage-0"] 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2016 Carmine DiMascio 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dbpedia-sparql-client 2 | 3 | A promisified DBpedia SPARQL client that keeps it simple. 4 | 5 | ## Install 6 | 7 | `npm install dbpedia-sparql-client` 8 | 9 | #### Note: 10 | Requires fetch when running in a browser. If fetch is not available, install a [fetch polyfill](https://github.com/github/fetch) 11 | 12 | 13 | ## Use 14 | ### ES2015 15 | 16 | ```javascript 17 | import dps from 'dbpedia-sparql-client'; 18 | const query = `SELECT DISTINCT ?Concept WHERE {[] a ?Concept} LIMIT 10`; 19 | 20 | dps 21 | .client() 22 | .query(query) 23 | .timeout(15000) // optional, defaults to 10000 24 | .asJson() // or asXml() 25 | .then(r => { /* handle success */}) 26 | .catch(e => { /* handle error */}); 27 | 28 | ``` 29 | 30 | ### ES5 31 | ```javascript 32 | var dps = require('dbpedia-sparql-client').default; 33 | var query = 'SELECT DISTINCT ?Concept WHERE {[] a ?Concept} LIMIT 10'; 34 | 35 | dps.client() 36 | .query(query) 37 | .timeout(15000) // optional, defaults to 10000 38 | .asJson() // or asXml() 39 | .then(function(r) { /* handle success */ }) 40 | .catch(function(e) { /* handle error */ }); 41 | 42 | ``` 43 | 44 | 45 | ### License 46 | MIT 47 | -------------------------------------------------------------------------------- /examples/json/ex.es6: -------------------------------------------------------------------------------- 1 | import dps from '../../lib'; 2 | 3 | const query = `SELECT DISTINCT ?Concept WHERE {[] a ?Concept} LIMIT 10`; 4 | dps 5 | .client() 6 | .query(query) 7 | .asJson() 8 | .then(r => console.log(JSON.stringify(r))) 9 | .catch(e => console.error(e)) 10 | -------------------------------------------------------------------------------- /examples/json/ex.js: -------------------------------------------------------------------------------- 1 | var dps = require('../../index').default; 2 | var query = 'SELECT DISTINCT ?Concept WHERE {[] a ?Concept} LIMIT 10'; 3 | dps.client() 4 | .query(query) 5 | .asJson() 6 | .then(function(r) { 7 | console.log(JSON.stringify(r, null, 2)); 8 | }) 9 | .catch(function(e) { 10 | console.error("ERROR: "+e); 11 | }) 12 | -------------------------------------------------------------------------------- /examples/xml/ex.es6: -------------------------------------------------------------------------------- 1 | import dps from '../../lib'; 2 | 3 | const query = `SELECT DISTINCT ?Concept WHERE {[] a ?Concept} LIMIT 10`; 4 | dps 5 | .client() 6 | .query(query) 7 | .asXml() 8 | .then(r => console.log(JSON.stringify(r))) 9 | .catch(e => console.error(e)) 10 | -------------------------------------------------------------------------------- /examples/xml/ex.js: -------------------------------------------------------------------------------- 1 | var dps = require('../../index').default; 2 | var query = 'SELECT DISTINCT ?Concept WHERE {[] a ?Concept} LIMIT 10'; 3 | dps.client() 4 | .query(query) 5 | .asXml() 6 | .then(function(r) { 7 | console.log(r); 8 | }) 9 | .catch(function(e) { 10 | console.error("ERROR: "+e); 11 | }) 12 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); 8 | 9 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 10 | 11 | var isBrowser = new Function("try {return this===window;}catch(e){ return false;}"); 12 | var fetch = isBrowser() ? window.fetch : require('node-fetch'); 13 | 14 | var DbPediaSparql = function () { 15 | function DbPediaSparql() { 16 | _classCallCheck(this, DbPediaSparql); 17 | 18 | this._query = null; 19 | this._uri = 'https://dbpedia.org/sparql'; 20 | this._timeout = 10000; 21 | } 22 | 23 | _createClass(DbPediaSparql, [{ 24 | key: 'query', 25 | value: function query(_query) { 26 | this._query = _query; 27 | return this; 28 | } 29 | }, { 30 | key: 'timeout', 31 | value: function timeout(_timeout) { 32 | this._timeout = _timeout; 33 | return this; 34 | } 35 | }, { 36 | key: 'asJson', 37 | value: function asJson() { 38 | return this._execute({ format: 'json' }); 39 | } 40 | }, { 41 | key: 'asXml', 42 | value: function asXml() { 43 | return this._execute({ format: 'xml' }); 44 | } 45 | }, { 46 | key: '_execute', 47 | value: function _execute(opts) { 48 | if (!this._uri) { 49 | Promise.reject('no uri specified'); 50 | } 51 | if (!this._query) { 52 | Promise.reject('no query specified'); 53 | } 54 | 55 | var qs = { 56 | query: this._query, 57 | format: 'application/sparql-results+json', 58 | timeout: this._timeout 59 | }; 60 | 61 | var headers = { 62 | 'Content-Type': 'application/x-www-form-urlencoded', 63 | 'Accept': 'application/sparql-results+json' 64 | }; 65 | 66 | if (opts.format === 'xml') { 67 | delete qs.format; 68 | delete headers.Accept; 69 | } 70 | 71 | var qst = '?' + Object.keys(qs).map(function (key) { 72 | return key + '=' + encodeURIComponent(qs[key]); 73 | }).join('&'); 74 | 75 | return fetch(this._uri + qst, { 76 | headers: headers, 77 | method: 'GET', 78 | mode: 'cors' 79 | }).then(function (res) { 80 | if (opts.format === 'json') { 81 | return res.json(); 82 | } else { 83 | return res.text(); 84 | } 85 | }); 86 | } 87 | }], [{ 88 | key: 'client', 89 | value: function client() { 90 | return new DbPediaSparql(); 91 | } 92 | }]); 93 | 94 | return DbPediaSparql; 95 | }(); 96 | 97 | exports.default = DbPediaSparql; 98 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | const isBrowser = new Function("try {return this===window;}catch(e){ return false;}"); 2 | const fetch = (isBrowser()) ? window.fetch : require('node-fetch'); 3 | class DbPediaSparql { 4 | constructor() { 5 | this._query = null; 6 | this._uri = 'https://dbpedia.org/sparql'; 7 | this._timeout = 10000; 8 | } 9 | 10 | static client() { 11 | return new DbPediaSparql(); 12 | } 13 | 14 | query(query) { 15 | this._query = query; 16 | return this; 17 | } 18 | 19 | timeout(timeout) { 20 | this._timeout = timeout; 21 | return this; 22 | } 23 | 24 | asJson() { 25 | return this._execute({ format: 'json'}); 26 | } 27 | 28 | asXml() { 29 | return this._execute({ format: 'xml' }); 30 | } 31 | 32 | _execute(opts) { 33 | if (!this._uri) { 34 | Promise.reject('no uri specified'); 35 | } 36 | if (!this._query) { 37 | Promise.reject('no query specified'); 38 | } 39 | 40 | let qs = { 41 | query: this._query, 42 | format: 'application/sparql-results+json', 43 | timeout: this._timeout 44 | }; 45 | 46 | let headers = { 47 | 'Content-Type' : 'application/x-www-form-urlencoded', 48 | 'Accept' : 'application/sparql-results+json' 49 | }; 50 | 51 | if (opts.format === 'xml') { 52 | delete qs.format; 53 | delete headers.Accept; 54 | } 55 | 56 | const qst = '?'+Object.keys(qs) 57 | .map(key => `${key}=${encodeURIComponent(qs[key])}`) 58 | .join('&'); 59 | 60 | return fetch(this._uri+qst, { 61 | headers, 62 | method: 'GET', 63 | mode: 'cors' 64 | }) 65 | .then(res => { 66 | if (opts.format === 'json') { 67 | return res.json() 68 | } else { 69 | return res.text(); 70 | } 71 | }); 72 | } 73 | } 74 | 75 | export default DbPediaSparql; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dbpedia-sparql-client", 3 | "version": "0.9.8", 4 | "description": "A simple DBpedia SPARQL client", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "node index.js", 8 | "compile": "./node_modules/.bin/babel lib/index.js --out-file index.js ", 9 | "examples:json": "npm run compile && node examples/json/ex.js", 10 | "examples:xml": "npm run compile && node examples/xml/ex.js", 11 | "test": "echo \"Error: no test specified\" && exit 1" 12 | }, 13 | "keywords": [ 14 | "sparql", 15 | "javascript", 16 | "rdf", 17 | "dbpedia" 18 | ], 19 | "repository": { 20 | "type": "git", 21 | "url": "https://github.com/cdimascio/dbpedia-sparql-client" 22 | }, 23 | "author": "Carmine DiMascio (https://github.com/cdimascio)", 24 | "license": "MIT", 25 | "dependencies": { 26 | "node-fetch": "^1.5.1" 27 | }, 28 | "devDependencies": { 29 | "babel-cli": "^6.0.0", 30 | "babel-preset-es2015": "^6.6.0", 31 | "babel-preset-stage-0": "^6.5.0", 32 | "babel-register": "^6.7.2" 33 | } 34 | } 35 | --------------------------------------------------------------------------------