Module: WeatherPinpointJp

Defined in:
lib/weather_pinpoint_jp.rb,
lib/weather_pinpoint_jp/version.rb

Defined Under Namespace

Classes: Forecast

Constant Summary collapse

ADDRESS =

search type

1
STATION =
2
POSTAL_CODE =
3
SUNNY =

weather code

100
CLOUDY =
200
RAINY =
300
SNOWY =
400
HEAT_WAVE =
550
HEAVY_RAIN =
850
VERSION =
"0.0.5"

Class Method Summary collapse

Class Method Details

.get(keyword, type = ADDRESS) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/weather_pinpoint_jp.rb', line 25

def get(keyword, type = ADDRESS)
  search_url = URI.parse("http://weathernews.jp/pinpoint/cgi/search_point.cgi")

  # search location...
  body = ""
  Net::HTTP.start(search_url.host, search_url.port) do |http|
    query = "<search><keyword>#{keyword}</keyword><category>#{type}</category></search>"
    res = http.post(search_url.path, query, {})
    body = res.body
  end

  # get location name & html url
  (name, html_url) = get_name_and_url(body)

  # find the location code...
  location_code = get_code(html_url)

  # return pinpoint forecast instance...
  xml_url = "http://weathernews.jp/pinpoint/xml/#{location_code}.xml"
  return load(name, xml_url)
end

.get_code(url) ⇒ Object



62
63
64
65
# File 'lib/weather_pinpoint_jp.rb', line 62

def get_code(url)
  html = open(url).read
  location_code = html.scan(/obsOrg=(.*?)&xmlFile/)[0][0]
end

.get_name_and_url(xml_str) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/weather_pinpoint_jp.rb', line 47

def get_name_and_url(xml_str)
  escaped_body = xml_str.gsub(/\&/, "&amp;")

  doc = REXML::Document.new(escaped_body)
  result_num = doc.elements['/pin_result/seac_cnt'].text.to_i
  if result_num == 0
    raise "cannot find the location..."
  end

  html_url = doc.elements['/pin_result/data_ret/ret/url'].text
  name     = doc.elements['/pin_result/data_ret/ret/name'].text

  [name, html_url]
end

.load(name, url) ⇒ Object



67
68
69
# File 'lib/weather_pinpoint_jp.rb', line 67

def load(name, url)
  return Forecast.new(name, url)
end