Blame platform-demos/pt_BR/weatherGeonames.js.page

Packit 1470ea
Packit 1470ea
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" type="topic" style="task" id="weatherGeonames.js" xml:lang="pt-BR">
Packit 1470ea
  <info>
Packit 1470ea
    <link type="guide" xref="weatherApp.js#main"/>
Packit 1470ea
    <revision version="0.1" date="2012-03-09" status="stub"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Susanna Huhtanen</name>
Packit 1470ea
      <email its:translate="no">ihmis.suski@gmail.com</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc/>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Rafael Ferreira</mal:name>
Packit 1470ea
      <mal:email>rafael.f.f1@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2013</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>Local library geoNames</title>
Packit 1470ea
  <synopsis>
Packit 1470ea
    

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 <link href="http://en.wikipedia.org/wiki/List_of_airports_by_ICAO_code:_E">ICAO codes </link> 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:

Packit 1470ea
Packit 1470ea
    <list>
Packit 1470ea
      <item>

<link xref="#geonamesimports">Local library for getting the weather</link>

</item>
Packit 1470ea
      <item>

<link xref="#geonamesfunction">Creating function geoNames</link>

</item>
Packit 1470ea
      <item>

<link xref="#geonamesmethods">Methods for geoNames</link>

</item>
Packit 1470ea
      <item>

<link xref="#geonames.js">geonames.js </link>

</item>
Packit 1470ea
    </list>
Packit 1470ea
  </synopsis>
Packit 1470ea
Packit 1470ea
  <section id="geonamesimports">
Packit 1470ea
  <title>Local library for getting the weather</title>
Packit 1470ea
  

For this we need a new file that will be our local library.

Packit 1470ea
  
Packit 1470ea
const Soup = imports.gi.Soup;
Packit 1470ea
const _httpSession = new Soup.SessionAsync();
Packit 1470ea
Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefault());
Packit 1470ea
]]>
Packit 1470ea
  

In the first lines we'll import and initialize the libraries we need to use in this local library. Soup handles all the requests we have to make with http.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="geonamesfunction">
Packit 1470ea
  <title>Creating function GeoNames</title>
Packit 1470ea
  
Packit 1470ea
function GeoNames(station) {
Packit 1470ea
  this.station = station;
Packit 1470ea
}
Packit 1470ea
Packit 1470ea
GeoNames.prototype = {
Packit 1470ea
Packit 1470ea
}
Packit 1470ea
]]>
Packit 1470ea
  

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{}

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="geonamesmethods">
Packit 1470ea
  <title>Methods for GeoNames</title>
Packit 1470ea
  
Packit 1470ea
getWeather: function(callback) {
Packit 1470ea
    var request = Soup.Message.new('GET', 'http://api.geonames.org/weatherIcaoJSON?ICAO=' + this.station + '&username=demo');
Packit 1470ea
    _httpSession.queue_message(request, function(_httpSession, message) {
Packit 1470ea
      if (message.status_code !== 200) {
Packit 1470ea
        callback(message.status_code, null);
Packit 1470ea
        return;
Packit 1470ea
      }
Packit 1470ea
      var weatherJSON = request.response_body.data;
Packit 1470ea
      var weather = JSON.parse(weatherJSON);
Packit 1470ea
      callback(null, weather);
Packit 1470ea
      });
Packit 1470ea
},
Packit 1470ea
Packit 1470ea
getIcon: function(weather){
Packit 1470ea
    switch (weather.weatherObservation.weatherCondition){
Packit 1470ea
    case "drizzle":
Packit 1470ea
    case "light showers rain":
Packit 1470ea
    case "light rain":
Packit 1470ea
      return "weather-showers-scattered.svg";
Packit 1470ea
    case "rain":
Packit 1470ea
      return "weather-showers.svg";
Packit 1470ea
    case "light snow":
Packit 1470ea
    case "snow grains":
Packit 1470ea
      return "weather-snow.svg";
Packit 1470ea
    }
Packit 1470ea
    switch (weather.weatherObservation.clouds){
Packit 1470ea
      case "few clouds":
Packit 1470ea
      case "scattered clouds":
Packit 1470ea
        return "weather-few-clouds.svg";
Packit 1470ea
      case "clear sky":
Packit 1470ea
        return "weather-clear.svg"
Packit 1470ea
      case "broken clouds":
Packit 1470ea
      case "overcast":
Packit 1470ea
        return "weather-overcast.svg";
Packit 1470ea
    }
Packit 1470ea
    return "weather-fog.svg";
Packit 1470ea
}
Packit 1470ea
]]>
Packit 1470ea
  

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.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
Packit 1470ea
  <section id="geonames.js">
Packit 1470ea
  <title>geonames.js</title>
Packit 1470ea
  

Here is the entire code for our local library. The main program file calls this asynchronously.

Packit 1470ea
  
Packit 1470ea
const Soup = imports.gi.Soup;
Packit 1470ea
const _httpSession = new Soup.SessionAsync();
Packit 1470ea
Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefault());
Packit 1470ea
Packit 1470ea
function GeoNames(station) {
Packit 1470ea
  this.station = station;
Packit 1470ea
}
Packit 1470ea
Packit 1470ea
GeoNames.prototype = {
Packit 1470ea
  getWeather: function(callback) {
Packit 1470ea
    var request = Soup.Message.new('GET', 'http://api.geonames.org/weatherIcaoJSON?ICAO=' + this.station + '&username=demo');
Packit 1470ea
    _httpSession.queue_message(request, function(_httpSession, message) {
Packit 1470ea
      if (message.status_code !== 200) {
Packit 1470ea
        callback(message.status_code, null);
Packit 1470ea
        return;
Packit 1470ea
      }
Packit 1470ea
      var weatherJSON = request.response_body.data;
Packit 1470ea
      var weather = JSON.parse(weatherJSON);
Packit 1470ea
      callback(null, weather);
Packit 1470ea
      });
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
  getIcon: function(weather){
Packit 1470ea
    switch (weather.weatherObservation.weatherCondition){
Packit 1470ea
    case "drizzle":
Packit 1470ea
    case "light showers rain":
Packit 1470ea
    case "light rain":
Packit 1470ea
      return "weather-showers-scattered.svg";
Packit 1470ea
    case "rain":
Packit 1470ea
      return "weather-showers.svg";
Packit 1470ea
    case "light snow":
Packit 1470ea
    case "snow grains":
Packit 1470ea
      return "weather-snow.svg";
Packit 1470ea
    }
Packit 1470ea
    switch (weather.weatherObservation.clouds){
Packit 1470ea
      case "few clouds":
Packit 1470ea
      case "scattered clouds":
Packit 1470ea
        return "weather-few-clouds.svg";
Packit 1470ea
      case "clear sky":
Packit 1470ea
        return "weather-clear.svg"
Packit 1470ea
      case "broken clouds":
Packit 1470ea
      case "overcast":
Packit 1470ea
        return "weather-overcast.svg";
Packit 1470ea
    }
Packit 1470ea
    return "weather-fog.svg";
Packit 1470ea
    }
Packit 1470ea
}
Packit 1470ea
}  ]]>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
</page>