weather_jp 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in weather_jp.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Taiki ONO
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,58 @@
1
+ ## About
2
+
3
+ Fetch Japan weather info as Ruby object easily.
4
+
5
+ http://taiki45.github.com/weather_jp
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'weather_jp'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install weather_jp
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ # creat weather object in differrnt ways
25
+ tokyo = WeatherJP::Weather.new :tokyo
26
+ minato = WeatherJp::Weather.new("東京都港区")
27
+
28
+ # get weather info as String
29
+ tokyo.get_weather(:today).to_s
30
+ #=> can be "東京都 東京の天気は曇りのち晴れ、最高気温34度...etc"
31
+
32
+ # to get weather info at 4 day later now
33
+ minato.get_weather(4) #=> <#DayWeather object>
34
+ minato.get_weather(:today).forecast #=> can be "晴れ"
35
+
36
+ # use Weather object
37
+ tokyo.each do |w|
38
+ puts w.city_name
39
+ puts w.forecast
40
+ puts w.max_temp
41
+ w.each_pair {|k,v| puts k, v }
42
+ end
43
+
44
+ minato.map {|w| [w.day, w.forecast] }
45
+
46
+ # or use as simple Array or Hash
47
+ tokyo.to_a
48
+ minato.each {|w| p w.to_hash }
49
+
50
+ ```
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "bundler"
4
+
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+
13
+ require "rspec/core"
14
+ require "rspec/core/rake_task"
15
+
16
+ RSpec::Core::RakeTask.new(:spec)
17
+
18
+ task :default => :spec
@@ -0,0 +1,48 @@
1
+ # -*- coding:utf-8 -*-
2
+
3
+ module WeatherJp
4
+ class Weather
5
+ class DayWeather
6
+ include Enumerable
7
+
8
+ def initialize(weathers, city_name, day)
9
+ @weather = weathers[day]
10
+ @city_name = city_name
11
+ end
12
+
13
+ attr_reader :city_name
14
+
15
+ def to_hash
16
+ @weather
17
+ end
18
+
19
+ def each
20
+ @weather.each {|_k, v| yield v }
21
+ end
22
+
23
+ def each_pair
24
+ @weather.each_pair {|k,v| yield k, v }
25
+ end
26
+
27
+ def inspect
28
+ word = "\#<DayWeather:@city_name = #{city_name}, "
29
+ word << "@day=#{day}, @forecast=#{forecast}, "
30
+ word << "@max_temp=#{max_temp}, @rain=#{rain}>"
31
+ word
32
+ end
33
+
34
+ def to_s
35
+ word = "#{city_name}の"
36
+ word << "#{day}の天気は"
37
+ word << "#{forecast}、"
38
+ word << "最高気温#{max_temp}度、"
39
+ word << "降水確率は#{rain}%です。"
40
+ word
41
+ end
42
+
43
+ def method_missing(key)
44
+ @weather[key]
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module WeatherJp
2
+ VERSION = "0.0.1"
3
+ end
data/lib/weather_jp.rb ADDED
@@ -0,0 +1,116 @@
1
+ # -*- coding:utf-8 -*-
2
+ require 'weather_jp/day_weather'
3
+ require 'weather_jp/version'
4
+ require 'uri'
5
+ require 'open-uri'
6
+ require 'rss'
7
+ require 'nokogiri'
8
+
9
+ module WeatherJp
10
+ class Weather
11
+ include Enumerable
12
+
13
+ def initialize(city_name)
14
+ city_name = city_name.to_s if city_name.class == Symbol
15
+ @area_code, @city_name = get_area_code(city_name)
16
+ @weathers = set_weathers
17
+ @day_weathers = Array.new(@weathers.size) do |n|
18
+ DayWeather.new(@weathers,@city_name, n)
19
+ end
20
+ end
21
+
22
+ attr_reader :city_name, :area_code, :day_weathers
23
+
24
+ public
25
+ def to_hash
26
+ @weathers
27
+ end
28
+
29
+ alias to_a to_hash
30
+
31
+ def each
32
+ @day_weathers.each do |i|
33
+ yield i
34
+ end
35
+ self
36
+ end
37
+
38
+ def get_weather(day = 0)
39
+ begin
40
+ day = day.to_sym if day.class == String
41
+ case day
42
+ when :today
43
+ day = 0
44
+ when :tomorrow
45
+ day = 1
46
+ end
47
+ raise ArgumentError if @day_weathers[day] == nil
48
+ @day_weathers[day]
49
+ rescue
50
+ raise ArgumentError,
51
+ "unvaild argument '#{day}' for get_weather"
52
+ end
53
+ end
54
+
55
+ private
56
+ def set_weathers
57
+ begin
58
+ weathers = Array.new
59
+ get_weather_data.each do |i|
60
+ h = Hash.new
61
+ h[:day] = i.slice(/(.*?):\s+(.*?)\./, 1)
62
+ h[:forecast] = i.slice(/(.*?):\s+(.*?)\./, 2)
63
+ h[:max_temp] = i.slice(/(最高).*?(\d+)/u, 2)
64
+ h[:min_temp] = i.slice(/(最低).*?(\d+)/u, 2)
65
+ h[:rain] = i.slice(/(降水確率).*?(\d+)/u, 2)
66
+ weathers << h
67
+ end
68
+ weathers
69
+ rescue
70
+ raise StandardError,
71
+ "the MSN weather sever may be downed, or something wrong"
72
+ end
73
+ end
74
+
75
+ def get_area_code(city_name)
76
+ uri = URI.encode(
77
+ "http://weather.service.msn.com/" + \
78
+ "find.aspx?outputview=search&weadegreetype=C&culture=ja-JP&" + \
79
+ "weasearchstr=#{city_name}")
80
+ doc = Nokogiri::XML(open(uri))
81
+ begin
82
+ code =
83
+ doc.xpath('//weather').attr('weatherlocationcode').value
84
+ full_name =
85
+ doc.xpath('//weather').attr('weatherlocationname').value
86
+ rescue
87
+ raise ArgumentError,
88
+ "invaild city name '#{city_name}'!"
89
+ end
90
+ [code.slice(3..-1), full_name]
91
+ end
92
+
93
+
94
+ def get_weather_data
95
+ begin
96
+ uri = URI.parse(
97
+ "http://weather.jp.msn.com/" + \
98
+ "RSS.aspx?wealocations=wc:#{@area_code}&" + \
99
+ "weadegreetype=C&culture=ja-JP")
100
+ rss = RSS::Parser.parse(uri, false)
101
+ str = rss.channel.item(0).description
102
+ data = remove_html_tag(str).split(/%/)
103
+ data.pop
104
+ data.map {|i| i.delete!('"') }
105
+ rescue
106
+ raise StandardError,
107
+ "the MSN weather sever may be downed, or got invaild city code"
108
+ end
109
+ end
110
+
111
+ def remove_html_tag(string)
112
+ string.gsub!(/<(\"[^\"]*\"|'[^']*'|[^'\">])*>/,'""')
113
+ end
114
+ end
115
+ end
116
+
@@ -0,0 +1,77 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe "DayWeather" do
5
+ before(:all) do
6
+ @tokyo = WeatherJp::Weather.new(:tokyo)
7
+ @weather = @tokyo.get_weather(:today)
8
+ end
9
+
10
+ describe "#initialize" do
11
+ it "should have @city_name" do
12
+ @weather.city_name.should == "東京都 東京"
13
+ end
14
+
15
+ it "should have @weather as Hash" do
16
+ @weather.instance_variable_get(:@weather).class.
17
+ should == Hash
18
+ end
19
+ end
20
+
21
+ describe "#inspect" do
22
+ it "should return String" do
23
+ @weather.inspect.class.should == String
24
+ end
25
+
26
+ it "should have certain format"
27
+ end
28
+
29
+ describe "#to_s" do
30
+ it "should return String" do
31
+ @weather.to_s.class.should == String
32
+ end
33
+
34
+ it "should be certain string when certain environment"
35
+ end
36
+
37
+ describe "#to_hash" do
38
+ it "should return Hash" do
39
+ @weather.to_hash.class.should == Hash
40
+ end
41
+
42
+ it "should be certain structure" do
43
+ @weather.to_hash.should have_key(:day)
44
+ @weather.to_hash.should have_key(:forecast)
45
+ @weather.to_hash.should have_key(:max_temp)
46
+ @weather.to_hash.should have_key(:rain)
47
+ end
48
+
49
+ it "should have certain value when certain env"
50
+ end
51
+
52
+ describe "#each" do
53
+ it "should yield only value" do
54
+ @weather.each {|v| v.class.should_not == Array }
55
+ @weather.each do |v, n|
56
+ n.should == nil
57
+ end
58
+ end
59
+
60
+ it "should yield value" do
61
+ ->(){ @weather.each {|v|} }.should_not raise_error(Exception)
62
+ end
63
+ end
64
+
65
+ describe "#each_pair" do
66
+ it "should yield pair" do
67
+ ->(){ @weather.each_pair {|k,v|} }.
68
+ should_not raise_error(Exception)
69
+ end
70
+ end
71
+
72
+ describe "Enumerable#methods" do
73
+ it "should not raise error when use Enumerable#mehod" do
74
+ ->(){ @weather.map{|v| v } }.should_not raise_error(NoMethodError)
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,7 @@
1
+ # -*- coding: utf-8 -*-
2
+ $LOAD_PATH.unshift File.expand_path("../", __FILE__)
3
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
4
+
5
+ require 'rubygems'
6
+ require 'nokogiri'
7
+ require 'weather_jp'
@@ -0,0 +1,138 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe "Weather" do
6
+ before(:all) do
7
+ @weather = WeatherJp::Weather.new(:tokyo)
8
+ end
9
+
10
+ describe "#initialize" do
11
+ it "should have @area_code and can access" do
12
+ @weather.area_code.should == "JAXX0085"
13
+ end
14
+
15
+ it "should have @city_name and can access" do
16
+ @weather.city_name.should == "東京都 東京"
17
+ end
18
+
19
+ it "should accept String argument" do
20
+ weather = WeatherJp::Weather.new "東京都府中市"
21
+ weather.city_name.should == "東京都 府中市"
22
+ end
23
+
24
+ it "should accept Symbol argument" do
25
+ weather = WeatherJp::Weather.new :tokyo
26
+ weather.city_name.should == "東京都 東京"
27
+ end
28
+
29
+ it "should have @day_weathers as Array" do
30
+ @weather.day_weathers.class.should == Array
31
+ end
32
+
33
+ it "should have DayWeather instance in @day_weathers" do
34
+ @weather.day_weathers.each do |w|
35
+ w.class.should == WeatherJp::Weather::DayWeather
36
+ end
37
+ end
38
+
39
+ it "should have 5 DayWeather instance in @day_weathers" do
40
+ @weather.day_weathers.size.should == 5
41
+ end
42
+ end
43
+
44
+ describe "#get_area_code" do
45
+ it "should get vaild area code" do
46
+ @weather.area_code.should == "JAXX0085"
47
+ end
48
+
49
+ it "should raise error when invaild city name taken" do
50
+ ->(){ WeatherJp::Weather.new(:aaa) }.should raise_error ArgumentError
51
+ end
52
+ end
53
+
54
+ describe "#get_weather_data" do
55
+ it "should vaild unprocessing weather data"
56
+
57
+ it "should raise StandardError when server connection is lost"
58
+ end
59
+
60
+ describe "#remove_html_tag" do
61
+ it "should remove html tags"
62
+ end
63
+
64
+ describe "#to_hash" do
65
+ it "should return Array and include Hashs" do
66
+ @weather.to_hash.class.should == Array
67
+ @weather.to_hash.each {|e| e.class.should == Hash }
68
+ end
69
+
70
+ it "should return with vaild structure" do
71
+ @weather.to_hash.each do |e|
72
+ e.should have_key(:day)
73
+ e.should have_key(:forecast)
74
+ e.should have_key(:max_temp)
75
+ e.should have_key(:rain)
76
+ end
77
+ end
78
+ end
79
+
80
+ describe "#to_a" do
81
+ it "should return and behavior sama as #to_hash" do
82
+ @weather.to_a.should equal(@weather.to_hash)
83
+ end
84
+ end
85
+
86
+ describe "#each" do
87
+ it "should yield DayWeather object" do
88
+ @weather.each {|w| w.class.should == WeatherJp::Weather::DayWeather }
89
+ end
90
+ end
91
+
92
+ describe "Enumerable mehods" do
93
+ it "should respond to Enumerable methods" do
94
+ @weather.should respond_to(:map)
95
+ end
96
+
97
+ it "should yield DayWeather Object" do
98
+ @weather.map {|w| w.class.should == WeatherJp::Weather::DayWeather }
99
+ end
100
+ end
101
+
102
+ describe "#get_weather" do
103
+ it "should accept Symbol argument" do
104
+ ->(){ @weather.get_weather(:today) }.
105
+ should_not raise_error(ArgumentError)
106
+ ->(){ @weather.get_weather(:tomorrow) }.
107
+ should_not raise_error(ArgumentError)
108
+ end
109
+
110
+ it "should accept String argument" do
111
+ ->(){ @weather.get_weather('today') }.
112
+ should_not raise_error(ArgumentError)
113
+ ->(){ @weather.get_weather('tomorrow') }.
114
+ should_not raise_error(ArgumentError)
115
+ end
116
+
117
+ it "should accept 0 to 4 number as argument" do
118
+ (0..4).each do |n|
119
+ ->(){ @weather.get_weather(n) }.
120
+ should_not raise_error(ArgumentError)
121
+ end
122
+ end
123
+
124
+ it "should raise ArgumentError when got invaild aregument" do
125
+ ->(){ @weather.get_weather(:yesterday) }.
126
+ should raise_error(ArgumentError)
127
+ ->(){ @weather.get_weather(5) }.
128
+ should raise_error(ArgumentError)
129
+ end
130
+
131
+ it "should return DayWeather object" do
132
+ @weather.get_weather(0).class.should == WeatherJp::Weather::DayWeather
133
+ end
134
+ end
135
+ end
136
+
137
+
138
+
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/weather_jp/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Taiki ONO"]
6
+ gem.email = ["taiks.4559@gmail.com"]
7
+ gem.description = "Fetch Japan weather info as Ruby object easily."
8
+ gem.summary = "Summary."
9
+ gem.homepage = "http://taiki45.github.com/weather_jp"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "weather_jp"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = WeatherJp::VERSION
17
+
18
+ gem.add_dependency "nokogiri", "~>1.5.5"
19
+ gem.add_dependency "rake", ">= 0.9.2"
20
+ gem.add_development_dependency "rspec"
21
+ gem.add_development_dependency "rspec-core"
22
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weather_jp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Taiki ONO
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.5.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.5.5
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.9.2
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.9.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
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: rspec-core
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Fetch Japan weather info as Ruby object easily.
79
+ email:
80
+ - taiks.4559@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE
88
+ - README.md
89
+ - Rakefile
90
+ - lib/weather_jp.rb
91
+ - lib/weather_jp/day_weather.rb
92
+ - lib/weather_jp/version.rb
93
+ - spec/day_weather_spec.rb
94
+ - spec/spec_helper.rb
95
+ - spec/weather_spec.rb
96
+ - weather_jp.gemspec
97
+ homepage: http://taiki45.github.com/weather_jp
98
+ licenses: []
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ segments:
110
+ - 0
111
+ hash: -3045644943114962078
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ segments:
119
+ - 0
120
+ hash: -3045644943114962078
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 1.8.23
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: Summary.
127
+ test_files:
128
+ - spec/day_weather_spec.rb
129
+ - spec/spec_helper.rb
130
+ - spec/weather_spec.rb