Skip to content

Instantly share code, notes, and snippets.

@japanetfutan
Created October 30, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save japanetfutan/eff2e5e914ccad856a0b to your computer and use it in GitHub Desktop.
Save japanetfutan/eff2e5e914ccad856a0b to your computer and use it in GitHub Desktop.
var UI = require('ui');
var ajax = require('ajax');
var myAPIKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
// 初期表示
var Card = new UI.Card({
title: "Please Wait",
body: "Downloading..."
});
Card.show();
// Construct URL
var cityName = 'Osaka';
var URL = 'http://api.openweathermap.org/data/2.5/weather?q=' + cityName + '&appid=' + myAPIKey;
// Make the request
ajax(
{
url: URL,
type: 'json'
},
function(data) {
// Success!
console.log('Successfully fetched weather data!');
// Extract data
var location = data.name;
var temperature = Math.round(data.main.temp - 273.15) + 'C';
// Always upper-case first letter of description
var description = data.weather[0].description;
description = description.charAt(0).toUpperCase() + description.substring(1);
// Show to user
Card.title('');
Card.subtitle(location + ', ' + temperature);
Card.body(description);
},
function(error) {
// Failure!
console.log('Failed fetching weather data: ' + error);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment