Created
October 30, 2015 14:12
-
-
Save japanetfutan/eff2e5e914ccad856a0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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