Last active
August 29, 2015 14:08
-
-
Save un33k/108c3cf280b5fb397863 to your computer and use it in GitHub Desktop.
AngularJS to get weather from Yahoo
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
var tvapp = angular.module('tvapp'); | |
tvapp.service('weatherService', function($http, $q){ | |
var getQuery = function(location) { | |
var query = 'select * from weather.forecast where woeid in ' + | |
'(select woeid from geo.places(1) where text="city, state country")'; | |
query = query.replace('city', location.city) | |
.replace('state', location.state) | |
.replace('country', location.country); | |
console.log(query); | |
return query; | |
} | |
var getUrl = function(location){ | |
var baseUrl = 'https://query.yahooapis.com/v1/public/yql?q='; | |
var query = encodeURIComponent(getQuery(location)); | |
var finalUrl = baseUrl + query; | |
return finalUrl; | |
} | |
this.getWeather = function(location){ | |
var url = getUrl(location); | |
var params = { | |
format: 'json', | |
env: 'store%3A%2F%2Fdatatables.org%2Falltableswithkeys', | |
callback: 'JSON_CALLBACK' | |
} | |
var deferred = $q.defer(); | |
$http({ | |
method: 'JSONP', | |
url: url, | |
params: params | |
}).success(function(json) { | |
console.log(JSON.stringify(json)); | |
var result = json.query.results; | |
deferred.resolve(result); | |
}).error(function(error) { | |
console.log(JSON.stringify(error)); | |
deferred.reject(JSON.stringify(error)) | |
}); | |
return deferred.promise; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment