aping-plugin-openweathermap
Version:
OpenWeatherMap plugin for apiNG
8 lines • 7.39 kB
JavaScript
/**
@name: aping-plugin-openweathermap
@version: 0.8.0 (26-09-2016)
@author: Jonathan Hornung <jonathan.hornung@gmail.com>
@url: https://github.com/JohnnyTheTank/apiNG-plugin-openweathermap
@license: MIT
*/
"use strict";angular.module("jtt_aping_openweathermap",["jtt_openweathermap"]).directive("apingOpenweathermap",["apingOpenWeatherMapHelper","apingUtilityHelper","openweathermapFactory",function(a,b,c){return{require:"?aping",restrict:"A",replace:"false",link:function(d,e,f,g){var h=g.getAppSettings(),i=b.parseJsonFromAttributes(f.apingOpenweathermap,a.getThisPlatformString(),h);i.forEach(function(d){var e={model:h.model};"undefined"!=typeof h.getNativeData?e.getNativeData=h.getNativeData:e.getNativeData=!1;var f={appid:b.getApiCredentials(a.getThisPlatformString(),"api_key"),lang:d.language||!1,units:d.units||"metric"};"kelvin"===d.units&&(f.units=void 0),d.cityName?(f.q=d.cityName,d.countryCode&&(f.q+=","+d.countryCode),d.type&&(f.type=d.type),"forecast5"===d.timeSlot?c.getForecast5FromCitySearchByName(f).then(function(b){g.concatToResults(a.getObjectByJsonData(b,e))}):c.getWeatherFromCitySearchByName(f).then(function(b){g.concatToResults(a.getObjectByJsonData(b,e))})):d.cityId?(f.id=d.cityId,"forecast5"===d.timeSlot?c.getForecast5FromCityById(f).then(function(b){g.concatToResults(a.getObjectByJsonData(b,e))}):c.getWeatherFromCityById(f).then(function(b){g.concatToResults(a.getObjectByJsonData(b,e))})):d.lat&&d.lng?(f.lat=d.lat,f.lon=d.lng,"forecast5"===d.timeSlot?c.getForecast5FromLocationByCoordinates(f).then(function(b){g.concatToResults(a.getObjectByJsonData(b,e))}):c.getWeatherFromLocationByCoordinates(f).then(function(b){g.concatToResults(a.getObjectByJsonData(b,e))})):d.zip&&(f.zip=d.zip,d.countryCode&&(f.zip+=","+d.countryCode),c.getWeatherFromLocationByZipcode(f).then(function(b){g.concatToResults(a.getObjectByJsonData(b,e))}))})}}}]),angular.module("jtt_aping_openweathermap").service("apingOpenWeatherMapHelper",["apingModels","apingTimeHelper","apingUtilityHelper",function(a,b,c){this.getThisPlatformString=function(){return"openweathermap"},this.getThisPlatformLink=function(){return"https://openweathermap.org/"},this.getObjectByJsonData=function(a,b){var c=[];if(a&&a.data){var d=this;if(a.data.constructor===Array)a.data.items&&angular.forEach(a.data.items,function(a,e){var f;f=b.getNativeData===!0||"true"===b.getNativeData?a:d.getItemByJsonData(a,b.model),f&&c.push(f)});else if("undefined"!=typeof a.data.list&&a.data.list.constructor===Array)angular.forEach(a.data.list,function(a,e){var f;f=b.getNativeData===!0||"true"===b.getNativeData?a:d.getItemByJsonData(a,b.model),f&&c.push(f)});else{var e;e=b.getNativeData===!0||"true"===b.getNativeData?a.data:d.getItemByJsonData(a.data,b.model),e&&c.push(e)}}return c},this.getItemByJsonData=function(a,b){var c={};if(a&&b)switch(b){case"weather":c=this.getWeatherItemByJsonData(a);break;default:return!1}return c},this.getRainInfoFromObject=function(a){var b={duration:void 0,volume:void 0};return"object"==typeof a&&angular.forEach(a,function(a,c){b.duration=c,b.volume=a}),b},this.getWeatherInfoFromArray=function(a){var b={code:void 0,caption:void 0,text:void 0,icon_name:void 0,icon_url:void 0};return a.constructor===Array&&(b.code=a[0].id||void 0,b.caption=a[0].main||void 0,b.text=a[0].description||void 0,b.icon_name=a[0].icon||void 0,b.icon_url=a[0].icon?"https://openweathermap.org/img/w/"+a[0].icon+".png":void 0),b},this.getWeatherItemByJsonData=function(b){var c=a.getNew("weather",this.getThisPlatformString());if(angular.extend(c,{temp:b.main?b.main.temp:void 0,pressure:b.main?b.main.pressure:void 0,humidity:b.main?b.main.humidity:void 0,temp_min:b.main?b.main.temp_min:void 0,temp_max:b.main?b.main.temp_max:void 0,sea_level:b.main?b.main.sea_level:void 0,grnd_level:b.main?b.main.grnd_level:void 0,wind_speed:b.wind?b.wind.speed:void 0,wind_deg:b.wind?b.wind.deg:void 0,clouds:b.clouds?b.clouds.all:void 0,sunrise_timestamp:b.sys?b.sys.sunrise:void 0,sunrise_date_time:b.sys?new Date(b.sys.sunrise):void 0,sunset_timestamp:b.sys?b.sys.sunset:void 0,sunset_date_time:b.sys?new Date(b.sys.sunset):void 0,loc_city:b.name||void 0,loc_city_id:b.id||void 0,loc_country:b.sys?b.sys.country:void 0,loc_lat:b.coord?b.coord.lat:void 0,loc_lng:b.coord?b.coord.lon:void 0}),b.dt?(c.timestamp=1e3*b.dt,c.date_time=new Date(c.timestamp)):(c.timestamp=Date.now(),c.date_time=new Date),b.rain){var d=this.getRainInfoFromObject(b.rain);c.rain_duration=d.duration||void 0,c.rain_volume=d.volume||void 0}if(b.weather){var e=this.getWeatherInfoFromArray(b.weather);c.weather_code=e.code||void 0,c.weather_caption=e.caption||void 0,c.weather_text=e.text||void 0,c.weather_icon_name=e.icon_name||void 0,c.weather_icon_url=e.icon_url||void 0}return c}}]),angular.module("jtt_openweathermap",[]).factory("openweathermapFactory",["$http","openweathermapSearchDataService",function(a,b){var c={};return c.getWeatherFromCitySearchByName=function(c){var d=b.getNew("citySearchByName",c);return a({method:"GET",url:d.url,params:d.object})},c.getWeatherFromCityById=function(c){var d=b.getNew("cityById",c);return a({method:"GET",url:d.url,params:d.object})},c.getWeatherFromGroupOfCitiesById=function(c){var d=b.getNew("citiesById",c);return a({method:"GET",url:d.url,params:d.object})},c.getWeatherFromLocationByCoordinates=function(c){var d=b.getNew("locationByCoordinates",c);return a({method:"GET",url:d.url,params:d.object})},c.getWeatherFromLocationByZipcode=function(c){var d=b.getNew("locationByZipcode",c);return a({method:"GET",url:d.url,params:d.object})},c.getForecast5FromCitySearchByName=function(c){var d=b.getNew("cityForecast5SearchByName",c);return a({method:"GET",url:d.url,params:d.object})},c.getForecast5FromCityById=function(c){var d=b.getNew("cityForecast5ById",c);return a({method:"GET",url:d.url,params:d.object})},c.getForecast5FromLocationByCoordinates=function(c){var d=b.getNew("locationForecast5ByCoordinates",c);return a({method:"GET",url:d.url,params:d.object})},c}]).service("openweathermapSearchDataService",function(){this.getApiBaseUrl=function(a){return"http://api.openweathermap.org/data/2.5/"},this.fillDataInObjectByList=function(a,b,c){return angular.forEach(c,function(c,d){angular.isDefined(b[c])&&(a.object[c]=b[c])}),a},this.getNew=function(a,b){var c={object:{appid:b.appid},url:""};switch(a){case"citySearchByName":c=this.fillDataInObjectByList(c,b,["q","lang","type","units"]),c.url=this.getApiBaseUrl()+"weather";break;case"cityById":c=this.fillDataInObjectByList(c,b,["id","lang","units"]),c.url=this.getApiBaseUrl()+"weather";break;case"citiesById":c=this.fillDataInObjectByList(c,b,["id","lang","units"]),c.url=this.getApiBaseUrl()+"group";break;case"locationByCoordinates":c=this.fillDataInObjectByList(c,b,["lat","lon","lang","units"]),c.url=this.getApiBaseUrl()+"weather";break;case"locationByZipcode":c=this.fillDataInObjectByList(c,b,["zip","lang","units"]),c.url=this.getApiBaseUrl()+"weather";break;case"cityForecast5SearchByName":c=this.fillDataInObjectByList(c,b,["q","lang","type","units"]),c.url=this.getApiBaseUrl()+"forecast";break;case"cityForecast5ById":c=this.fillDataInObjectByList(c,b,["id","lang","units"]),c.url=this.getApiBaseUrl()+"forecast";break;case"locationForecast5ByCoordinates":c=this.fillDataInObjectByList(c,b,["lat","lon","lang","units"]),c.url=this.getApiBaseUrl()+"forecast"}return c}});