Created
November 7, 2014 14:56
-
-
Save cantlin/84e9bceac11d24c1637f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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