Skip to content

Instantly share code, notes, and snippets.

@ocolot
Created July 23, 2013 12:50
Show Gist options
  • Save ocolot/6062104 to your computer and use it in GitHub Desktop.
Save ocolot/6062104 to your computer and use it in GitHub Desktop.
Url Shortener (AngularJS service using Google UrlShortener API)
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