Created
July 23, 2013 12:50
-
-
Save ocolot/6062104 to your computer and use it in GitHub Desktop.
Url Shortener (AngularJS service using Google UrlShortener API)
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
angular.module('AppServices') | |
.factory 'UrlShortener', ($q, $rootScope) -> | |
gapiKey = 'APP_KEY' | |
gapi_deferred = $q.defer() | |
gapi_loaded = gapi_deferred.promise | |
gapi.client.setApiKey(gapiKey) | |
gapi.client.load 'urlshortener', 'v1', -> | |
$rootScope.$apply -> | |
gapi_deferred.resolve('loaded') | |
{ | |
expand: (short_url, callback) -> | |
gapi_loaded.then -> | |
request = gapi.client.urlshortener.url.get | |
'shortUrl': short_url | |
request.execute (response) -> | |
callback(response.longUrl) | |
shorten: (url) -> | |
short_url_deffered = $q.defer() | |
gapi_loaded.then -> | |
request = gapi.client.urlshortener.url.insert | |
'resource': | |
'longUrl': url | |
request.execute (response) -> | |
$rootScope.$apply -> | |
short_url_deffered.resolve(response.id) | |
short_url_deffered.promise | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment