Skip to content

Commit

Permalink
Complete rewrite and code cleanup. Now <4.0 kB in size. Fixed http/ht…
Browse files Browse the repository at this point in the history
…tps API call bug [monkeecreate#79]. Minimium jQuery required is now v1.2.0.
  • Loading branch information
fleeting committed Apr 28, 2014
1 parent faa3aca commit 7a4d916
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 200 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

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

## v3.0 - X X X

* Complete rewrite! Removed over 100 lines of code without losing functionality.
* Now < 4.0 kB in size.
* Fixed http/https issue on API call [#79](https://github.com/monkeecreate/jquery.simpleWeather/pull/79).
* Hat tip to [@defvayne23](https://github.com/defvayne23) for a quick code review.

## v2.7 - April 17 2014

* Added gulp and a build/release process.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# simpleWeather - jQuery Plugin [![GitHub version](https://badge.fury.io/gh/monkeecreate%2Fjquery.simpleWeather.png)](http://badge.fury.io/gh/monkeecreate%2Fjquery.simpleWeather)

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).
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).

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

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

**Git**
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"license": "MIT",
"dependencies": {
"jquery": ">= 1.5.0"
"jquery": ">= 1.2.0"
},
"ignore": [
"**/.*",
Expand Down
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ gulp.task('lint', function () {
gulp.task('build', ['lint'], function () {
return gulp.src(source)
.pipe(rename(sourceMin))
.pipe(uglify({preserveComments: 'all'}))
.pipe(uglify({preserveComments: 'some'}))
.pipe(size())
.pipe(gulp.dest('./'));
});
Expand Down
252 changes: 70 additions & 182 deletions jquery.simpleWeather.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/*
* simpleWeather
* http://simpleweatherjs.com
*
* A simple jQuery plugin to display current weather data
* for any location and doesn't get in your way.
*
* Developed by James Fleeting <@fleetingftw> <http://iwasasuperhero.com>
* Another project from monkeeCreate <http://monkeecreate.com>
*
* Version 2.7.0 - Last updated: April 17 2014
*/
/*! simpleWeather v3.0.0 - http://simpleweatherjs.com */
(function($) {
"use strict";

function getAltTemp(unit, temp) {
if(unit === 'f') {
return Math.round((5.0/9.0)*(temp-32.0));
} else {
return Math.round((9.0/5.0)*temp+32.0);
}
}

$.extend({
simpleWeather: function(options){
options = $.extend({
Expand All @@ -23,197 +21,87 @@
}, options);

var now = new Date();
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=';
var weatherUrl = 'https://query.yahooapis.com/v1/public/yql?format=json&rnd='+now.getFullYear()+now.getMonth()+now.getDay()+now.getHours()+'&diagnostics=true&callback=?&q=';
if(options.location !== '') {
weatherUrl += 'select * from weather.forecast where woeid in (select woeid from geo.placefinder where text="'+options.location+'" and gflags="R") and u="'+options.unit+'"';
} else if(options.woeid !== '') {
weatherUrl += 'select * from weather.forecast where woeid='+options.woeid+' and u="'+options.unit+'"';
} else {
options.error("Could not retrieve weather due to an invalid location.");
options.error({message: "Could not retrieve weather due to an invalid location."});
return false;
}

$.getJSON(
encodeURI(weatherUrl),
function(data) {
if(data !== null && data.query !== null && data.query.results !== null && data.query.results.channel.description !== 'Yahoo! Weather Error') {
$.each(data.query.results, function(i, result) {
if (result.constructor.toString().indexOf("Array") !== -1) {
result = result[0];
}

var altTemps = [], heatIndex, images = [];
var compass = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N'];
var windDirection = compass[Math.round(result.wind.direction / 22.5)];

if(result.item.condition.temp < 80 && result.atmosphere.humidity < 40) {
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));
} else {
heatIndex = result.item.condition.temp;
}
var result = data.query.results.channel,
weather = {},
forecast,
compass = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N'],
image404 = "https://s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png";

if(options.unit === "f") {
altTemps.unit = "c";
altTemps.temp = Math.round((5.0/9.0)*(result.item.condition.temp-32.0));
altTemps.high = Math.round((5.0/9.0)*(result.item.forecast[0].high-32.0));
altTemps.low = Math.round((5.0/9.0)*(result.item.forecast[0].low-32.0));
altTemps.forecastOneHigh = Math.round((5.0/9.0)*(result.item.forecast[1].high-32.0));
altTemps.forecastOneLow = Math.round((5.0/9.0)*(result.item.forecast[1].low-32.0));
altTemps.forecastTwoHigh = Math.round((5.0/9.0)*(result.item.forecast[2].high-32.0));
altTemps.forecastTwoLow = Math.round((5.0/9.0)*(result.item.forecast[2].low-32.0));
altTemps.forecastThreeHigh = Math.round((5.0/9.0)*(result.item.forecast[3].high-32.0));
altTemps.forecastThreeLow = Math.round((5.0/9.0)*(result.item.forecast[3].low-32.0));
altTemps.forecastFourHigh = Math.round((5.0/9.0)*(result.item.forecast[4].high-32.0));
altTemps.forecastFourLow = Math.round((5.0/9.0)*(result.item.forecast[4].low-32.0));
} else {
altTemps.unit = "f";
altTemps.temp = Math.round((9.0/5.0)*result.item.condition.temp+32.0);
altTemps.high = Math.round((9.0/5.0)*result.item.forecast[0].high+32.0);
altTemps.low = Math.round((9.0/5.0)*result.item.forecast[0].low+32.0);
altTemps.forecastOneHigh = Math.round((9.0/5.0)*(result.item.forecast[1].high+32.0));
altTemps.forecastOneLow = Math.round((9.0/5.0)*(result.item.forecast[1].low+32.0));
altTemps.forecastTwoHigh = Math.round((9.0/5.0)*(result.item.forecast[2].high+32.0));
altTemps.forecastTwoLow = Math.round((9.0/5.0)*(result.item.forecast[2].low+32.0));
altTemps.forecastThreeHigh = Math.round((9.0/5.0)*(result.item.forecast[3].high+32.0));
altTemps.forecastThreeLow = Math.round((9.0/5.0)*(result.item.forecast[3].low+32.0));
altTemps.forecastFourHigh = Math.round((9.0/5.0)*(result.item.forecast[4].high+32.0));
altTemps.forecastFourLow = Math.round((9.0/5.0)*(result.item.forecast[4].low+32.0));
}
weather.title = result.item.title;
weather.temp = result.item.condition.temp;
weather.code = result.item.condition.code;
weather.todayCode = result.item.forecast[0].code;
weather.currently = result.item.condition.text;
weather.high = result.item.forecast[0].high;
weather.low = result.item.forecast[0].low;
weather.text = result.item.forecast[0].text;
weather.humidity = result.atmosphere.humidity;
weather.pressure = result.atmosphere.pressure;
weather.rising = result.atmosphere.rising;
weather.visibility = result.atmosphere.visibility;
weather.sunrise = result.astronomy.sunrise;
weather.sunset = result.astronomy.sunset;
weather.description = result.item.description;
weather.city = result.location.city;
weather.country = result.location.country;
weather.region = result.location.region;
weather.updated = result.item.pubDate;
weather.link = result.item.link;
weather.units = {temp: result.units.temperature, distance: result.units.distance, pressure: result.units.pressure, speed: result.units.speed};
weather.wind = {chill: result.wind.chill, direction: compass[Math.round(result.wind.direction / 22.5)], speed: result.wind.speed};

if(result.item.condition.code == "3200") {
images.thumbnail = "//s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png";
images.image = "//s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png";
} else {
images.thumbnail = "//l.yimg.com/a/i/us/nws/weather/gr/"+result.item.condition.code+"ds.png";
images.image = "//l.yimg.com/a/i/us/nws/weather/gr/"+result.item.condition.code+"d.png";
}
if(result.item.condition.temp < 80 && result.atmosphere.humidity < 40) {
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));
} else {
weather.heatindex = result.item.condition.temp;
}

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

if(result.item.forecast[2].code == "3200")
images.forecastTwo = "//s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png";
else
images.forecastTwo = "//l.yimg.com/a/i/us/nws/weather/gr/"+result.item.forecast[2].code+"d.png";
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)};
if(options.unit === 'f') {
weather.alt.unit = 'c';
} else {
weather.alt.unit = 'f';
}

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

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

var weather = {
title: result.item.title,
temp: result.item.condition.temp,
tempAlt: altTemps.temp,
code: result.item.condition.code,
todayCode: result.item.forecast[0].code,
units:{
temp: result.units.temperature,
distance: result.units.distance,
pressure: result.units.pressure,
speed: result.units.speed,
tempAlt: altTemps.unit
},
currently: result.item.condition.text,
high: result.item.forecast[0].high,
highAlt: altTemps.high,
low: result.item.forecast[0].low,
lowAlt: altTemps.low,
forecast: result.item.forecast[0].text,
wind:{
chill: result.wind.chill,
direction: windDirection,
speed: result.wind.speed
},
humidity: result.atmosphere.humidity,
heatindex: heatIndex,
pressure: result.atmosphere.pressure,
rising: result.atmosphere.rising,
visibility: result.atmosphere.visibility,
sunrise: result.astronomy.sunrise,
sunset: result.astronomy.sunset,
description: result.item.description,
thumbnail: images.thumbnail,
image: images.image,
tomorrow:{
high: result.item.forecast[1].high,
highAlt: altTemps.forecastOneHigh,
low: result.item.forecast[1].low,
lowAlt: altTemps.forecastOneLow,
forecast: result.item.forecast[1].text,
code: result.item.forecast[1].code,
date: result.item.forecast[1].date,
day: result.item.forecast[1].day,
image: images.forecastOne
},
forecasts:{
one:{
high: result.item.forecast[1].high,
highAlt: altTemps.forecastOneHigh,
low: result.item.forecast[1].low,
lowAlt: altTemps.forecastOneLow,
forecast: result.item.forecast[1].text,
code: result.item.forecast[1].code,
date: result.item.forecast[1].date,
day: result.item.forecast[1].day,
image: images.forecastOne
},
two:{
high: result.item.forecast[2].high,
highAlt: altTemps.forecastTwoHigh,
low: result.item.forecast[2].low,
lowAlt: altTemps.forecastTwoLow,
forecast: result.item.forecast[2].text,
code: result.item.forecast[2].code,
date: result.item.forecast[2].date,
day: result.item.forecast[2].day,
image: images.forecastTwo
},
three:{
high: result.item.forecast[3].high,
highAlt: altTemps.forecastThreeHigh,
low: result.item.forecast[3].low,
lowAlt: altTemps.forecastThreeLow,
forecast: result.item.forecast[3].text,
code: result.item.forecast[3].code,
date: result.item.forecast[3].date,
day: result.item.forecast[3].day,
image: images.forecastThree
},
four:{
high: result.item.forecast[4].high,
highAlt: altTemps.forecastFourHigh,
low: result.item.forecast[4].low,
lowAlt: altTemps.forecastFourLow,
forecast: result.item.forecast[4].text,
code: result.item.forecast[4].code,
date: result.item.forecast[4].date,
day: result.item.forecast[4].day,
image: images.forecastFour
},
},
city: result.location.city,
country: result.location.country,
region: result.location.region,
updated: result.item.pubDate,
link: result.item.link
};
weather.forecast.push(forecast);
}

options.success(weather);
});
options.success(weather);
} else {
if (data.query.results === null) {
options.error("An invalid WOEID or location was provided.");
} else {
options.error("There was an error retrieving the latest weather information. Please try again.");
}
options.error({message: "There was an error retrieving the latest weather information. Please try again.", error: data.query.results.channel.item.title});
}
}
);
Expand Down
Loading

0 comments on commit 7a4d916

Please sign in to comment.