From caf0c84e97ef1d5aee2a372ea8412cc77acc9dfe Mon Sep 17 00:00:00 2001 From: Rufus Pollock Date: Sun, 5 May 2013 18:03:52 +0100 Subject: [PATCH] [#314,be/ckan][s]: remove ckan backend from core as now in own repo https://github.com/okfn/recline.backend.ckan. --- _includes/backend-list.html | 2 +- _includes/recline-deps.html | 1 - docs/src/backend.ckan.html | 125 ---------------------- src/backend.ckan.js | 153 --------------------------- test/backend.ckan.test.js | 203 ------------------------------------ test/index.html | 2 - 6 files changed, 1 insertion(+), 485 deletions(-) delete mode 100644 docs/src/backend.ckan.html delete mode 100644 src/backend.ckan.js delete mode 100644 test/backend.ckan.test.js diff --git a/_includes/backend-list.html b/_includes/backend-list.html index 19542d5a..88e5f38d 100644 --- a/_includes/backend-list.html +++ b/_includes/backend-list.html @@ -4,7 +4,7 @@
  • solr: SOLR (partial)
  • elasticsearch: ElasticSearch
  • dataproxy: DataProxy (CSV and XLS on the Web)
  • -
  • ckan: CKAN – support for CKAN datastore
  • +
  • ckan: CKAN – support for CKAN datastore
  • couchdb: CouchDB
  • memory: Memory (local data)
  • diff --git a/_includes/recline-deps.html b/_includes/recline-deps.html index 6bdfeea9..f4d09e5f 100644 --- a/_includes/recline-deps.html +++ b/_includes/recline-deps.html @@ -55,7 +55,6 @@ - diff --git a/docs/src/backend.ckan.html b/docs/src/backend.ckan.html deleted file mode 100644 index 2130baa2..00000000 --- a/docs/src/backend.ckan.html +++ /dev/null @@ -1,125 +0,0 @@ - backend.ckan.js

    backend.ckan.js

    this.recline = this.recline || {};
    -this.recline.Backend = this.recline.Backend || {};
    -this.recline.Backend.Ckan = this.recline.Backend.Ckan || {};
    -
    -(function($, my) {

    CKAN Backend

    - -

    This provides connection to the CKAN DataStore (v2)

    - -

    General notes

    - -

    We need 2 things to make most requests:

    - -
      -
    1. CKAN API endpoint
    2. -
    3. ID of resource for which request is being made
    4. -
    - -

    There are 2 ways to specify this information.

    - -

    EITHER (checked in order):

    - -
      -
    • Every dataset must have an id equal to its resource id on the CKAN instance
    • -
    • The dataset has an endpoint attribute pointing to the CKAN API endpoint
    • -
    - -

    OR:

    - -

    Set the url attribute of the dataset to point to the Resource on the CKAN instance. The endpoint and id will then be automatically computed.

      my.__type__ = 'ckan';

    Default CKAN API endpoint used for requests (you can change this but it will affect every request!)

    - -

    DEPRECATION: this will be removed in v0.7. Please set endpoint attribute on dataset instead

      my.API_ENDPOINT = 'http://datahub.io/api';

    fetch

      my.fetch = function(dataset) {
    -    if (dataset.endpoint) {
    -      var wrapper = my.DataStore(dataset.endpoint);
    -    } else {
    -      var out = my._parseCkanResourceUrl(dataset.url);
    -      dataset.id = out.resource_id;
    -      var wrapper = my.DataStore(out.endpoint);
    -    }
    -    var dfd = $.Deferred();
    -    var jqxhr = wrapper.search({resource_id: dataset.id, limit: 0});
    -    jqxhr.done(function(results) {

    map ckan types to our usual types ...

          var fields = _.map(results.result.fields, function(field) {
    -        field.type = field.type in CKAN_TYPES_MAP ? CKAN_TYPES_MAP[field.type] : field.type;
    -        return field;
    -      });
    -      var out = {
    -        fields: fields,
    -        useMemoryStore: false
    -      };
    -      dfd.resolve(out);  
    -    });
    -    return dfd.promise();
    -  };

    only put in the module namespace so we can access for tests!

      my._normalizeQuery = function(queryObj, dataset) {
    -    var actualQuery = {
    -      resource_id: dataset.id,
    -      q: queryObj.q,
    -      limit: queryObj.size || 10,
    -      offset: queryObj.from || 0
    -    };
    -    if (queryObj.sort && queryObj.sort.length > 0) {
    -      var _tmp = _.map(queryObj.sort, function(sortObj) {
    -        return sortObj.field + ' ' + (sortObj.order || '');
    -      });
    -      actualQuery.sort = _tmp.join(',');
    -    }
    -    return actualQuery;
    -  }
    -
    -  my.query = function(queryObj, dataset) {
    -    if (dataset.endpoint) {
    -      var wrapper = my.DataStore(dataset.endpoint);
    -    } else {
    -      var out = my._parseCkanResourceUrl(dataset.url);
    -      dataset.id = out.resource_id;
    -      var wrapper = my.DataStore(out.endpoint);
    -    }
    -    var actualQuery = my._normalizeQuery(queryObj, dataset);
    -    var dfd = $.Deferred();
    -    var jqxhr = wrapper.search(actualQuery);
    -    jqxhr.done(function(results) {
    -      var out = {
    -        total: results.result.total,
    -        hits: results.result.records,
    -      };
    -      dfd.resolve(out);  
    -    });
    -    return dfd.promise();
    -  };

    DataStore

    - -

    Simple wrapper around the CKAN DataStore API

    - -

    @param endpoint: CKAN api endpoint (e.g. http://datahub.io/api)

      my.DataStore = function(endpoint) { 
    -    var that = {
    -      endpoint: endpoint || my.API_ENDPOINT
    -    };
    -    that.search = function(data) {
    -      var searchUrl = that.endpoint + '/3/action/datastore_search';
    -      var jqxhr = $.ajax({
    -        url: searchUrl,
    -        data: data,
    -        dataType: 'json'
    -      });
    -      return jqxhr;
    -    }
    -
    -    return that;
    -  };

    Parse a normal CKAN resource URL and return API endpoint etc

    - -

    Normal URL is something like http://demo.ckan.org/dataset/some-dataset/resource/eb23e809-ccbb-4ad1-820a-19586fc4bebd

      my._parseCkanResourceUrl = function(url) {
    -    parts = url.split('/');
    -    var len = parts.length;
    -    return {
    -      resource_id: parts[len-1],
    -      endpoint: parts.slice(0,[len-4]).join('/') + '/api'
    -    }
    -  };
    -
    -  var CKAN_TYPES_MAP = {
    -    'int4': 'integer',
    -    'int8': 'integer',
    -    'float8': 'float'
    -  };
    -
    -}(jQuery, this.recline.Backend.Ckan));
    -
    -
    \ No newline at end of file diff --git a/src/backend.ckan.js b/src/backend.ckan.js deleted file mode 100644 index 19998cab..00000000 --- a/src/backend.ckan.js +++ /dev/null @@ -1,153 +0,0 @@ -this.recline = this.recline || {}; -this.recline.Backend = this.recline.Backend || {}; -this.recline.Backend.Ckan = this.recline.Backend.Ckan || {}; - -(function(my) { - // ## CKAN Backend - // - // This provides connection to the CKAN DataStore (v2) - // - // General notes - // - // We need 2 things to make most requests: - // - // 1. CKAN API endpoint - // 2. ID of resource for which request is being made - // - // There are 2 ways to specify this information. - // - // EITHER (checked in order): - // - // * Every dataset must have an id equal to its resource id on the CKAN instance - // * The dataset has an endpoint attribute pointing to the CKAN API endpoint - // - // OR: - // - // Set the url attribute of the dataset to point to the Resource on the CKAN instance. The endpoint and id will then be automatically computed. - - my.__type__ = 'ckan'; - - // private - use either jQuery or Underscore Deferred depending on what is available - var Deferred = _.isUndefined(this.jQuery) ? _.Deferred : jQuery.Deferred; - - // Default CKAN API endpoint used for requests (you can change this but it will affect every request!) - // - // DEPRECATION: this will be removed in v0.7. Please set endpoint attribute on dataset instead - my.API_ENDPOINT = 'http://datahub.io/api'; - - // ### fetch - my.fetch = function(dataset) { - var wrapper; - if (dataset.endpoint) { - wrapper = my.DataStore(dataset.endpoint); - } else { - var out = my._parseCkanResourceUrl(dataset.url); - dataset.id = out.resource_id; - wrapper = my.DataStore(out.endpoint); - } - var dfd = new Deferred(); - var jqxhr = wrapper.search({resource_id: dataset.id, limit: 0}); - jqxhr.done(function(results) { - // map ckan types to our usual types ... - var fields = _.map(results.result.fields, function(field) { - field.type = field.type in CKAN_TYPES_MAP ? CKAN_TYPES_MAP[field.type] : field.type; - return field; - }); - var out = { - fields: fields, - useMemoryStore: false - }; - dfd.resolve(out); - }); - return dfd.promise(); - }; - - // only put in the module namespace so we can access for tests! - my._normalizeQuery = function(queryObj, dataset) { - var actualQuery = { - resource_id: dataset.id, - q: queryObj.q, - filters: {}, - limit: queryObj.size || 10, - offset: queryObj.from || 0 - }; - - if (queryObj.sort && queryObj.sort.length > 0) { - var _tmp = _.map(queryObj.sort, function(sortObj) { - return sortObj.field + ' ' + (sortObj.order || ''); - }); - actualQuery.sort = _tmp.join(','); - } - - if (queryObj.filters && queryObj.filters.length > 0) { - _.each(queryObj.filters, function(filter) { - if (filter.type === "term") { - actualQuery.filters[filter.field] = filter.term; - } - }); - } - return actualQuery; - }; - - my.query = function(queryObj, dataset) { - var wrapper; - if (dataset.endpoint) { - wrapper = my.DataStore(dataset.endpoint); - } else { - var out = my._parseCkanResourceUrl(dataset.url); - dataset.id = out.resource_id; - wrapper = my.DataStore(out.endpoint); - } - var actualQuery = my._normalizeQuery(queryObj, dataset); - var dfd = new Deferred(); - var jqxhr = wrapper.search(actualQuery); - jqxhr.done(function(results) { - var out = { - total: results.result.total, - hits: results.result.records - }; - dfd.resolve(out); - }); - return dfd.promise(); - }; - - // ### DataStore - // - // Simple wrapper around the CKAN DataStore API - // - // @param endpoint: CKAN api endpoint (e.g. http://datahub.io/api) - my.DataStore = function(endpoint) { - var that = {endpoint: endpoint || my.API_ENDPOINT}; - - that.search = function(data) { - var searchUrl = that.endpoint + '/3/action/datastore_search'; - var jqxhr = jQuery.ajax({ - url: searchUrl, - type: 'POST', - data: JSON.stringify(data) - }); - return jqxhr; - }; - - return that; - }; - - // Parse a normal CKAN resource URL and return API endpoint etc - // - // Normal URL is something like http://demo.ckan.org/dataset/some-dataset/resource/eb23e809-ccbb-4ad1-820a-19586fc4bebd - my._parseCkanResourceUrl = function(url) { - parts = url.split('/'); - var len = parts.length; - return { - resource_id: parts[len-1], - endpoint: parts.slice(0,[len-4]).join('/') + '/api' - }; - }; - - var CKAN_TYPES_MAP = { - 'int4': 'integer', - 'int8': 'integer', - 'float8': 'float' - }; - -}(this.recline.Backend.Ckan)); diff --git a/test/backend.ckan.test.js b/test/backend.ckan.test.js deleted file mode 100644 index e985e656..00000000 --- a/test/backend.ckan.test.js +++ /dev/null @@ -1,203 +0,0 @@ -(function ($) { -module("Backend CKAN"); - -test('_parseCkanResourceUrl', function() { - var resid = 'eb23e809-ccbb-4ad1-820a-19586fc4bebd'; - var url = 'http://demo.ckan.org/dataset/some-dataset/resource/' + resid; - var out = recline.Backend.Ckan._parseCkanResourceUrl(url); - var exp = { - resource_id: resid, - endpoint: 'http://demo.ckan.org/api' - } - deepEqual(out, exp); -}); - -test('_normalizeQuery', function() { - var dataset = new recline.Model.Dataset({ - url: 'does-not-matter', - id: 'xyz', - backend: 'ckan' - }); - - var queryObj = { - q: 'abc', - sort: [ - { field: 'location', order: 'desc' }, - { field: 'last' } - ] - }; - var out = recline.Backend.Ckan._normalizeQuery(queryObj, dataset); - var exp = { - resource_id: dataset.id, - q: 'abc', - filters: {}, - sort: 'location desc,last ', - limit: 10, - offset: 0 - }; - deepEqual(out, exp); -}); - -test("fetch", function() { - var dataset = new recline.Model.Dataset({ - url: 'http://localhost:5000/dataset/test-data-viewer/resource/4f1299ab-a100-4e5f-ba81-e6d234a2f3bd', - backend: 'ckan' - }); - - var stub = sinon.stub($, 'ajax', function(options) { - if (options.url.indexOf('datastore_search') != -1) { - return { - done: function(callback) { - callback(sample_data); - return this; - }, - fail: function() { - } - }; - } - }); - - dataset.fetch().done(function(dataset) { - deepEqual( - _.pluck(dataset.fields.toJSON(), 'id'), - _.pluck(sample_data.result.fields, 'id') - ); - // check we've mapped types correctly - equal(dataset.fields.get('x').get('type'), 'integer'); - equal(dataset.fields.get('country').get('type'), 'string'); - - // fetch does a query so we can check for records - equal(dataset.recordCount, 6); - equal(dataset.records.length, 6); - equal(dataset.records.at(0).get('id'), 0); - equal(dataset.records.at(0).get('country'), 'DE'); - }); - $.ajax.restore(); -}); - -var sample_data = { - "help": "", - "result": { - "fields": [ - { - "id": "_id", - "type": "int4" - }, - { - "id": "id", - "type": "int4" - }, - { - "id": "date", - "type": "date" - }, - { - "id": "x", - "type": "int4" - }, - { - "id": "y", - "type": "int4" - }, - { - "id": "z", - "type": "int4" - }, - { - "id": "country", - "type": "text" - }, - { - "id": "title", - "type": "text" - }, - { - "id": "lat", - "type": "float8" - }, - { - "id": "lon", - "type": "float8" - } - ], - "records": [ - { - "_id": 1, - "country": "DE", - "date": "2011-01-01", - "id": 0, - "lat": 52.56, - "lon": 13.4, - "title": "first", - "x": 1, - "y": 2, - "z": 3 - }, - { - "_id": 2, - "country": "UK", - "date": "2011-02-02", - "id": 1, - "lat": 54.97, - "lon": -1.6, - "title": "second", - "x": 2, - "y": 4, - "z": 24 - }, - { - "_id": 3, - "country": "US", - "date": "2011-03-03", - "id": 2, - "lat": 40.0, - "lon": -75.5, - "title": "third", - "x": 3, - "y": 6, - "z": 9 - }, - { - "_id": 4, - "country": "UK", - "date": "2011-04-04", - "id": 3, - "lat": 57.27, - "lon": -6.2, - "title": "fourth", - "x": 4, - "y": 8, - "z": 6 - }, - { - "_id": 5, - "country": "UK", - "date": "2011-05-04", - "id": 4, - "lat": 51.58, - "lon": 0.0, - "title": "fifth", - "x": 5, - "y": 10, - "z": 15 - }, - { - "_id": 6, - "country": "DE", - "date": "2011-06-02", - "id": 5, - "lat": 51.04, - "lon": 7.9, - "title": "sixth", - "x": 6, - "y": 12, - "z": 18 - } - ], - "resource_id": "4f1299ab-a100-4e5f-ba81-e6d234a2f3bd", - "total": 6 - }, - "success": true -}; - -})(this.jQuery); diff --git a/test/index.html b/test/index.html index a60fa4fa..d728917b 100644 --- a/test/index.html +++ b/test/index.html @@ -37,7 +37,6 @@ - @@ -45,7 +44,6 @@ -