weather_pinpoint_jp 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/weather_pinpoint_jp/version.rb +1 -1
- data/lib/weather_pinpoint_jp.rb +46 -31
- data/test/test_data.html +1 -0
- data/test/test_data.xml +1 -0
- data/test/test_data_result.xml +14 -0
- data/test/test_load.rb +15 -1
- data/weather_pinpoint_jp.gemspec +0 -2
- metadata +6 -18
data/lib/weather_pinpoint_jp.rb
CHANGED
@@ -3,8 +3,8 @@
|
|
3
3
|
require "weather_pinpoint_jp/version"
|
4
4
|
require 'net/http'
|
5
5
|
require 'uri'
|
6
|
-
require 'nokogiri'
|
7
6
|
require 'open-uri'
|
7
|
+
require 'rexml/document'
|
8
8
|
require 'pp'
|
9
9
|
|
10
10
|
module WeatherPinpointJp
|
@@ -13,42 +13,57 @@ module WeatherPinpointJp
|
|
13
13
|
STATION = 2
|
14
14
|
POSTAL_CODE = 3
|
15
15
|
|
16
|
+
# weather code
|
17
|
+
SUNNY = 100
|
18
|
+
CLOUDY = 200
|
19
|
+
RAINY = 300
|
20
|
+
SNOWY = 400
|
21
|
+
HEAT_WAVE = 500
|
22
|
+
HEAVY_RAIN = 850
|
23
|
+
|
16
24
|
class << self
|
17
25
|
def get(keyword, type = ADDRESS)
|
18
26
|
search_url = URI.parse("http://weathernews.jp/pinpoint/cgi/search_point.cgi")
|
19
|
-
html_url = nil
|
20
|
-
xml_url = nil
|
21
|
-
name = nil
|
22
27
|
|
23
28
|
# search location...
|
29
|
+
body = ""
|
24
30
|
Net::HTTP.start(search_url.host, search_url.port) do |http|
|
25
31
|
query = "<search><keyword>#{keyword}</keyword><category>#{type}</category></search>"
|
26
|
-
|
27
32
|
res = http.post(search_url.path, query, {})
|
28
|
-
|
29
|
-
doc = Nokogiri::XML(escaped_body)
|
30
|
-
|
31
|
-
result_num = doc.search('seac_cnt').children[0].content.to_i
|
32
|
-
if result_num == 0
|
33
|
-
raise "cannot find the location..."
|
34
|
-
end
|
35
|
-
|
36
|
-
html_url = doc.search('url').children[0].content
|
37
|
-
name = doc.search('name').children[0].content
|
33
|
+
body = res.body
|
38
34
|
end
|
39
35
|
|
40
|
-
# get html
|
41
|
-
|
36
|
+
# get location name & html url
|
37
|
+
(name, html_url) = get_name_and_url(body)
|
42
38
|
|
43
39
|
# find the location code...
|
44
|
-
|
45
|
-
location_code = onload_str.scan(/obsOrg=(.*?)&xmlFile/)[0][0]
|
40
|
+
location_code = get_code(html_url)
|
46
41
|
|
47
42
|
# return pinpoint forecast instance...
|
48
43
|
xml_url = "http://weathernews.jp/pinpoint/xml/#{location_code}.xml"
|
49
44
|
return load(name, xml_url)
|
50
45
|
end
|
51
46
|
|
47
|
+
def get_name_and_url(xml_str)
|
48
|
+
escaped_body = xml_str.gsub(/\&/, "&")
|
49
|
+
|
50
|
+
doc = REXML::Document.new(escaped_body)
|
51
|
+
result_num = doc.elements['/pin_result/seac_cnt'].text.to_i
|
52
|
+
if result_num == 0
|
53
|
+
raise "cannot find the location..."
|
54
|
+
end
|
55
|
+
|
56
|
+
html_url = doc.elements['/pin_result/data_ret/ret/url'].text
|
57
|
+
name = doc.elements['/pin_result/data_ret/ret/name'].text
|
58
|
+
|
59
|
+
[name, html_url]
|
60
|
+
end
|
61
|
+
|
62
|
+
def get_code(url)
|
63
|
+
html = open(url).read
|
64
|
+
location_code = html.scan(/obsOrg=(.*?)&xmlFile/)[0][0]
|
65
|
+
end
|
66
|
+
|
52
67
|
def load(name, url)
|
53
68
|
return Forecast.new(name, url)
|
54
69
|
end
|
@@ -60,33 +75,33 @@ module WeatherPinpointJp
|
|
60
75
|
@url = url
|
61
76
|
|
62
77
|
# load xml
|
63
|
-
doc =
|
64
|
-
day = doc.
|
78
|
+
doc = REXML::Document.new(open(url))
|
79
|
+
day = doc.elements['//weathernews/data/day']
|
65
80
|
|
66
81
|
# get starttime
|
67
|
-
year = day.
|
68
|
-
month = day.
|
69
|
-
date = day.
|
70
|
-
hour = day.
|
82
|
+
year = day.attributes['startYear'].to_i
|
83
|
+
month = day.attributes['startMonth'].to_i
|
84
|
+
date = day.attributes['startDate'].to_i
|
85
|
+
hour = day.attributes['startHour'].to_i
|
71
86
|
|
72
87
|
@start_time = Time.local(year, month, date, hour, 0, 0)
|
73
88
|
|
74
89
|
# weather
|
75
90
|
@weather = []
|
76
|
-
day.
|
77
|
-
@weather << e.
|
91
|
+
day.elements.each('weather/hour') do |e|
|
92
|
+
@weather << e.text.to_i
|
78
93
|
end
|
79
94
|
|
80
95
|
# temperature
|
81
96
|
@temperature = []
|
82
|
-
day.
|
83
|
-
@temperature << e.
|
97
|
+
day.elements.each('temperature/hour') do |e|
|
98
|
+
@temperature << e.text.to_i
|
84
99
|
end
|
85
100
|
|
86
101
|
# precipitation
|
87
102
|
@precipitation = []
|
88
|
-
day.
|
89
|
-
@precipitation << e.
|
103
|
+
day.elements.each('precipitation/hour') do |e|
|
104
|
+
@precipitation << e.text.to_i
|
90
105
|
end
|
91
106
|
end
|
92
107
|
|
data/test/test_data.html
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
obsOrg=12345&xmlFile
|
data/test/test_data.xml
CHANGED
@@ -0,0 +1,14 @@
|
|
1
|
+
<pin_result>
|
2
|
+
<ret_cnt>1</ret_cnt>
|
3
|
+
<seac_cnt>1</seac_cnt>
|
4
|
+
<data_ret>
|
5
|
+
<ret>
|
6
|
+
<name>地名1</name>
|
7
|
+
<url>http://example.com/result_url_1?text=1&test2=2&test3=3</url>
|
8
|
+
</ret>
|
9
|
+
<ret>
|
10
|
+
<name>地名2</name>
|
11
|
+
<url>http://example.com/result_url_2?text=1&test2=2&test3=3</url>
|
12
|
+
</ret>
|
13
|
+
</data_ret>
|
14
|
+
</pin_result>
|
data/test/test_load.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# -*- coding:utf-8 -*-
|
1
2
|
require 'rubygems'
|
2
3
|
require 'bundler/setup'
|
3
4
|
$:.unshift File.expand_path '../lib', File.dirname(__FILE__)
|
@@ -18,12 +19,25 @@ class TestLoad < Minitest::Test
|
|
18
19
|
assert_equal f.nil?, true
|
19
20
|
end
|
20
21
|
|
22
|
+
def test_get_name_and_url
|
23
|
+
result_xml = open(File.dirname(__FILE__) + "/test_data_result.xml").read
|
24
|
+
(name, url) = WeatherPinpointJp.get_name_and_url(result_xml)
|
25
|
+
assert_equal name, "地名1"
|
26
|
+
assert_equal url, "http://example.com/result_url_1?text=1&test2=2&test3=3"
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_get_code
|
30
|
+
url = File.dirname(__FILE__) + "/test_data.html"
|
31
|
+
code = WeatherPinpointJp.get_code(url)
|
32
|
+
assert_equal code, "12345"
|
33
|
+
end
|
34
|
+
|
21
35
|
def test_load
|
22
36
|
f = WeatherPinpointJp.load("test_data", File.dirname(__FILE__) + "/test_data.xml")
|
23
37
|
assert_equal f.nil?, false, "load() failed..."
|
24
38
|
|
25
39
|
# start time
|
26
|
-
assert_equal
|
40
|
+
assert_equal f.start_time, Time.new(2013, 11, 12, 13, 0, 0)
|
27
41
|
|
28
42
|
# weather
|
29
43
|
assert_equal f.weather.size, 36
|
data/weather_pinpoint_jp.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weather_pinpoint_jp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-11-
|
12
|
+
date: 2013-11-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -59,22 +59,6 @@ dependencies:
|
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
-
- !ruby/object:Gem::Dependency
|
63
|
-
name: nokogiri
|
64
|
-
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
|
-
requirements:
|
67
|
-
- - ~>
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
version: '1.6'
|
70
|
-
type: :runtime
|
71
|
-
prerelease: false
|
72
|
-
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
|
-
requirements:
|
75
|
-
- - ~>
|
76
|
-
- !ruby/object:Gem::Version
|
77
|
-
version: '1.6'
|
78
62
|
description: weather information library for ruby
|
79
63
|
email:
|
80
64
|
- yoggy0@gmail.com
|
@@ -92,7 +76,9 @@ files:
|
|
92
76
|
- samples/address.rb
|
93
77
|
- samples/postalcode.rb
|
94
78
|
- samples/station.rb
|
79
|
+
- test/test_data.html
|
95
80
|
- test/test_data.xml
|
81
|
+
- test/test_data_result.xml
|
96
82
|
- test/test_load.rb
|
97
83
|
- weather_pinpoint_jp.gemspec
|
98
84
|
homepage: https://github.com/yoggy/weather_pinpoint_jp
|
@@ -121,5 +107,7 @@ signing_key:
|
|
121
107
|
specification_version: 3
|
122
108
|
summary: weather information library for ruby
|
123
109
|
test_files:
|
110
|
+
- test/test_data.html
|
124
111
|
- test/test_data.xml
|
112
|
+
- test/test_data_result.xml
|
125
113
|
- test/test_load.rb
|