Class: Weather

Inherits:
DynamicContent
  • Object
show all
Defined in:
app/models/weather.rb

Constant Summary collapse

DISPLAY_NAME =
'Weather'
UNITS =
{
  'metric' => 'Celsius',
  'imperial' => 'Fahrenheit'
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.form_attributesObject

Weather needs a location. Also allow specification of units



52
53
54
55
# File 'app/models/weather.rb', line 52

def self.form_attributes
  attributes = super()
  attributes.concat([:config => [:location, :units]])
end

Instance Method Details

#build_contentObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/weather.rb', line 9

def build_content
  require 'json'
  require 'net/http'

  # Build request url 
  params = {
    q: self.config['location'],
    units: self.config['units'],
    cnt: 1,
    mode: 'json',
    appid: ConcertoConfig['open_weather_map_api_key']
  }

  url = "http://api.openweathermap.org/data/2.5/forecast/daily?#{params.to_query}"

  # Make request to OpenWeatherMapAPI
  response = Net::HTTP.get_response(URI.parse(url)).body
  data = JSON.parse(response)
  
  # Build HTML using API data
  #<img src='http://api.openweathermap.org/img/w/#{data['list'][0]['weather'][0]['icon']}' />
  rawhtml = "
              <h1> Today in #{data['city']['name']} </h1>
              <div style='float: left; width: 50%'>
                <i class='owf owf-#{data['list'][0]['weather'][0]['id']} owf-5x'></i>
              </div>
              <div style='float: left; width: 50%'>
                <p> High </p>
                <h1> #{data['list'][0]['temp']['max']} &deg;#{UNITS[params[:units]][0]}</h1>
                <p> Low </p>
                <h1> #{data['list'][0]['temp']['min']} &deg;#{UNITS[params[:units]][0]}</h1>
              </div>
            "

  # Create HtmlText content
  htmltext = HtmlText.new()
  htmltext.name = "Today's weather in #{data['city']['name']}"
  htmltext.data = rawhtml

  return [htmltext]
end