From 88dfa0b314acfe05e5e9a34e76330fc6b0903e59 Mon Sep 17 00:00:00 2001 From: Rufus Pollock Date: Sun, 14 Oct 2012 17:52:29 +0100 Subject: [PATCH] [#250,backend/ckan][s]: nicer way to do configuration of CKAN API endpoint including parsing of url - fixes #250. --- src/backend.ckan.js | 47 +++++++++++++++++++++++++++++++++++---- test/backend.ckan.test.js | 11 +++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/backend.ckan.js b/src/backend.ckan.js index edea9b06..89a65a2c 100644 --- a/src/backend.ckan.js +++ b/src/backend.ckan.js @@ -9,17 +9,38 @@ this.recline.Backend.Ckan = this.recline.Backend.Ckan || {}; // // 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 - // * You should set the CKAN API endpoint for requests by setting API_ENDPOINT value on this module (recline.Backend.Ckan.API_ENDPOINT) + // * 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) { - var wrapper = my.DataStore(); + 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) { @@ -55,8 +76,14 @@ this.recline.Backend.Ckan = this.recline.Backend.Ckan || {}; } 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 wrapper = my.DataStore(); var dfd = $.Deferred(); var jqxhr = wrapper.search(actualQuery); jqxhr.done(function(results) { @@ -89,7 +116,19 @@ this.recline.Backend.Ckan = this.recline.Backend.Ckan || {}; } 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', diff --git a/test/backend.ckan.test.js b/test/backend.ckan.test.js index 157c383e..e0598933 100644 --- a/test/backend.ckan.test.js +++ b/test/backend.ckan.test.js @@ -1,6 +1,17 @@ (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',