Skip to content

Commit

Permalink
move to new yahoo api,convert endpoint to use json, fix specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni authored and Yoni committed May 22, 2016
1 parent 4c5fc0d commit 8474f31
Show file tree
Hide file tree
Showing 13 changed files with 406 additions and 198 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
yahoo_weatherman (2.0.0)
yahoo_weatherman (2.0.3)
nokogiri

GEM
Expand Down
18 changes: 9 additions & 9 deletions lib/yahoo_weatherman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Weatherman

VERSION = '2.0.2'

URI = 'http://xml.weather.yahoo.com/forecastrss'
URI = 'https://query.yahooapis.com/v1/public/yql?q='

# = Client
#
Expand Down Expand Up @@ -44,26 +44,26 @@ def initialize(options = {})
# Looks up weather by woeid.
#
def lookup_by_woeid(woeid)
raw = get request_url(woeid)
raw = get woeid_query_url(woeid)
Response.new(raw, options[:lang])
end

#
# Looks up weather by location.
#
def lookup_by_location(location)
lookup = WoeidLookup.new
woeid = lookup.get_woeid(location)
lookup_by_woeid(woeid)
raw = get location_query_url(location)
Response.new(raw, options[:lang])
end

private
def request_url(woeid)
@uri + query_string(woeid)

def woeid_query_url(woeid)
"#{URI}select%20*%20from%20weather.forecast%20where%20woeid%20%3D%20#{woeid}%20and%20u%20%3D%20'#{degrees_units}'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
end

def query_string(woeid)
"?w=#{woeid}&u=#{degrees_units}"
def location_query_url(location)
"#{URI}select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22#{::CGI.escape(location)}%22)%20and%20u%20%3D%20'#{degrees_units}'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
end

def degrees_units
Expand Down
2 changes: 1 addition & 1 deletion lib/yahoo_weatherman/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(doc)
end

def [](attr)
@image_root.xpath(attr).first.content
@image_root[attr]
end
end
end
53 changes: 26 additions & 27 deletions lib/yahoo_weatherman/response.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# coding: utf-8

require 'json'

module Weatherman

# = Response
Expand All @@ -11,7 +14,7 @@ class Response
attr_accessor :document_root

def initialize(raw, language = nil)
@document_root = Nokogiri::XML(raw).xpath('rss/channel')
@document_root = JSON.parse(raw).dig('query', 'results', 'channel')
@i18n = Weatherman::I18N.new(language)
end

Expand All @@ -25,20 +28,20 @@ def initialize(raw, language = nil)
# condition['date'] => #<Date: -1/2,0,2299161>
#
def condition
condition = item_attribute('yweather:condition')
condition = item_attribute('condition')
translate! do_convertions(condition, [:code, :to_i], [:temp, :to_i], [:date, :to_date], :text)
end

#
#
# Wind's details:
#
# wind = response.wind
# wind['chill'] => 21
# wind['direction'] => 340
# wind['chill'] => 21
# wind['direction'] => 340
# wind['chill'] => 9.66
#
def wind
do_convertions(attribute('yweather:wind'), [:chill, :to_i], [:direction, :to_i], [:speed, :to_f])
do_convertions(attribute('wind'), [:chill, :to_i], [:direction, :to_i], [:speed, :to_f])
end

#
Expand All @@ -53,7 +56,7 @@ def wind
#
def forecasts
convertions = [[:date, :to_date], [:low, :to_i], [:high, :to_i], [:code, :to_i], :day, :text]
item_attribute('yweather:forecast').collect do |forecast|
item_attribute('forecast').collect do |forecast|
translate! do_convertions(forecast, *convertions)
end
end
Expand All @@ -67,7 +70,7 @@ def forecasts
# location['city'] => Belo Horizonte
#
def location
translate! attribute('yweather:location')
translate! attribute('location')
end

# Units:
Expand All @@ -79,7 +82,7 @@ def location
# units['speed'] => "km/h"
#
def units
attribute('yweather:units')
attribute('units')
end

#
Expand All @@ -90,22 +93,22 @@ def units
# astronomy['sunset'] => "7:20 pm"
#
def astronomy
attribute('yweather:astronomy')
attribute('astronomy')
end

#
# Atmosphere :
#
# atmosphere = response.atmosphere
# atmosphere = response.atmosphere
# atmosphere['humidity'] => "62"
# atmosphere['visibility'] => "9.99"
# atmosphere['pressure'] => "982.05"
# atmosphere['rising'] => "0"
#
def atmosphere
atm = attribute('yweather:atmosphere')
atm = attribute('atmosphere')
do_convertions(atm, [:humidity, :to_f], [:visibility, :to_f], [:pressure, :to_f], [:rising, :to_f])
end
end

#
# Latitude:
Expand All @@ -115,7 +118,7 @@ def latitude
geo_attribute('lat')
end

#
#
# Longitude;
#
# response.longitude => -45.32
Expand All @@ -139,7 +142,7 @@ def image
end

#
# Description image. You might gonna need this if you have to customize the
# Description image. You might gonna need this if you have to customize the
# forecast summary.
#
def description_image
Expand All @@ -148,9 +151,10 @@ def description_image

#
# A short HTML snippet (raw text) with a simple weather description.
#
#
def description
text_attribute('description')
doc = Nokogiri::XML.fragment(item_attribute('description'))
doc.content
end
alias :summary :description

Expand All @@ -159,25 +163,21 @@ def description
# if you have to walk through its nodes.
#
def parsed_description
@parsed_description ||= Nokogiri::HTML(description)
@parsed_description ||= Nokogiri::HTML(description)
end

private
def attribute(attr, root = @document_root)
elements = root.xpath(attr)
elements = root[attr]
elements.size == 1 ? elements.first : elements
end

def item_attribute(attr)
attribute(attr, document_root.xpath('item').first)
attribute(attr, @document_root['item'])
end

def geo_attribute(attr)
item_attribute('geo:' + attr).children.first.text.to_f
end

def text_attribute(attr)
item_attribute(attr).content
item_attribute(attr).to_f
end

def do_convertions(attributes, *pairs)
Expand All @@ -194,8 +194,7 @@ def convert(value, method)
end

def translate!(attributes)
@i18n.translate! attributes
@i18n.translate! attributes
end
end
end

149 changes: 149 additions & 0 deletions spec/files/belo_horizonte_c.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
{
"query": {
"count": 1,
"created": "2016-05-22T13:22:34Z",
"lang": "en-US",
"results": {
"channel": {
"units": {
"distance": "km",
"pressure": "mb",
"speed": "km/h",
"temperature": "C"
},
"title": "Yahoo! Weather - Belo Horizonte, MG, BR",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-455821/",
"description": "Yahoo! Weather for Belo Horizonte, MG, BR",
"language": "en-us",
"lastBuildDate": "Sun, 22 May 2016 10:22 AM BRT",
"ttl": "60",
"location": {
"city": "Belo Horizonte",
"country": "Brazil",
"region": " MG"
},
"wind": {
"chill": "72",
"direction": "68",
"speed": "11.27"
},
"atmosphere": {
"humidity": "83",
"pressure": "31290.24",
"rising": "0",
"visibility": "25.91"
},
"astronomy": {
"sunrise": "6:20 am",
"sunset": "5:25 pm"
},
"image": {
"title": "Yahoo! Weather",
"width": "142",
"height": "18",
"link": "http://weather.yahoo.com",
"url": "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"
},
"item": {
"title": "Conditions for Belo Horizonte, MG, BR at 09:00 AM BRT",
"lat": "-19.899309",
"long": "-43.964352",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-455821/",
"pubDate": "Sun, 22 May 2016 09:00 AM BRT",
"condition": {
"code": "32",
"date": "Sun, 22 May 2016 09:00 AM BRT",
"temp": "22",
"text": "Sunny"
},
"forecast": [
{
"code": "32",
"date": "22 May 2016",
"day": "Sun",
"high": "26",
"low": "16",
"text": "Sunny"
},
{
"code": "47",
"date": "23 May 2016",
"day": "Mon",
"high": "26",
"low": "16",
"text": "Scattered Thunderstorms"
},
{
"code": "28",
"date": "24 May 2016",
"day": "Tue",
"high": "21",
"low": "16",
"text": "Mostly Cloudy"
},
{
"code": "30",
"date": "25 May 2016",
"day": "Wed",
"high": "21",
"low": "14",
"text": "Partly Cloudy"
},
{
"code": "30",
"date": "26 May 2016",
"day": "Thu",
"high": "24",
"low": "13",
"text": "Partly Cloudy"
},
{
"code": "30",
"date": "27 May 2016",
"day": "Fri",
"high": "25",
"low": "15",
"text": "Partly Cloudy"
},
{
"code": "26",
"date": "28 May 2016",
"day": "Sat",
"high": "23",
"low": "16",
"text": "Cloudy"
},
{
"code": "47",
"date": "29 May 2016",
"day": "Sun",
"high": "23",
"low": "16",
"text": "Scattered Thunderstorms"
},
{
"code": "34",
"date": "30 May 2016",
"day": "Mon",
"high": "21",
"low": "15",
"text": "Mostly Sunny"
},
{
"code": "32",
"date": "31 May 2016",
"day": "Tue",
"high": "26",
"low": "13",
"text": "Sunny"
}
],
"description": "<![CDATA[<img src=\"http://l.yimg.com/a/i/us/we/52/32.gif\"/>\n<BR />\n<b>Current Conditions:</b>\n<BR />Sunny\n<BR />\n<BR />\n<b>Forecast:</b>\n<BR /> Sun - Sunny. High: 26Low: 16\n<BR /> Mon - Scattered Thunderstorms. High: 26Low: 16\n<BR /> Tue - Mostly Cloudy. High: 21Low: 16\n<BR /> Wed - Partly Cloudy. High: 21Low: 14\n<BR /> Thu - Partly Cloudy. High: 24Low: 13\n<BR />\n<BR />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-455821/\">Full Forecast at Yahoo! Weather</a>\n<BR />\n<BR />\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)\n<BR />\n]]>",
"guid": {
"isPermaLink": "false"
}
}
}
}
}
}
Loading

0 comments on commit 8474f31

Please sign in to comment.