Class: Thundersnow::Weather

Inherits:
Object
  • Object
show all
Defined in:
lib/thundersnow/weather.rb

Instance Method Summary collapse

Constructor Details

#initialize(location) ⇒ Weather

Returns a new instance of Weather.



6
7
8
# File 'lib/thundersnow/weather.rb', line 6

def initialize(location)
  @xml = get_weather_xml(location)
end

Instance Method Details

#cityObject



42
43
44
# File 'lib/thundersnow/weather.rb', line 42

def city
  @city ||= read_attr(@xml, '//forecast_information/city')
end

#conditionObject



53
54
55
# File 'lib/thundersnow/weather.rb', line 53

def condition
  read_attr(current_conditions, 'condition')
end

#currentObject



20
21
22
23
24
25
26
27
# File 'lib/thundersnow/weather.rb', line 20

def current
  values = ["Current Conditions for #{city}"]
  values << condition
  values << temperatures
  values << wind
  values << humidity
  values.join("\n")
end

#current_conditionsObject



77
78
79
# File 'lib/thundersnow/weather.rb', line 77

def current_conditions
  @current_conditions ||= @xml.xpath('//current_conditions')
end

#day_of_week(day) ⇒ Object



46
47
48
49
50
51
# File 'lib/thundersnow/weather.rb', line 46

def day_of_week(day)
  day_of_week = read_attr(day, 'day_of_week')
  return "Today" if day_of_week == today
  return "Tomorrow" if day_of_week == tomorrow
  day_of_week
end

#forecastObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/thundersnow/weather.rb', line 29

def forecast
  values = ["Weather Forecast for #{city}"]

  @xml.xpath('//forecast_conditions').each do |day|
    values << "Forecast for #{day_of_week(day)}"
    values << "High: #{read_attr(day, 'high')+deg_symbol}F / Low: #{read_attr(day, 'low')+deg_symbol}F"
    values << "Conditions: #{read_attr(day, 'condition')}"
    values << "-------------------------"
  end

  values.join("\n")
end

#get_weather_xml(location) ⇒ Object



10
11
12
13
# File 'lib/thundersnow/weather.rb', line 10

def get_weather_xml(location)
  uri = URI.encode "http://www.google.com/ig/api?weather=#{location}"
  Nokogiri::XML(open(uri))
end

#humidityObject



65
66
67
# File 'lib/thundersnow/weather.rb', line 65

def humidity
  read_attr(current_conditions, 'humidity')
end

#temp_cObject



73
74
75
# File 'lib/thundersnow/weather.rb', line 73

def temp_c
  read_attr(current_conditions, 'temp_c')
end

#temp_fObject



69
70
71
# File 'lib/thundersnow/weather.rb', line 69

def temp_f
  read_attr(current_conditions, 'temp_f')
end

#temperaturesObject



57
58
59
# File 'lib/thundersnow/weather.rb', line 57

def temperatures
  "#{temp_f} #{deg_symbol}F / #{temp_c} #{deg_symbol}C"
end

#valid?Boolean

Invalid locations contain problem_cause xml tag

Returns:

  • (Boolean)


16
17
18
# File 'lib/thundersnow/weather.rb', line 16

def valid?
  @xml.xpath('//weather/problem_cause').size == 0
end

#windObject



61
62
63
# File 'lib/thundersnow/weather.rb', line 61

def wind
  read_attr(current_conditions, 'wind_condition')
end