weather_jp 0.1.1 → 1.0.0

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 = "0.1.1"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/weather_jp.rb CHANGED
@@ -7,17 +7,53 @@ require 'rss'
7
7
  require 'nokogiri'
8
8
 
9
9
  module WeatherJp
10
- def self.get(city_name, option = nil)
11
- if option
12
- Weather.new(city_name).get_weather(option)
13
- else
14
- Weather.new(city_name)
10
+
11
+ class << self
12
+ def get(city_name, option = nil)
13
+ if option
14
+ Weather.new(city_name).get_weather(option)
15
+ else
16
+ Weather.new(city_name)
17
+ end
18
+ end
19
+
20
+ def customize_to_s(&code)
21
+ Weather::DayWeather.class_eval do
22
+ define_method(:to_s, &code)
23
+ end
15
24
  end
16
- end
17
25
 
18
- def self.customize_to_s(&code)
19
- Weather::DayWeather.class_eval do
20
- define_method(:to_s, &code)
26
+ def parse(str)
27
+ if day_and_city = parser(str)
28
+ WeatherJp.get(day_and_city[:city]).get_weather(day_and_city[:day])
29
+ end
30
+ end
31
+
32
+ def parser(str)
33
+ if str =~ /((?<city>.*)の
34
+ (?<day>今日|きょう|明日|あした|明後日|あさって|3日後|4日後|3日後|4日後)の(天気|てんき).*) |
35
+ ((?<day>今日|きょう|明日|あした|明後日|あさって|3日後|4日後|3日後|4日後)の
36
+ (?<city>.*)の(天気|てんき))/ux then
37
+ data = Regexp.last_match
38
+ day = data[:day]
39
+ case day
40
+ when /今日|きょう/u
41
+ day = 'today'
42
+ when /明日|あした/u
43
+ day = 'tomorrow'
44
+ when /明後日|あさって/u
45
+ day = 'day_after_tomorrow'
46
+ when /3日後|3日後/u
47
+ day = 3
48
+ when /4日後|4日後/u
49
+ day = 4
50
+ else
51
+ raise "No matched"
52
+ end
53
+ {day: day, city: data[:city]}
54
+ else
55
+ nil
56
+ end
21
57
  end
22
58
  end
23
59
 
@@ -23,5 +23,29 @@ describe "WeatherJp" do
23
23
  WeatherJp.get(:tokyo, :today).to_s.should == 'success'
24
24
  end
25
25
  end
26
+
27
+ describe ".parse" do
28
+ WeatherJp.parse("今日の東京の天気教えて").to_s.should =~ /東京都\s東京の今日の天気は.*です。/u
29
+ end
30
+
31
+ describe ".parser" do
32
+ {"東京の今日の天気" => {day: "today", city: "東京"},
33
+ "今日の東京の天気" => {day: "today", city: "東京"},
34
+ "東京の明日の天気" => {day: "tomorrow", city: "東京"},
35
+ "東京の明後日の天気" => {day: "day_after_tomorrow", city: "東京"},
36
+ "東京の3日後の天気" => {day: 3, city: "東京"},
37
+ "東京の3日後の天気" => {day: 3, city: "東京"},
38
+ "東京の4日後の天気" => {day: 4, city: "東京"},
39
+ "東京の4日後の天気" => {day: 4, city: "東京"},
40
+ "東京の今日の天気教えて〜" => {day: "today", city: "東京"},
41
+ #"ねえ、東京の今日の天気教えなさいよ" => {day: "today", city: "東京"},
42
+ #"ε( ε^o^)э東京の今日の天気教えてほしいでし…!" => {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
49
+ end
26
50
  end
27
51
 
data/spec/weather_spec.rb CHANGED
@@ -143,10 +143,12 @@ describe "Weather" do
143
143
  end
144
144
 
145
145
  describe "with fixtures" do
146
- before(:all) do
146
+ before :all do
147
147
  dummy = ''
148
148
  rss_one {|rss| dummy = RSS::Parser.parse rss }
149
149
  WeatherJp::Weather.class_exec dummy do |dummy|
150
+ alias :back_code :get_area_code
151
+ alias :back_rss :get_rss
150
152
  define_method(:get_area_code) {|city_name| ["JAXX0085", 'tokyo'] }
151
153
  define_method(:get_rss) { dummy }
152
154
  end
@@ -182,5 +184,12 @@ describe "Weather" do
182
184
  end
183
185
  end
184
186
  end
187
+
188
+ after :all do
189
+ WeatherJp::Weather.class_exec do
190
+ alias :get_area_code :back_code
191
+ alias :get_rss :back_rss
192
+ end
193
+ end
185
194
  end
186
195
  end
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: 0.1.1
4
+ version: 1.0.0
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: 2012-09-30 00:00:00.000000000 Z
12
+ date: 2012-10-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -112,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
112
  version: '0'
113
113
  segments:
114
114
  - 0
115
- hash: 113366814983018964
115
+ hash: 1993595247945938449
116
116
  required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  none: false
118
118
  requirements:
@@ -121,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  version: '0'
122
122
  segments:
123
123
  - 0
124
- hash: 113366814983018964
124
+ hash: 1993595247945938449
125
125
  requirements: []
126
126
  rubyforge_project:
127
127
  rubygems_version: 1.8.23