Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit 7a4d916

Browse files
committed
Complete rewrite and code cleanup. Now <4.0 kB in size. Fixed http/https API call bug [#79]. Minimium jQuery required is now v1.2.0.
1 parent faa3aca commit 7a4d916

7 files changed

Lines changed: 84 additions & 200 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
For a more complete changelog you can always check out the [commit log](https://github.com/monkeecreate/jquery.simpleWeather/commits/master).
44

5+
## v3.0 - X X X
6+
7+
* Complete rewrite! Removed over 100 lines of code without losing functionality.
8+
* Now < 4.0 kB in size.
9+
* Fixed http/https issue on API call [#79](https://github.com/monkeecreate/jquery.simpleWeather/pull/79).
10+
* Hat tip to [@defvayne23](https://github.com/defvayne23) for a quick code review.
11+
512
## v2.7 - April 17 2014
613

714
* Added gulp and a build/release process.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# simpleWeather - jQuery Plugin [![GitHub version](https://badge.fury.io/gh/monkeecreate%2Fjquery.simpleWeather.png)](http://badge.fury.io/gh/monkeecreate%2Fjquery.simpleWeather)
22

3-
A simple jQuery plugin to display current weather data for any location and doesn't get in your way. Now supports HTML5 GeoLocation! Developed by [James Fleeting](http://twitter.com/fleetingftw). You can find demos and docs at [simpleweatherjs.com](http://simpleweatherjs.com).
3+
A simple jQuery plugin to display current weather data for any location and doesn't get in your way. Now supports HTML5 GeoLocation! Handcrafted with ♥ from Austin, Texas by [James Fleeting](http://twitter.com/fleetingftw). You can find demos and docs at [simpleweatherjs.com](http://simpleweatherjs.com).
44

55
## Get The Latest
66
**[Bower](http://bower.io/)**
@@ -15,7 +15,7 @@ component install monkeecreate/jquery.simpleWeather
1515

1616
**[CDNJS](http://cdnjs.com/libraries/jquery.simpleWeather/)**
1717
```html
18-
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.simpleWeather/2.6.0/jquery.simpleWeather.min.js"></script>
18+
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.simpleWeather/2.7.0/jquery.simpleWeather.min.js"></script>
1919
```
2020

2121
**Git**

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
],
1010
"license": "MIT",
1111
"dependencies": {
12-
"jquery": ">= 1.5.0"
12+
"jquery": ">= 1.2.0"
1313
},
1414
"ignore": [
1515
"**/.*",

gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ gulp.task('lint', function () {
2121
gulp.task('build', ['lint'], function () {
2222
return gulp.src(source)
2323
.pipe(rename(sourceMin))
24-
.pipe(uglify({preserveComments: 'all'}))
24+
.pipe(uglify({preserveComments: 'some'}))
2525
.pipe(size())
2626
.pipe(gulp.dest('./'));
2727
});

jquery.simpleWeather.js

Lines changed: 70 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
/*
2-
* simpleWeather
3-
* http://simpleweatherjs.com
4-
*
5-
* A simple jQuery plugin to display current weather data
6-
* for any location and doesn't get in your way.
7-
*
8-
* Developed by James Fleeting <@fleetingftw> <http://iwasasuperhero.com>
9-
* Another project from monkeeCreate <http://monkeecreate.com>
10-
*
11-
* Version 2.7.0 - Last updated: April 17 2014
12-
*/
1+
/*! simpleWeather v3.0.0 - http://simpleweatherjs.com */
132
(function($) {
143
"use strict";
4+
5+
function getAltTemp(unit, temp) {
6+
if(unit === 'f') {
7+
return Math.round((5.0/9.0)*(temp-32.0));
8+
} else {
9+
return Math.round((9.0/5.0)*temp+32.0);
10+
}
11+
}
12+
1513
$.extend({
1614
simpleWeather: function(options){
1715
options = $.extend({
@@ -23,197 +21,87 @@
2321
}, options);
2422

2523
var now = new Date();
26-
var weatherUrl = '//query.yahooapis.com/v1/public/yql?format=json&rnd='+now.getFullYear()+now.getMonth()+now.getDay()+now.getHours()+'&diagnostics=true&callback=?&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&q=';
24+
var weatherUrl = 'https://query.yahooapis.com/v1/public/yql?format=json&rnd='+now.getFullYear()+now.getMonth()+now.getDay()+now.getHours()+'&diagnostics=true&callback=?&q=';
2725
if(options.location !== '') {
2826
weatherUrl += 'select * from weather.forecast where woeid in (select woeid from geo.placefinder where text="'+options.location+'" and gflags="R") and u="'+options.unit+'"';
2927
} else if(options.woeid !== '') {
3028
weatherUrl += 'select * from weather.forecast where woeid='+options.woeid+' and u="'+options.unit+'"';
3129
} else {
32-
options.error("Could not retrieve weather due to an invalid location.");
30+
options.error({message: "Could not retrieve weather due to an invalid location."});
3331
return false;
3432
}
3533

3634
$.getJSON(
3735
encodeURI(weatherUrl),
3836
function(data) {
3937
if(data !== null && data.query !== null && data.query.results !== null && data.query.results.channel.description !== 'Yahoo! Weather Error') {
40-
$.each(data.query.results, function(i, result) {
41-
if (result.constructor.toString().indexOf("Array") !== -1) {
42-
result = result[0];
43-
}
44-
45-
var altTemps = [], heatIndex, images = [];
46-
var compass = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N'];
47-
var windDirection = compass[Math.round(result.wind.direction / 22.5)];
48-
49-
if(result.item.condition.temp < 80 && result.atmosphere.humidity < 40) {
50-
heatIndex = -42.379+2.04901523*result.item.condition.temp+10.14333127*result.atmosphere.humidity-0.22475541*result.item.condition.temp*result.atmosphere.humidity-6.83783*(Math.pow(10, -3))*(Math.pow(result.item.condition.temp, 2))-5.481717*(Math.pow(10, -2))*(Math.pow(result.atmosphere.humidity, 2))+1.22874*(Math.pow(10, -3))*(Math.pow(result.item.condition.temp, 2))*result.atmosphere.humidity+8.5282*(Math.pow(10, -4))*result.item.condition.temp*(Math.pow(result.atmosphere.humidity, 2))-1.99*(Math.pow(10, -6))*(Math.pow(result.item.condition.temp, 2))*(Math.pow(result.atmosphere.humidity,2));
51-
} else {
52-
heatIndex = result.item.condition.temp;
53-
}
38+
var result = data.query.results.channel,
39+
weather = {},
40+
forecast,
41+
compass = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N'],
42+
image404 = "https://s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png";
5443

55-
if(options.unit === "f") {
56-
altTemps.unit = "c";
57-
altTemps.temp = Math.round((5.0/9.0)*(result.item.condition.temp-32.0));
58-
altTemps.high = Math.round((5.0/9.0)*(result.item.forecast[0].high-32.0));
59-
altTemps.low = Math.round((5.0/9.0)*(result.item.forecast[0].low-32.0));
60-
altTemps.forecastOneHigh = Math.round((5.0/9.0)*(result.item.forecast[1].high-32.0));
61-
altTemps.forecastOneLow = Math.round((5.0/9.0)*(result.item.forecast[1].low-32.0));
62-
altTemps.forecastTwoHigh = Math.round((5.0/9.0)*(result.item.forecast[2].high-32.0));
63-
altTemps.forecastTwoLow = Math.round((5.0/9.0)*(result.item.forecast[2].low-32.0));
64-
altTemps.forecastThreeHigh = Math.round((5.0/9.0)*(result.item.forecast[3].high-32.0));
65-
altTemps.forecastThreeLow = Math.round((5.0/9.0)*(result.item.forecast[3].low-32.0));
66-
altTemps.forecastFourHigh = Math.round((5.0/9.0)*(result.item.forecast[4].high-32.0));
67-
altTemps.forecastFourLow = Math.round((5.0/9.0)*(result.item.forecast[4].low-32.0));
68-
} else {
69-
altTemps.unit = "f";
70-
altTemps.temp = Math.round((9.0/5.0)*result.item.condition.temp+32.0);
71-
altTemps.high = Math.round((9.0/5.0)*result.item.forecast[0].high+32.0);
72-
altTemps.low = Math.round((9.0/5.0)*result.item.forecast[0].low+32.0);
73-
altTemps.forecastOneHigh = Math.round((9.0/5.0)*(result.item.forecast[1].high+32.0));
74-
altTemps.forecastOneLow = Math.round((9.0/5.0)*(result.item.forecast[1].low+32.0));
75-
altTemps.forecastTwoHigh = Math.round((9.0/5.0)*(result.item.forecast[2].high+32.0));
76-
altTemps.forecastTwoLow = Math.round((9.0/5.0)*(result.item.forecast[2].low+32.0));
77-
altTemps.forecastThreeHigh = Math.round((9.0/5.0)*(result.item.forecast[3].high+32.0));
78-
altTemps.forecastThreeLow = Math.round((9.0/5.0)*(result.item.forecast[3].low+32.0));
79-
altTemps.forecastFourHigh = Math.round((9.0/5.0)*(result.item.forecast[4].high+32.0));
80-
altTemps.forecastFourLow = Math.round((9.0/5.0)*(result.item.forecast[4].low+32.0));
81-
}
44+
weather.title = result.item.title;
45+
weather.temp = result.item.condition.temp;
46+
weather.code = result.item.condition.code;
47+
weather.todayCode = result.item.forecast[0].code;
48+
weather.currently = result.item.condition.text;
49+
weather.high = result.item.forecast[0].high;
50+
weather.low = result.item.forecast[0].low;
51+
weather.text = result.item.forecast[0].text;
52+
weather.humidity = result.atmosphere.humidity;
53+
weather.pressure = result.atmosphere.pressure;
54+
weather.rising = result.atmosphere.rising;
55+
weather.visibility = result.atmosphere.visibility;
56+
weather.sunrise = result.astronomy.sunrise;
57+
weather.sunset = result.astronomy.sunset;
58+
weather.description = result.item.description;
59+
weather.city = result.location.city;
60+
weather.country = result.location.country;
61+
weather.region = result.location.region;
62+
weather.updated = result.item.pubDate;
63+
weather.link = result.item.link;
64+
weather.units = {temp: result.units.temperature, distance: result.units.distance, pressure: result.units.pressure, speed: result.units.speed};
65+
weather.wind = {chill: result.wind.chill, direction: compass[Math.round(result.wind.direction / 22.5)], speed: result.wind.speed};
8266

83-
if(result.item.condition.code == "3200") {
84-
images.thumbnail = "//s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png";
85-
images.image = "//s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png";
86-
} else {
87-
images.thumbnail = "//l.yimg.com/a/i/us/nws/weather/gr/"+result.item.condition.code+"ds.png";
88-
images.image = "//l.yimg.com/a/i/us/nws/weather/gr/"+result.item.condition.code+"d.png";
89-
}
67+
if(result.item.condition.temp < 80 && result.atmosphere.humidity < 40) {
68+
weather.heatindex = -42.379+2.04901523*result.item.condition.temp+10.14333127*result.atmosphere.humidity-0.22475541*result.item.condition.temp*result.atmosphere.humidity-6.83783*(Math.pow(10, -3))*(Math.pow(result.item.condition.temp, 2))-5.481717*(Math.pow(10, -2))*(Math.pow(result.atmosphere.humidity, 2))+1.22874*(Math.pow(10, -3))*(Math.pow(result.item.condition.temp, 2))*result.atmosphere.humidity+8.5282*(Math.pow(10, -4))*result.item.condition.temp*(Math.pow(result.atmosphere.humidity, 2))-1.99*(Math.pow(10, -6))*(Math.pow(result.item.condition.temp, 2))*(Math.pow(result.atmosphere.humidity,2));
69+
} else {
70+
weather.heatindex = result.item.condition.temp;
71+
}
9072

91-
if(result.item.forecast[1].code == "3200")
92-
images.forecastOne = "//s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png";
93-
else
94-
images.forecastOne = "//l.yimg.com/a/i/us/nws/weather/gr/"+result.item.forecast[1].code+"d.png";
73+
if(result.item.condition.code == "3200") {
74+
weather.thumbnail = image404;
75+
weather.image = image404;
76+
} else {
77+
weather.thumbnail = "http://l.yimg.com/a/i/us/nws/weather/gr/"+result.item.condition.code+"ds.png";
78+
weather.image = "http://l.yimg.com/a/i/us/nws/weather/gr/"+result.item.condition.code+"d.png";
79+
}
9580

96-
if(result.item.forecast[2].code == "3200")
97-
images.forecastTwo = "//s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png";
98-
else
99-
images.forecastTwo = "//l.yimg.com/a/i/us/nws/weather/gr/"+result.item.forecast[2].code+"d.png";
81+
weather.alt = {temp: getAltTemp(options.unit, result.item.condition.temp), high: getAltTemp(options.unit, result.item.forecast[0].high), low: getAltTemp(options.unit, result.item.forecast[0].low)};
82+
if(options.unit === 'f') {
83+
weather.alt.unit = 'c';
84+
} else {
85+
weather.alt.unit = 'f';
86+
}
10087

101-
if(result.item.forecast[3].code == "3200")
102-
images.forecastThree = "//s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png";
103-
else
104-
images.forecastThree = "//l.yimg.com/a/i/us/nws/weather/gr/"+result.item.forecast[3].code+"d.png";
88+
weather.forecast = [];
89+
for(var i=0;i<result.item.forecast.length;i++) {
90+
forecast = result.item.forecast[i];
91+
forecast.alt = {high: getAltTemp(options.unit, result.item.forecast[i].high), low: getAltTemp(options.unit, result.item.forecast[i].low)};
10592

106-
if(result.item.forecast[4].code == "3200")
107-
images.forecastFour = "//s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png";
108-
else
109-
images.forecastFour = "//l.yimg.com/a/i/us/nws/weather/gr/"+result.item.forecast[4].code+"d.png";
93+
if(result.item.forecast[i].code == "3200") {
94+
forecast.image = image404;
95+
} else {
96+
forecast.image = "http://l.yimg.com/a/i/us/nws/weather/gr/"+result.item.forecast[1].code+"d.png";
97+
}
11098

111-
var weather = {
112-
title: result.item.title,
113-
temp: result.item.condition.temp,
114-
tempAlt: altTemps.temp,
115-
code: result.item.condition.code,
116-
todayCode: result.item.forecast[0].code,
117-
units:{
118-
temp: result.units.temperature,
119-
distance: result.units.distance,
120-
pressure: result.units.pressure,
121-
speed: result.units.speed,
122-
tempAlt: altTemps.unit
123-
},
124-
currently: result.item.condition.text,
125-
high: result.item.forecast[0].high,
126-
highAlt: altTemps.high,
127-
low: result.item.forecast[0].low,
128-
lowAlt: altTemps.low,
129-
forecast: result.item.forecast[0].text,
130-
wind:{
131-
chill: result.wind.chill,
132-
direction: windDirection,
133-
speed: result.wind.speed
134-
},
135-
humidity: result.atmosphere.humidity,
136-
heatindex: heatIndex,
137-
pressure: result.atmosphere.pressure,
138-
rising: result.atmosphere.rising,
139-
visibility: result.atmosphere.visibility,
140-
sunrise: result.astronomy.sunrise,
141-
sunset: result.astronomy.sunset,
142-
description: result.item.description,
143-
thumbnail: images.thumbnail,
144-
image: images.image,
145-
tomorrow:{
146-
high: result.item.forecast[1].high,
147-
highAlt: altTemps.forecastOneHigh,
148-
low: result.item.forecast[1].low,
149-
lowAlt: altTemps.forecastOneLow,
150-
forecast: result.item.forecast[1].text,
151-
code: result.item.forecast[1].code,
152-
date: result.item.forecast[1].date,
153-
day: result.item.forecast[1].day,
154-
image: images.forecastOne
155-
},
156-
forecasts:{
157-
one:{
158-
high: result.item.forecast[1].high,
159-
highAlt: altTemps.forecastOneHigh,
160-
low: result.item.forecast[1].low,
161-
lowAlt: altTemps.forecastOneLow,
162-
forecast: result.item.forecast[1].text,
163-
code: result.item.forecast[1].code,
164-
date: result.item.forecast[1].date,
165-
day: result.item.forecast[1].day,
166-
image: images.forecastOne
167-
},
168-
two:{
169-
high: result.item.forecast[2].high,
170-
highAlt: altTemps.forecastTwoHigh,
171-
low: result.item.forecast[2].low,
172-
lowAlt: altTemps.forecastTwoLow,
173-
forecast: result.item.forecast[2].text,
174-
code: result.item.forecast[2].code,
175-
date: result.item.forecast[2].date,
176-
day: result.item.forecast[2].day,
177-
image: images.forecastTwo
178-
},
179-
three:{
180-
high: result.item.forecast[3].high,
181-
highAlt: altTemps.forecastThreeHigh,
182-
low: result.item.forecast[3].low,
183-
lowAlt: altTemps.forecastThreeLow,
184-
forecast: result.item.forecast[3].text,
185-
code: result.item.forecast[3].code,
186-
date: result.item.forecast[3].date,
187-
day: result.item.forecast[3].day,
188-
image: images.forecastThree
189-
},
190-
four:{
191-
high: result.item.forecast[4].high,
192-
highAlt: altTemps.forecastFourHigh,
193-
low: result.item.forecast[4].low,
194-
lowAlt: altTemps.forecastFourLow,
195-
forecast: result.item.forecast[4].text,
196-
code: result.item.forecast[4].code,
197-
date: result.item.forecast[4].date,
198-
day: result.item.forecast[4].day,
199-
image: images.forecastFour
200-
},
201-
},
202-
city: result.location.city,
203-
country: result.location.country,
204-
region: result.location.region,
205-
updated: result.item.pubDate,
206-
link: result.item.link
207-
};
99+
weather.forecast.push(forecast);
100+
}
208101

209-
options.success(weather);
210-
});
102+
options.success(weather);
211103
} else {
212-
if (data.query.results === null) {
213-
options.error("An invalid WOEID or location was provided.");
214-
} else {
215-
options.error("There was an error retrieving the latest weather information. Please try again.");
216-
}
104+
options.error({message: "There was an error retrieving the latest weather information. Please try again.", error: data.query.results.channel.item.title});
217105
}
218106
}
219107
);

0 commit comments

Comments
 (0)