1+ /**
2+ @name : angular-openweathermap-api-factory
3+ @version : 0.5.0 (06-01-2016)
4+ @author : Jonathan Hornung
5+ @url : https://github.com/JohnnyTheTank/angular-openweathermap-api-factory#readme
6+ @license : MIT
7+ */
8+ "use strict" ;
9+
10+ angular . module ( "jtt_openweathermap" , [ ] )
11+ . factory ( 'openweathermapFactory' , [ '$http' , 'openweathermapSearchDataService' , function ( $http , openweathermapSearchDataService ) {
12+
13+ var openweathermapFactory = { } ;
14+
15+ openweathermapFactory . getWeatherFromCitySearchByName = function ( _params ) {
16+ var searchData = openweathermapSearchDataService . getNew ( "citySearchByName" , _params ) ;
17+ return $http ( {
18+ method : 'GET' ,
19+ url : searchData . url ,
20+ params : searchData . object ,
21+ } ) ;
22+ } ;
23+
24+ openweathermapFactory . getWeatherFromCityById = function ( _params ) {
25+ var searchData = openweathermapSearchDataService . getNew ( "cityById" , _params ) ;
26+ return $http ( {
27+ method : 'GET' ,
28+ url : searchData . url ,
29+ params : searchData . object ,
30+ } ) ;
31+ } ;
32+
33+ openweathermapFactory . getWeatherFromGroupOfCitiesById = function ( _params ) {
34+ var searchData = openweathermapSearchDataService . getNew ( "citiesById" , _params ) ;
35+ return $http ( {
36+ method : 'GET' ,
37+ url : searchData . url ,
38+ params : searchData . object ,
39+ } ) ;
40+ } ;
41+
42+ openweathermapFactory . getWeatherFromLocationByCoordinates = function ( _params ) {
43+ var searchData = openweathermapSearchDataService . getNew ( "locationByCoordinates" , _params ) ;
44+ return $http ( {
45+ method : 'GET' ,
46+ url : searchData . url ,
47+ params : searchData . object ,
48+ } ) ;
49+ } ;
50+
51+ openweathermapFactory . getWeatherFromLocationByZipcode = function ( _params ) {
52+ var searchData = openweathermapSearchDataService . getNew ( "locationByZipcode" , _params ) ;
53+ return $http ( {
54+ method : 'GET' ,
55+ url : searchData . url ,
56+ params : searchData . object ,
57+ } ) ;
58+ } ;
59+
60+ return openweathermapFactory ;
61+ } ] )
62+ . service ( 'openweathermapSearchDataService' , function ( ) {
63+ this . getApiBaseUrl = function ( _params ) {
64+ return "http://api.openweathermap.org/data/2.5/" ;
65+ } ;
66+
67+ this . fillDataInObjectByList = function ( _object , _params , _list ) {
68+ angular . forEach ( _list , function ( value , key ) {
69+ if ( typeof _params [ value ] !== "undefined" ) {
70+ _object . object [ value ] = _params [ value ] ;
71+ }
72+ } ) ;
73+ return _object ;
74+ } ;
75+
76+ this . getNew = function ( _type , _params ) {
77+
78+ var openweathermapSearchData = {
79+ object : {
80+ appid : _params . appid ,
81+ } ,
82+ url : "" ,
83+ } ;
84+
85+ switch ( _type ) {
86+ case "citySearchByName" :
87+ openweathermapSearchData = this . fillDataInObjectByList ( openweathermapSearchData , _params , [
88+ 'q' , 'lang' , 'type' , "units" ,
89+ ] ) ;
90+ openweathermapSearchData . url = this . getApiBaseUrl ( ) + "weather" ;
91+ break ;
92+
93+ case "cityById" :
94+ openweathermapSearchData = this . fillDataInObjectByList ( openweathermapSearchData , _params , [
95+ 'id' , 'lang' , "units" ,
96+ ] ) ;
97+ openweathermapSearchData . url = this . getApiBaseUrl ( ) + "weather" ;
98+ break ;
99+
100+ case "citiesById" :
101+ openweathermapSearchData = this . fillDataInObjectByList ( openweathermapSearchData , _params , [
102+ 'id' , 'lang' , "units" ,
103+ ] ) ;
104+ openweathermapSearchData . url = this . getApiBaseUrl ( ) + "group" ;
105+ break ;
106+
107+ case "locationByCoordinates" :
108+ openweathermapSearchData = this . fillDataInObjectByList ( openweathermapSearchData , _params , [
109+ 'lat' , 'lon' , 'lang' , "units" ,
110+ ] ) ;
111+ openweathermapSearchData . url = this . getApiBaseUrl ( ) + "weather" ;
112+ break ;
113+
114+ case "locationByZipcode" :
115+ openweathermapSearchData = this . fillDataInObjectByList ( openweathermapSearchData , _params , [
116+ 'zip' , 'lang' , "units" ,
117+ ] ) ;
118+ openweathermapSearchData . url = this . getApiBaseUrl ( ) + "weather" ;
119+ break ;
120+ }
121+ return openweathermapSearchData ;
122+ } ;
123+ } ) ;
0 commit comments