Susanna Huhtanen
ihmis.suski@gmail.com 2012
Sebastian Rasmussen
sebras@gmail.com 2019
Anders Jonsson
anders.jonsson@norsjovallen.se 2021

# Det lokala biblioteket geoNames

        In this part of the guide we'll construct the local library geoNames using asynchronous calls. Weather information in this example is fetched from geonames.org and the application is using the ICAO codes  to place your weather request. To write and run all the code examples yourself, you need an editor to write code in, Terminal and GNOME 3 or higher installed into your computer. In this guide we'll go through the following parts:

          Lokalt bibliotek för att erhålla vädret
          Skapa funktionen geoNames
          Metoder för geoNames
          geonames.js

# Lokalt bibliotek för att erhålla vädret

För detta behöver vi en ny fil som kommer vara vårt lokala bibliotek.

const Soup = imports.gi.Soup; const _httpSession = new Soup.SessionAsync(); Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefault());

I de första raderna kommer vi importera och initiera de bibliotek vi behöver använda i detta lokala bibliotek. Soup hanterar alla begäranden som vi behöver göra med http.

# Skapa funktionen GeoNames

function GeoNames(station) { this.station = station; }

GeoNames.prototype = {

}

Here we create the function GeoNames that will handle getting weather for us. JavaScript allows us to create functions that have little inside at first and later expand them. This will be done inside the GeoNames.prototype curly braces{}

# Metoder för geoNames

getWeather: function(callback) { var request = Soup.Message.new('GET', 'http://api.geonames.org/weatherIcaoJSON?ICAO=' + this.station + '&username=demo'); _httpSession.queue_message(request, function(_httpSession, message) { if (message.status_code !== 200) { callback(message.status_code, null); return; } var weatherJSON = request.response_body.data; var weather = JSON.parse(weatherJSON); callback(null, weather); }); },

getIcon: function(weather){ switch (weather.weatherObservation.weatherCondition){ case "drizzle": case "light showers rain": case "light rain": return "weather-showers-scattered.svg"; case "rain": return "weather-showers.svg"; case "light snow": case "snow grains": return "weather-snow.svg"; } switch (weather.weatherObservation.clouds){ case "few clouds": case "scattered clouds": return "weather-few-clouds.svg"; case "clear sky": return "weather-clear.svg" case "broken clouds": case "overcast": return "weather-overcast.svg"; } return "weather-fog.svg"; }

The first method for GeoNames is getWeather and the second getIcon. In getWeather we make a http request with soup, handle errors and then parse the information from the request to form we can use it. In getIcon we simply compare the results we got from getWeather to the switch we have in order to get the icon matching current weather. Now that we have our local library ready, it's time to make use of it.

# geonames.js

Här är hela koden för vårt lokala bibliotek. Huvudprogramfilen anropar detta asynkront.

const Soup = imports.gi.Soup; const _httpSession = new Soup.SessionAsync(); Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefault());

function GeoNames(station) { this.station = station; }

GeoNames.prototype = { getWeather: function(callback) { var request = Soup.Message.new('GET', 'http://api.geonames.org/weatherIcaoJSON?ICAO=' + this.station + '&username=demo'); _httpSession.queue_message(request, function(_httpSession, message) { if (message.status_code !== 200) { callback(message.status_code, null); return; } var weatherJSON = request.response_body.data; var weather = JSON.parse(weatherJSON); callback(null, weather); }); },

getIcon: function(weather){ switch (weather.weatherObservation.weatherCondition){ case "drizzle": case "light showers rain": case "light rain": return "weather-showers-scattered.svg"; case "rain": return "weather-showers.svg"; case "light snow": case "snow grains": return "weather-snow.svg"; } switch (weather.weatherObservation.clouds){ case "few clouds": case "scattered clouds": return "weather-few-clouds.svg"; case "clear sky": return "weather-clear.svg" case "broken clouds": case "overcast": return "weather-overcast.svg"; } return "weather-fog.svg"; } } }