weather_jp 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module WeatherJp
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
data/lib/weather_jp.rb CHANGED
@@ -7,6 +7,8 @@ require 'rss'
7
7
  require 'nokogiri'
8
8
 
9
9
  module WeatherJp
10
+ class WeatherJpError < StandardError; end
11
+
10
12
  class << self
11
13
  def get(city_name, option = nil)
12
14
  if option
@@ -30,15 +32,15 @@ module WeatherJp
30
32
 
31
33
  def parser(str)
32
34
  if str =~ /((?<city>.*)の
33
- (?<day>今日|きょう|明日|あした|明後日|あさって|3日後|4日後|3日後|4日後)
35
+ (?<day>今日|きょう|今|いま|明日|あした|明後日|あさって|3日後|4日後|3日後|4日後)
34
36
  の(天気|てんき).*) |
35
- ((?<day>今日|きょう|明日|あした|明後日|あさって|3日後|4日後|3日後|4日後)の
37
+ ((?<day>今日|きょう|今|いま|明日|あした|明後日|あさって|3日後|4日後|3日後|4日後)の
36
38
  (?<city>.*)の(天気|てんき))
37
39
  /ux
38
40
  data = Regexp.last_match
39
41
  day = data[:day]
40
42
  case day
41
- when /今日|きょう/u
43
+ when /今日|きょう|今|いま/u
42
44
  day = 'today'
43
45
  when /明日|あした/u
44
46
  day = 'tomorrow'
@@ -49,7 +51,7 @@ module WeatherJp
49
51
  when /4日後|4日後/u
50
52
  day = 4
51
53
  else
52
- raise "No matched"
54
+ raise WeatherJpError, "Can't parse given String"
53
55
  end
54
56
  {day: day, city: data[:city]}
55
57
  else
@@ -94,10 +96,10 @@ module WeatherJp
94
96
  when :day_after_tomorrow
95
97
  day = 2
96
98
  end
97
- raise ArgumentError if @day_weathers[day] == nil
99
+ raise WeatherJpError if @day_weathers[day] == nil
98
100
  @day_weathers[day]
99
101
  rescue
100
- raise ArgumentError,
102
+ raise WeatherJpError,
101
103
  "unvaild argument '#{day}' for get_weather"
102
104
  end
103
105
  end
@@ -141,8 +143,7 @@ module WeatherJp
141
143
  full_name =
142
144
  doc.xpath('//weather').attr('weatherlocationname').value
143
145
  rescue
144
- raise ArgumentError,
145
- "invaild city name '#{city_name}'!"
146
+ raise WeatherJpError, "Cant't parse XML"
146
147
  end
147
148
  [code.slice(3..-1), full_name]
148
149
  end
@@ -156,7 +157,7 @@ module WeatherJp
156
157
  )
157
158
  RSS::Parser.parse(uri, false)
158
159
  rescue
159
- raise StandardError,
160
+ raise WeatherJpError,
160
161
  "the MSN weather sever may be downed, or got invaild city code"
161
162
  end
162
163
  end
@@ -188,8 +189,8 @@ module WeatherJp
188
189
  weathers << h
189
190
  end
190
191
  weathers
191
- rescue
192
- raise StandardError,
192
+ rescue NameError
193
+ raise WeatherJpError,
193
194
  "the MSN weather sever may be downed, or something wrong"
194
195
  end
195
196
  end
@@ -25,20 +25,27 @@ describe "WeatherJp" do
25
25
  end
26
26
 
27
27
  describe ".parser" do
28
- {"東京の今日の天気" => {day: "today", city: "東京"},
29
- "今日の東京の天気" => {day: "today", city: "東京"},
30
- "東京の明日の天気" => {day: "tomorrow", city: "東京"},
31
- "東京の明後日の天気" => {day: "day_after_tomorrow", city: "東京"},
32
- "東京の3日後の天気" => {day: 3, city: "東京"},
33
- "東京の3日後の天気" => {day: 3, city: "東京"},
34
- "東京の4日後の天気" => {day: 4, city: "東京"},
35
- "東京の4日後の天気" => {day: 4, city: "東京"},
36
- "東京の今日の天気教えて〜" => {day: "today", city: "東京"},
37
- "あきるの市の今日の天気" => {day: "today", city: "あきるの市"},
38
- "あきるの市の今日の天気教えなさい" => {day: "today", city: "あきるの市"},
39
- "今日のあきるの市の天気" => {day: "today", city: "あきるの市"}
40
- }.each do |test, expect|
41
- WeatherJp.parser(test).should == expect
28
+ it "should parse japanese string" do
29
+ {
30
+ "東京の今日の天気" => {day: "today", city: "東京"},
31
+ "今日の東京の天気" => {day: "today", city: "東京"},
32
+ "東京のいまの天気" => {day: "today", city: "東京"},
33
+ "いまの東京の天気" => {day: "today", city: "東京"},
34
+ "東京の今の天気" => {day: "today", city: "東京"},
35
+ "今の東京の天気" => {day: "today", city: "東京"},
36
+ "東京の明日の天気" => {day: "tomorrow", city: "東京"},
37
+ "東京の明後日の天気" => {day: "day_after_tomorrow", city: "東京"},
38
+ "東京の3日後の天気" => {day: 3, city: "東京"},
39
+ "東京の3日後の天気" => {day: 3, city: "東京"},
40
+ "東京の4日後の天気" => {day: 4, city: "東京"},
41
+ "東京の4日後の天気" => {day: 4, city: "東京"},
42
+ "東京の今日の天気教えて〜" => {day: "today", city: "東京"},
43
+ "あきるの市の今日の天気" => {day: "today", city: "あきるの市"},
44
+ "あきるの市の今日の天気教えなさい" => {day: "today", city: "あきるの市"},
45
+ "今日のあきるの市の天気" => {day: "today", city: "あきるの市"}
46
+ }.each do |test, expect|
47
+ WeatherJp.parser(test).should == expect
48
+ end
42
49
  end
43
50
  end
44
51
  end
data/spec/weather_spec.rb CHANGED
@@ -74,30 +74,30 @@ describe "Weather" do
74
74
  describe "#get_weather" do
75
75
  it "should accept Symbol argument" do
76
76
  ->(){ @weather.get_weather(:today) }.
77
- should_not raise_error(ArgumentError)
77
+ should_not raise_error(WeatherJp::WeatherJpError)
78
78
  ->(){ @weather.get_weather(:tomorrow) }.
79
- should_not raise_error(ArgumentError)
79
+ should_not raise_error(WeatherJp::WeatherJpError)
80
80
  end
81
81
 
82
82
  it "should accept String argument" do
83
83
  ->(){ @weather.get_weather('today') }.
84
- should_not raise_error(ArgumentError)
84
+ should_not raise_error(WeatherJp::WeatherJpError)
85
85
  ->(){ @weather.get_weather('tomorrow') }.
86
- should_not raise_error(ArgumentError)
86
+ should_not raise_error(WeatherJp::WeatherJpError)
87
87
  end
88
88
 
89
89
  it "should accept 0 to 4 number as argument" do
90
90
  (0..4).each do |n|
91
91
  ->(){ @weather.get_weather(n) }.
92
- should_not raise_error(ArgumentError)
92
+ should_not raise_error(WeatherJp::WeatherJpError)
93
93
  end
94
94
  end
95
95
 
96
- it "should raise ArgumentError when got invaild aregument" do
96
+ it "should raise WeatherJp::WeatherJpError when got invaild aregument" do
97
97
  ->(){ @weather.get_weather(:yesterday) }.
98
- should raise_error(ArgumentError)
98
+ should raise_error(WeatherJp::WeatherJpError)
99
99
  ->(){ @weather.get_weather(5) }.
100
- should raise_error(ArgumentError)
100
+ should raise_error(WeatherJp::WeatherJpError)
101
101
  end
102
102
 
103
103
  it "should return DayWeather object" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weather_jp
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -115,7 +115,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
115
115
  version: '0'
116
116
  segments:
117
117
  - 0
118
- hash: -2712567142271719011
118
+ hash: -4408068253640136415
119
119
  required_rubygems_version: !ruby/object:Gem::Requirement
120
120
  none: false
121
121
  requirements:
@@ -124,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
124
  version: '0'
125
125
  segments:
126
126
  - 0
127
- hash: -2712567142271719011
127
+ hash: -4408068253640136415
128
128
  requirements: []
129
129
  rubyforge_project:
130
130
  rubygems_version: 1.8.23