Skip to content

Commit 8d3d358

Browse files
committed
adding unit testing
1 parent 1158d28 commit 8d3d358

12 files changed

+262
-130
lines changed

app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
var darkskyForecast = angular.module('darkskyForecast', [
55
'ngRoute',
66
'Components',
7-
'service.forecastProvider',
8-
'service.coordProvider',
7+
'service.forecastService',
8+
'service.coordService',
99
'darkskyForecast.main',
1010
'darkskyForecast.forecast',
1111
])

app/forecast/forecast.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
}
88

99
.forecast-view p {
10-
color: #222222;
1110
text-align: center;
1211
font-weight: bold;
1312
display: inline-block;

app/main/locations.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"locations": [{
3+
"tooltip": "Timisoara",
4+
"label": "Timisoara",
5+
"icon": "timisoara",
6+
"id": "timisoara"
7+
}, {
8+
"tooltip": "Cluj-Napoca",
9+
"label": "Cluj-Napoca",
10+
"icon": "cluj-napoca",
11+
"id": "cluj"
12+
}, {
13+
"tooltip": "Iasi",
14+
"label": "Iasi",
15+
"icon": "iasi",
16+
"id": "iasi"
17+
}, {
18+
"tooltip": "Bucuresti",
19+
"label": "Bucuresti",
20+
"icon": "bucuresti",
21+
"id": "bucuresti"
22+
}, {
23+
"tooltip": "Constanta",
24+
"label": "Constanta",
25+
"icon": "constanta",
26+
"id": "constanta"
27+
}
28+
29+
]
30+
}

app/main/locationsService.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
angular.module('darkskyForecast.main')
4+
.service('locationsService', function ($q, $http) {
5+
this.getLocations = function () {
6+
var deferred = $q.defer();
7+
8+
$http.get('app/main/locations.json')
9+
.then(function (data) {
10+
deferred.resolve(data.data.locations);
11+
})
12+
.catch(function (data) {
13+
deferred.reject(data);
14+
});
15+
16+
return deferred.promise;
17+
}
18+
});

app/main/main.js

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,31 @@ angular.module('darkskyForecast.main', ['ngRoute'])
99
});
1010
}])
1111

12-
.controller('MainCtrl', ['$scope', '$location', 'coordProvider', 'forecastProvider',
13-
function Main($scope, $location, coordProvider, forecastProvider) {
14-
$scope.icon = "rain";
15-
$scope.locations = [
16-
{
17-
tooltip: 'Timisoara',
18-
label: 'Timisoara',
19-
icon: 'timisoara',
20-
id: 'timisoara'
21-
},
22-
{
23-
tooltip: 'Cluj-Napoca',
24-
label: 'Cluj-Napoca',
25-
icon: 'cluj-napoca',
26-
id: 'cluj'
27-
},
28-
{
29-
tooltip: 'Iasi',
30-
label: 'Iasi',
31-
icon: 'iasi',
32-
id: 'iasi'
33-
},
34-
{
35-
tooltip: 'Bucuresti',
36-
label: 'Bucuresti',
37-
icon: 'bucuresti',
38-
id: 'bucuresti'
39-
},
40-
{
41-
tooltip: 'Constanta',
42-
label: 'Constanta',
43-
icon: 'constanta',
44-
id: 'constanta'
45-
}
46-
47-
];
12+
.controller('MainCtrl', ['$scope', '$location', 'coordService', 'forecastService', 'locationsService',
13+
function Main($scope, $location, coordProvider, forecastProvider, locationsProvider) {
14+
$scope.locations = [];
15+
locationsProvider.getLocations()
16+
.then(function (locations) {
17+
$scope.locations = locations;
18+
})
4819

4920
$scope.displayWeather = function (index) {
5021
var locationDetails = $scope.locations[index];
5122
var location = locationDetails.id;
5223
var savedLoc = localStorage.getItem(location);
24+
5325
if (!savedLoc) { //non existent location in storage
5426
coordProvider.getCoords(location)
5527
.then(function (coords) {
5628
localStorage.setItem(location, JSON.stringify({
57-
label: locationDetails.label,
5829
coords: coords
5930
}));
6031
return forecastProvider.getWeather(coords);
6132
})
6233
.then(function (weather) {
6334
var loc = JSON.parse(localStorage.getItem(location));
6435
localStorage.setItem(location, JSON.stringify({
65-
label: loc.label,
36+
label: locationDetails.label,
6637
coords: loc.coords,
6738
timestamp: Date.now(),
6839
temperature: weather.temperature,
@@ -72,11 +43,9 @@ angular.module('darkskyForecast.main', ['ngRoute'])
7243
});
7344
} else {
7445
var loc = JSON.parse(savedLoc);
75-
if (loc.timestamp && Math.abs(Date.now() - loc.timestamp) / 60000 < 60) {
76-
//all data available
46+
if (loc.timestamp && Math.abs(Date.now() - loc.timestamp) / 60000 < 60) { //all data available
7747
$location.path('/forecast/' + location);
78-
} else {
79-
//refresh weather data
48+
} else { //refresh only weather data
8049
forecastProvider.getWeather(loc.coords)
8150
.then(function (weather) {
8251
localStorage.setItem(location, JSON.stringify({
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
'use strict';
2-
angular.module('service.coordProvider', [])
3-
.factory('coordProvider', function ($q, $http, GOOGLE_API) {
4-
return {
5-
getCoords: function (location) {
6-
var deferred = $q.defer();
72

8-
$http({
3+
angular.module('service.coordService', [])
4+
.service('coordService', function ($q, $http, GOOGLE_API) {
5+
this.getCoords = function (location) {
6+
var deferred = $q.defer();
7+
8+
$http({
99
method: 'GET',
1010
timeout: 300000,
1111
url: GOOGLE_API.URL,
1212
params: {
1313
'address': location,
1414
'key': GOOGLE_API.KEY
1515
}
16-
}).then(function successCallback(data) {
16+
})
17+
.then(function (data) {
1718
if (parseInt(data.status) === 200) {
1819
deferred.resolve(data.data.results[0].geometry.location);
1920
} else {
2021
deferred.reject(data.status);
2122
}
2223

23-
}, function errorCallback(data) {
24+
})
25+
.catch(function (data) {
2426
deferred.reject(data.error_message);
2527
});
2628

27-
//return a promise that needs to be resolved before data can be used
28-
return deferred.promise;
29-
}
29+
return deferred.promise;
3030
}
3131
});

app/services/forecastProvider.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

app/services/forecastService.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
angular.module('service.forecastService', [])
4+
.service('forecastService', function ($q, $http, $sce, DARKSKY_API) {
5+
6+
this.getWeather = function (coords) {
7+
var deferred = $q.defer();
8+
9+
var url = [DARKSKY_API.URL, DARKSKY_API.KEY, '/', coords.lat, ',', coords.lng, '?exclude=hourly,daily,flags'].join('');
10+
/*$http({
11+
method: 'GET',
12+
timeout: 300000,
13+
url: url
14+
})
15+
.then(function (data) {
16+
// check response code
17+
if (parseInt(data.status) === 200) {
18+
deferred.resolve(data.data.currently);
19+
} else {
20+
deferred.reject(data.status);
21+
}
22+
})
23+
.catch(function (data) {
24+
deferred.reject(data.message);
25+
});*/
26+
27+
setTimeout(function () {
28+
deferred.resolve({
29+
apparentTemperature: 41.42,
30+
cloudCover: 0.3,
31+
dewPoint: 34.35,
32+
humidity: 0.68,
33+
icon: "partly-cloudy-night",
34+
ozone: 390.56,
35+
precipIntensity: 0,
36+
precipProbability: 0,
37+
pressure: 1016.39,
38+
summary: "Partly Cloudy",
39+
temperature: 44.18,
40+
time: 1489252719,
41+
visibility: 6.21,
42+
windBearing: 325,
43+
windSpeed: 4.95
44+
});
45+
}, 700);
46+
47+
//return a promise that needs to be resolved before data can be used
48+
return deferred.promise;
49+
}
50+
});

index.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,26 @@
1919
<script src="node_modules/angular/angular.js"></script>
2020
<script src="node_modules/angular-route/angular-route.js"></script>
2121
<script src="node_modules/skycons/skycons.js"></script>
22+
</head>
23+
24+
<body>
25+
<div ng-view></div>
2226

2327
<!-- Components -->
2428
<script src="app/components/Components.js"></script>
2529
<script src="app/components/location/locationDirective.js"></script>
2630
<script src="app/components/skycon/skyconDirective.js"></script>
2731

2832
<!-- Services -->
29-
<script src="app/services/Services.js"></script>
30-
<script src="app/services/coordinatesProvider.js"></script>
31-
<script src="app/services/forecastProvider.js"></script>
33+
<script src="app/services/coordinatesService.js"></script>
34+
<script src="app/services/forecastService.js"></script>
3235

3336
<script src="app.js"></script>
3437
<script src="app/main/main.js"></script>
38+
<script src="app/main/locationsService.js"></script>
3539
<script src="app/forecast/forecast.js"></script>
40+
</body>
3641

3742

38-
</head>
39-
40-
<body>
41-
<div ng-view></div>
42-
</body>
4343

4444
</html>

0 commit comments

Comments
 (0)