weather_pinpoint_jp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in weather_pinpoint_jp.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 yoggy
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # WeatherPinpointJp
2
+
3
+ weather information library for ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'weather_pinpoint_jp'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install weather_pinpoint_jp
18
+
19
+ ## Usage
20
+
21
+ <pre>
22
+ require 'weather_pinpoint_jp'
23
+
24
+ forecast = WeatherPinpointJp.get("東京都千代田区")
25
+
26
+ puts forecast.location
27
+
28
+ t = forecast.start_time
29
+ forecast.weather.each {|w|
30
+ puts "#{t.strftime("%Y/%m/%d %H:%M")} #{w}"
31
+ t += 3600
32
+ }
33
+ </pre>
34
+
35
+ <pre>
36
+ require 'weather_pinpoint_jp'
37
+
38
+ forecast = WeatherPinpointJp.get("100000", WeatherPinpointJp::POSTAL_CODE)
39
+
40
+ puts forecast.location
41
+ t = forecast.start_time
42
+ forecast.weather.each {|w|
43
+ puts "#{t.strftime("%Y/%m/%d %H:%M")} #{w}"
44
+ t += 3600
45
+ }
46
+ </pre>
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "test/test_*.rb"
6
+ end
7
+
8
+ task :default => :test
@@ -0,0 +1,96 @@
1
+ # -*- coding:utf-8 -*-
2
+
3
+ require "weather_pinpoint_jp/version"
4
+ require 'net/http'
5
+ require 'uri'
6
+ require 'nokogiri'
7
+ require 'open-uri'
8
+ require 'pp'
9
+
10
+ module WeatherPinpointJp
11
+ # search type
12
+ ADDRESS = 1
13
+ STATION = 2
14
+ POSTAL_CODE = 3
15
+
16
+ class << self
17
+ def get(keyword, type = ADDRESS)
18
+ search_url = URI.parse("http://weathernews.jp/pinpoint/cgi/search_point.cgi")
19
+ html_url = nil
20
+ xml_url = nil
21
+ name = nil
22
+
23
+ # search location...
24
+ Net::HTTP.start(search_url.host, search_url.port) do |http|
25
+ query = "<search><keyword>#{keyword}</keyword><category>#{type}</category></search>"
26
+
27
+ res = http.post(search_url.path, query, {})
28
+ escaped_body = res.body.gsub(/\&/, "&amp;")
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
38
+ end
39
+
40
+ # get html...
41
+ doc = Nokogiri::HTML(open(html_url))
42
+
43
+ # find the location code...
44
+ onload_str = doc.search('body').first.attribute('onload').value
45
+ location_code = onload_str.scan(/obsOrg=(.*?)&xmlFile/)[0][0]
46
+
47
+ # return pinpoint forecast instance...
48
+ xml_url = "http://weathernews.jp/pinpoint/xml/#{location_code}.xml"
49
+ return load(name, xml_url)
50
+ end
51
+
52
+ def load(name, url)
53
+ return Forecast.new(name, url)
54
+ end
55
+ end
56
+
57
+ class Forecast
58
+ def initialize(location, url)
59
+ @location = location
60
+ @url = url
61
+
62
+ # load xml
63
+ doc = Nokogiri::XML(open(url))
64
+ day = doc.xpath('//weathernews/data/day')[0]
65
+
66
+ # get starttime
67
+ year = day.attribute('startYear').content.to_i
68
+ month = day.attribute('startMonth').content.to_i
69
+ date = day.attribute('startDate').content.to_i
70
+ hour = day.attribute('startHour').content.to_i
71
+
72
+ @start_time = Time.local(year, month, date, hour, 0, 0)
73
+
74
+ # weather
75
+ @weather = []
76
+ day.xpath('weather/hour').children.each do |e|
77
+ @weather << e.content.to_i
78
+ end
79
+
80
+ # temperature
81
+ @temperature = []
82
+ day.xpath('temperature/hour').children.each do |e|
83
+ @temperature << e.content.to_i
84
+ end
85
+
86
+ # precipitation
87
+ @precipitation = []
88
+ day.xpath('precipitation/hour').children.each do |e|
89
+ @precipitation << e.content.to_i
90
+ end
91
+ end
92
+
93
+ attr_reader :location, :start_time, :weather, :temperature, :precipitation
94
+ end
95
+ end
96
+
@@ -0,0 +1,3 @@
1
+ module WeatherPinpointJp
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ # -*- coding:utf-8 -*-
2
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
3
+
4
+ require 'weather_pinpoint_jp'
5
+
6
+ forecast = WeatherPinpointJp.get("東京都千代田区")
7
+
8
+ puts forecast.location
9
+ t = forecast.start_time
10
+ forecast.weather.each {|w|
11
+ puts "#{t.strftime("%Y/%m/%d %H:%M")} #{w}"
12
+ t += 3600
13
+ }
@@ -0,0 +1,13 @@
1
+ # -*- coding:utf-8 -*-
2
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
3
+
4
+ require 'weather_pinpoint_jp'
5
+
6
+ forecast = WeatherPinpointJp.get("100000", WeatherPinpointJp::POSTAL_CODE)
7
+
8
+ puts forecast.location
9
+ t = forecast.start_time
10
+ forecast.weather.each {|w|
11
+ puts "#{t.strftime("%Y/%m/%d %H:%M")} #{w}"
12
+ t += 3600
13
+ }
@@ -0,0 +1,13 @@
1
+ # -*- coding:utf-8 -*-
2
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
3
+
4
+ require 'weather_pinpoint_jp'
5
+
6
+ forecast = WeatherPinpointJp.get("有楽町", WeatherPinpointJp::STATION)
7
+
8
+ puts forecast.location
9
+ t = forecast.start_time
10
+ forecast.weather.each {|w|
11
+ puts "#{t.strftime("%Y/%m/%d %H:%M")} #{w}"
12
+ t += 3600
13
+ }
@@ -0,0 +1,119 @@
1
+ <weathernews>
2
+ <data>
3
+ <day startHour="13" startYear="2013" startMonth="11" startDate="12" startDay="2">
4
+ <weather>
5
+ <hour>100</hour>
6
+ <hour>200</hour>
7
+ <hour>300</hour>
8
+ <hour>400</hour>
9
+ <hour>100</hour>
10
+ <hour>200</hour>
11
+ <hour>300</hour>
12
+ <hour>400</hour>
13
+ <hour>100</hour>
14
+ <hour>200</hour>
15
+ <hour>300</hour>
16
+ <hour>400</hour>
17
+ <hour>100</hour>
18
+ <hour>200</hour>
19
+ <hour>300</hour>
20
+ <hour>400</hour>
21
+ <hour>100</hour>
22
+ <hour>200</hour>
23
+ <hour>300</hour>
24
+ <hour>400</hour>
25
+ <hour>100</hour>
26
+ <hour>200</hour>
27
+ <hour>300</hour>
28
+ <hour>400</hour>
29
+ <hour>100</hour>
30
+ <hour>200</hour>
31
+ <hour>300</hour>
32
+ <hour>400</hour>
33
+ <hour>100</hour>
34
+ <hour>200</hour>
35
+ <hour>300</hour>
36
+ <hour>400</hour>
37
+ <hour>100</hour>
38
+ <hour>200</hour>
39
+ <hour>300</hour>
40
+ <hour>400</hour>
41
+ </weather>
42
+ <temperature>
43
+ <hour>1</hour>
44
+ <hour>2</hour>
45
+ <hour>3</hour>
46
+ <hour>4</hour>
47
+ <hour>5</hour>
48
+ <hour>6</hour>
49
+ <hour>7</hour>
50
+ <hour>8</hour>
51
+ <hour>9</hour>
52
+ <hour>10</hour>
53
+ <hour>11</hour>
54
+ <hour>12</hour>
55
+ <hour>13</hour>
56
+ <hour>14</hour>
57
+ <hour>15</hour>
58
+ <hour>16</hour>
59
+ <hour>17</hour>
60
+ <hour>18</hour>
61
+ <hour>19</hour>
62
+ <hour>20</hour>
63
+ <hour>21</hour>
64
+ <hour>22</hour>
65
+ <hour>23</hour>
66
+ <hour>24</hour>
67
+ <hour>25</hour>
68
+ <hour>26</hour>
69
+ <hour>27</hour>
70
+ <hour>28</hour>
71
+ <hour>29</hour>
72
+ <hour>30</hour>
73
+ <hour>31</hour>
74
+ <hour>32</hour>
75
+ <hour>33</hour>
76
+ <hour>34</hour>
77
+ <hour>35</hour>
78
+ <hour>36</hour>
79
+ </temperature>
80
+ <precipitation>
81
+ <hour>11</hour>
82
+ <hour>12</hour>
83
+ <hour>13</hour>
84
+ <hour>14</hour>
85
+ <hour>15</hour>
86
+ <hour>16</hour>
87
+ <hour>17</hour>
88
+ <hour>18</hour>
89
+ <hour>19</hour>
90
+ <hour>20</hour>
91
+ <hour>21</hour>
92
+ <hour>22</hour>
93
+ <hour>23</hour>
94
+ <hour>24</hour>
95
+ <hour>25</hour>
96
+ <hour>26</hour>
97
+ <hour>27</hour>
98
+ <hour>28</hour>
99
+ <hour>29</hour>
100
+ <hour>30</hour>
101
+ <hour>31</hour>
102
+ <hour>32</hour>
103
+ <hour>33</hour>
104
+ <hour>34</hour>
105
+ <hour>35</hour>
106
+ <hour>36</hour>
107
+ <hour>37</hour>
108
+ <hour>38</hour>
109
+ <hour>39</hour>
110
+ <hour>40</hour>
111
+ <hour>41</hour>
112
+ <hour>42</hour>
113
+ <hour>43</hour>
114
+ <hour>44</hour>
115
+ <hour>45</hour>
116
+ <hour>46</hour>
117
+ </precipitation>
118
+ </day>
119
+ </weathernews>
data/test/test_load.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
4
+ require 'minitest/autorun'
5
+
6
+ require 'weather_pinpoint_jp'
7
+
8
+ class TestLoad < Minitest::Test
9
+ def setup
10
+ end
11
+
12
+ def test_load_failed
13
+ f = nil
14
+ begin
15
+ f = WeatherPinpointJp.load("test_data", File.dirname(__FILE__) + "/aaa.xml")
16
+ rescue Exception => e
17
+ end
18
+ assert_equal f.nil?, true
19
+ end
20
+
21
+ def test_load
22
+ f = WeatherPinpointJp.load("test_data", File.dirname(__FILE__) + "/test_data.xml")
23
+ assert_equal f.nil?, false, "load() failed..."
24
+
25
+ # start time
26
+ assert_equal f.start_time, Time.new(2013, 11, 12, 13, 0, 0)
27
+
28
+ # weather
29
+ assert_equal f.weather.size, 36
30
+ f.weather.each_with_index {|w, i|
31
+ w = ((i % 4) + 1) * 100
32
+ }
33
+
34
+ # temperature
35
+ assert_equal f.temperature.size, 36
36
+ f.temperature.each_with_index {|t, i|
37
+ assert_equal t, i + 1
38
+ }
39
+
40
+ # precipitation
41
+ assert_equal f.precipitation.size, 36
42
+ f.precipitation.each_with_index {|p, i|
43
+ assert_equal p, i + 1 + 10
44
+ }
45
+ end
46
+ end
47
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'weather_pinpoint_jp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "weather_pinpoint_jp"
8
+ spec.version = WeatherPinpointJp::VERSION
9
+ spec.authors = ["yoggy"]
10
+ spec.email = ["yoggy0@gmail.com"]
11
+ spec.description = %q{weather information library for ruby}
12
+ spec.summary = %q{weather information library for ruby}
13
+ spec.homepage = "https://github.com/yoggy/weather_pinpoint_jp"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+
25
+ spec.add_dependency "nokogiri", "~> 1.6"
26
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weather_pinpoint_jp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - yoggy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
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
+ description: weather information library for ruby
79
+ email:
80
+ - yoggy0@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - lib/weather_pinpoint_jp.rb
91
+ - lib/weather_pinpoint_jp/version.rb
92
+ - samples/address.rb
93
+ - samples/postalcode.rb
94
+ - samples/station.rb
95
+ - test/test_data.xml
96
+ - test/test_load.rb
97
+ - weather_pinpoint_jp.gemspec
98
+ homepage: https://github.com/yoggy/weather_pinpoint_jp
99
+ licenses:
100
+ - MIT
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 1.8.23
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: weather information library for ruby
123
+ test_files:
124
+ - test/test_data.xml
125
+ - test/test_load.rb