weatherscout 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 935355d42555116fed7cd482ce7dad6ae7a8a37b
4
- data.tar.gz: 6518fc9a70d79116fd45e8ef71a46093b0f14a77
3
+ metadata.gz: 8b0d63da54f05eea838d5a10bed697dcd1d735eb
4
+ data.tar.gz: db40952ef17dee2abf0ebe33df5ec1a0fd8d3dc2
5
5
  SHA512:
6
- metadata.gz: e16fc9b14fc90784da1105e459d863fe60d12b26b93f3af9035d0965b6a63223228a9e9768b80f24e8a531460261653d55f138af70cabb54c36da91f313d9339
7
- data.tar.gz: bbe6aa56c145f5018568c508b3029b52fc7a140212fea6bf92713005df638f20e03caa4e91c31e8ae4c229c7109e492b8f8393b3af2cb86a5c571bd1e8aa2c36
6
+ metadata.gz: 4587da3cc3e1945bfb4285c792f5032d07c11925112e4de641ba7aa08761e5ed0a3b7e7af6a99bd78e19bd23a0d5aa7e2c7ba909f25f698e633b1233ffd9c793
7
+ data.tar.gz: 86e041ebf87ee31d42b40714a3386911661064fa0550cd798d46e8a4ee9cb1da13c236d4eb4c7ae74e358142bba4bb3c421bf6b2abb5cd7f3ff62049b003f97e
data/bin/weatherscout CHANGED
@@ -8,17 +8,16 @@ class WeatherCLI < Thor
8
8
  def today(city)
9
9
  weather = DailyWeatherScraper::DailyWeather.new(city)
10
10
  city_eng = weather.city
11
- date = weather.date
12
- temperature_day = weather.temperature_day
13
- temperature_night = weather.temperature_night
14
- rain_probability = weather.rain_probability
15
- condition_day = weather.condition_day
16
- condition_night = weather.condition_night
11
+ weathers = weather.weathers
17
12
  tips = weather.tips
18
- puts "#{city}(#{city_eng})在 #{date} 的天氣狀況:"
19
- puts "白天氣象:#{condition_day},氣溫 #{temperature_day} 度"
20
- puts "晚上氣象:#{condition_night},氣溫 #{temperature_night} 度"
21
- puts "降雨機率:#{rain_probability}"
13
+ puts "#{city}(#{city_eng})的天氣預測:"
14
+ weathers.each do |timestamp, weather|
15
+ attributes = weather.values
16
+ puts "#{timestamp}"
17
+ puts " -氣溫:#{attributes[0]} 度"
18
+ puts " -降雨機率:#{attributes[1]}"
19
+ puts " -天氣狀況:#{attributes[2]}"
20
+ end
22
21
  puts "溫馨叮嚀:"
23
22
  puts "#{tips}"
24
23
  end
@@ -8,36 +8,16 @@ module DailyWeatherScraper
8
8
  CITY_WEATHER_URL = "#{CWB_URL}/V7/forecast/taiwan/"
9
9
 
10
10
  def initialize(city)
11
- @city = name_mapping(city)
12
- parse_html(@city)
11
+ @city = name_mapping(city).delete("_")
12
+ parse_html(name_mapping(city))
13
13
  end
14
14
 
15
15
  def city
16
16
  @city
17
17
  end
18
18
 
19
- def date
20
- @data ||= parse_date
21
- end
22
-
23
- def temperature_day
24
- @temperature_day ||= parse_temp_day
25
- end
26
-
27
- def temperature_night
28
- @temperature_night ||= parse_temp_night
29
- end
30
-
31
- def rain_probability
32
- @rain_probability ||= parse_rain_prob
33
- end
34
-
35
- def condition_day
36
- @condition_day ||= parse_condition_day
37
- end
38
-
39
- def condition_night
40
- @condition_night ||= parse_condition_night
19
+ def weathers
20
+ @weathers ||= parse_weathers
41
21
  end
42
22
 
43
23
  def tips
@@ -51,34 +31,18 @@ module DailyWeatherScraper
51
31
  @document = Oga.parse_html(open(url))
52
32
  end
53
33
 
54
- def parse_date
55
- data = @document.xpath("//table[@class='FcstBoxTable01']")
56
- result = data[2].children[3].children[0].children[1].children.text.split(" ")[1]
57
- end
58
-
59
- def parse_temp_day
60
- data = @document.xpath("//table[@class='FcstBoxTable01']")
61
- result = data[2].children[3].children[0].children[3].children.text
62
- end
63
-
64
- def parse_temp_night
65
- data = @document.xpath("//table[@class='FcstBoxTable01']")
66
- result = data[2].children[3].children[1].children[3].children.text
67
- end
68
-
69
- def parse_rain_prob
70
- data = @document.xpath("//table[@class='FcstBoxTable01']")
71
- result = data[2].children[3].children[0].children[9].children.text
72
- end
73
-
74
- def parse_condition_day
75
- data = @document.xpath("//table[@class='FcstBoxTable01']")
76
- result = data[2].children[3].children[0].children[5].children[1].attributes[1].value.force_encoding('UTF-8')
77
- end
78
-
79
- def parse_condition_night
34
+ def parse_weathers
80
35
  data = @document.xpath("//table[@class='FcstBoxTable01']")
81
- result = data[2].children[3].children[1].children[5].children[1].attributes[1].value.force_encoding('UTF-8')
36
+ data[2].children[3].children.map do |weathers|
37
+ timestamp= weathers.children[1].children.text.force_encoding('UTF-8')
38
+ temperature = weathers.children[3].children.text
39
+ rain_probability = weathers.children[9].children.text
40
+ condition = weathers.children[5].children[1].attributes[1].value.force_encoding('UTF-8')
41
+ [timestamp,
42
+ {"temperature" => temperature,
43
+ "rain_probability" => rain_probability,
44
+ "condition" => condition}]
45
+ end.to_h
82
46
  end
83
47
 
84
48
  def parse_tips
@@ -1,5 +1,5 @@
1
1
  # Versioning
2
2
  module WeatherDesc
3
- VERSION = '0.1.1'
4
- DATE = '2016-01-02'
3
+ VERSION = '0.2.0'
4
+ DATE = '2016-01-21'
5
5
  end
@@ -0,0 +1,54 @@
1
+ require 'minitest/autorun'
2
+ require './lib/weatherscout.rb'
3
+
4
+ CITY = "新竹"
5
+ daily_weather = DailyWeatherScraper::DailyWeather.new(CITY)
6
+
7
+ describe 'Daily weather desc scrap' do
8
+
9
+ it 'should return city name' do
10
+ city = daily_weather.city
11
+ city.must_be_instance_of String
12
+ end
13
+
14
+ it 'has the right number of period weathers' do
15
+ weathers = daily_weather.weathers
16
+ weathers.size.must_equal 3
17
+ end
18
+
19
+ it 'should return the timestamp in correct format' do
20
+ timestamps = daily_weather.weathers.keys
21
+ timestamps.each do |timestamp|
22
+ timestamp.must_match /\A.{4,5}\s\d\d\/\d\d\s\d\d:\d\d~\d\d\/\d\d\s\d\d:\d\d/
23
+ end
24
+ end
25
+
26
+ it 'should return temperature' do
27
+ weathers = daily_weather.weathers.values
28
+ weathers.each do |weather|
29
+ temperature = weather["temperature"]
30
+ temperature.must_match /(-|\d)\d+\s~\s(-|\d)\d+/
31
+ end
32
+ end
33
+
34
+ it 'should return weather condition' do
35
+ weathers = daily_weather.weathers.values
36
+ weathers.each do |weather|
37
+ condition = weather["condition"]
38
+ condition.must_be_instance_of String
39
+ end
40
+ end
41
+
42
+ it 'should return rain probaliity' do
43
+ weathers = daily_weather.weathers.values
44
+ weathers.each do |weather|
45
+ probaliity = weather["rain_probability"]
46
+ probaliity.must_match /\d+\s%/
47
+ end
48
+ end
49
+
50
+ it 'should return weather tips' do
51
+ tips = daily_weather.tips
52
+ tips.must_be_instance_of String
53
+ end
54
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weatherscout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vicky Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-02 00:00:00.000000000 Z
11
+ date: 2016-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -70,7 +70,7 @@ files:
70
70
  - lib/weatherdesc/daily_weather.rb
71
71
  - lib/weatherdesc/version.rb
72
72
  - lib/weatherscout.rb
73
- - spec/weatherdesc_spec.rb
73
+ - spec/dailyweather_spec.rb
74
74
  - weatherscout.gemspec
75
75
  homepage: https://github.com/vicky-sunshine/weather-scout-scraper
76
76
  licenses:
@@ -97,4 +97,4 @@ signing_key:
97
97
  specification_version: 4
98
98
  summary: Tell you the weather of Taiwan
99
99
  test_files:
100
- - spec/weatherdesc_spec.rb
100
+ - spec/dailyweather_spec.rb
@@ -1,47 +0,0 @@
1
- require 'minitest/autorun'
2
- require './lib/weatherscout.rb'
3
-
4
- CITY = "新竹"
5
- weather = DailyWeatherScraper::DailyWeather.new(CITY)
6
-
7
- describe 'Daily weather desc scrap' do
8
- it 'should return date with the "month/day" style' do
9
- city = weather.city
10
- city.must_be_instance_of String
11
- end
12
-
13
- it 'should return date with the "month/day" style' do
14
- date = weather.date
15
- date.must_match /\d\d\/\d\d/
16
- end
17
-
18
- it 'should return temperature in daytime' do
19
- temp = weather.temperature_day
20
- temp.must_match /(-|\d)\d+\s~\s(-|\d)\d+/
21
- end
22
-
23
- it 'should return temperature in nighttime' do
24
- temp = weather.temperature_night
25
- temp.must_match /(-|\d)\d+\s~\s(-|\d)\d+/
26
- end
27
-
28
- it 'should return weather condition in daytime' do
29
- con = weather.condition_day
30
- con.must_be_instance_of String
31
- end
32
-
33
- it 'should return weather condition in nighttime' do
34
- con = weather.condition_night
35
- con.must_be_instance_of String
36
- end
37
-
38
- it 'should return rain probaliity' do
39
- prob = weather.rain_probability
40
- prob.must_match /\d+\s%/
41
- end
42
-
43
- it 'should return weather tips' do
44
- tips = weather.tips
45
- tips.must_be_instance_of String
46
- end
47
- end