Skip to content

Instantly share code, notes, and snippets.

@cantlin
Created November 7, 2014 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cantlin/84e9bceac11d24c1637f to your computer and use it in GitHub Desktop.
Save cantlin/84e9bceac11d24c1637f to your computer and use it in GitHub Desktop.
define(['angular', 'lodash'], function(angular, _) {
'use strict';
var mod = angular.module('guardianCardServices', []);
// Matches on any Guardian content URL
// Higher priority services (e.g. video embed) should be tried first
var guCardUriPattern = /https?:\/\/www\.theguardian\.com\/.*\/\d{4}\/.*/;
// var guCardEmbedApiHost = "http://embed.theguardian.com/oembed/"
var guCardEmbedApiHost = "http://cantl.in:8080/oembed/";
mod.factory('embedGuardianCard',
['$http', '$q',
function($http, $q) {
function processResponse(response, originalUri) {
if (response.error || response.errors) {
throw 'error-response';
}
return {
elementType: 'interactive',
fields: {
originalUrl: originalUri,
source: 'Guardian',
scriptUrl: response.script_url,
scriptName: 'gu-card',
iframeUrl: response.iframe_url,
html: response.stub
},
assets: []
};
}
return function(originalUri) {
// Fail early if doesn't look like a supported URL
if (! guCardUriPattern.test(originalUri)) {
return $q.reject('unsupported-uri');
}
var path = originalUri.replace(/https?:\/\/www\.theguardian\.com\//, '');
var embedApiUri = guCardEmbedApiHost + path;
return $http.get(embedApiUri)
.then(function(response) {
return processResponse(response.data, originalUri);
});
};
}]);
return mod;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment