kisyo 0.0.2 → 0.0.3

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: 6d5eabd129f1b2191de6fdd0116779aa1104a091
4
- data.tar.gz: 563fda3733d0e577057212146d90d23112801716
3
+ metadata.gz: d8a99f62b8535e4a98f5852dfe53fafa38452f83
4
+ data.tar.gz: 99a1477bb5740fcda0fea9b75b0434491ce70933
5
5
  SHA512:
6
- metadata.gz: f0c75847c3f5fa34c177eeabb730291936e38457a531ad3f504a7aa8eec2251ef474222f021a8d0897c2c31b4241d1faa13a9f88596b631f35c0413aa797f694
7
- data.tar.gz: d63e03065ce76e92f2b6a9de8846794c86f570062cefaa37c710e38622c20aec370a28cf84077f39e2774f61244fa1325b9279674f006ab72e3a56b15dac79f8
6
+ metadata.gz: d8c8bb2db8af4f8950f4fb9c173df90b63d35bf931dc505e45aaa0eeba91bdc71e5805500ac64032f6e1c3f7519cfdd989e4455eedb7622563c7c8d482c73744
7
+ data.tar.gz: 8a8e10cc74bd021ef9dc5921f8163447233d19f0e009a0ee91590ea5c92388a19432b0d55344aa0e5bc0f1532cb0b83bcdaab5a91ce5b92e8a746d90c048f2ed
@@ -1,5 +1,6 @@
1
1
  require "kisyo/error"
2
2
  require "kisyo/location"
3
+ require "kisyo/cache"
3
4
  require "kisyo/daily"
4
5
  require "kisyo/elements/day"
5
6
  require "kisyo/version"
@@ -0,0 +1,28 @@
1
+ module Kisyo
2
+ class Cache
3
+ CACHE_SIZE = 100
4
+
5
+ def initialize
6
+ @keys = []
7
+ @values = {}
8
+ end
9
+
10
+ def get(key)
11
+ values[key]
12
+ end
13
+
14
+ def set(key, value)
15
+ keys << key
16
+ values[key] = value
17
+
18
+ if keys.size > CACHE_SIZE
19
+ oldest_key = keys.shift
20
+ values.delete(oldest_key)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :keys, :values
27
+ end
28
+ end
@@ -3,14 +3,23 @@ require 'open-uri'
3
3
 
4
4
  module Kisyo
5
5
  class Daily
6
+ CACHE_SIZE = 100
7
+
6
8
  def initialize(location)
7
9
  @location = location
10
+ @cache = Cache.new
8
11
  end
9
12
 
10
13
  def at(date)
14
+ key = [date.year, date.month, date.day].join(',')
15
+
16
+ if value = cache.get(key)
17
+ return value
18
+ end
19
+
11
20
  url = 'http://www.data.jma.go.jp/obd/stats/etrn/view/daily_s1.php?prec_no=%i&block_no=%i&year=%i&month=%i&day=01&view=p1' % [
12
- @location.prefecture_id,
13
- @location.block_id,
21
+ location.prefecture_id,
22
+ location.block_id,
14
23
  date.year,
15
24
  date.month
16
25
  ]
@@ -22,13 +31,18 @@ module Kisyo
22
31
  raise WeatherInformationNotAvailable if days.size == 0
23
32
 
24
33
  days.each do |el|
25
- if el.text.to_i == date.day
26
- tr = el.parent.parent
27
- values = tr.css('td').map(&:text)
34
+ tr = el.parent.parent
35
+ values = tr.css('td').map(&:text)
28
36
 
29
- return Element::Day.new(*values[1 .. -1])
30
- end
37
+ k = [date.year, date.month, values[0]].join(',')
38
+ cache.set(k, Element::Day.new(*values[1 .. -1]))
31
39
  end
40
+
41
+ cache.get(key)
32
42
  end
43
+
44
+ private
45
+
46
+ attr_reader :cache, :location
33
47
  end
34
48
  end
@@ -1,3 +1,3 @@
1
1
  module Kisyo
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -23,52 +23,77 @@ describe Kisyo::Daily do
23
23
  '201611.html'
24
24
  }
25
25
 
26
- before do
27
- stub_request(:get, url).
28
- to_return(:body => read_fixture_file(fixture_file_name))
29
- end
26
+ context 'single request' do
27
+ before do
28
+ stub_request(:get, url).
29
+ to_return(:body => read_fixture_file(fixture_file_name))
30
+ end
31
+
32
+ context 'information is available' do
33
+ it 'returns weather information for given day' do
34
+ info = daily.at(date)
30
35
 
31
- context 'information is available' do
32
- it 'returns weather information for given day' do
33
- info = daily.at(date)
34
-
35
- expect(info.precipitation_total).to eql(1.5)
36
- expect(info.precipitation_hourly_max).to eql(1.0)
37
- expect(info.precipitation_10min_max).to eql(0.5)
38
- expect(info.temperature_avg).to eql(10.9)
39
- expect(info.temperature_max).to eql(12.2)
40
- expect(info.temperature_min).to eql(8.6)
41
- expect(info.humidity_avg).to eql(68.0)
42
- expect(info.humidity_min).to eql(53.0)
43
- expect(info.day_length).to eql(0.0)
44
- expect(info.general_weather_condition_day).to eql('曇後時々雨')
45
- expect(info.general_weather_condition_night).to eql('雨時々曇')
36
+ expect(info.precipitation_total).to eql(1.5)
37
+ expect(info.precipitation_hourly_max).to eql(1.0)
38
+ expect(info.precipitation_10min_max).to eql(0.5)
39
+ expect(info.temperature_avg).to eql(10.9)
40
+ expect(info.temperature_max).to eql(12.2)
41
+ expect(info.temperature_min).to eql(8.6)
42
+ expect(info.humidity_avg).to eql(68.0)
43
+ expect(info.humidity_min).to eql(53.0)
44
+ expect(info.day_length).to eql(0.0)
45
+ expect(info.general_weather_condition_day).to eql('曇後時々雨')
46
+ expect(info.general_weather_condition_night).to eql('雨時々曇')
47
+ end
46
48
  end
47
- end
48
49
 
49
- context 'information is not available' do
50
- let(:fixture_file_name) {
51
- 'ng.html'
52
- }
50
+ context 'information is not available' do
51
+ let(:fixture_file_name) {
52
+ 'ng.html'
53
+ }
54
+
55
+ it 'raises error' do
56
+ expect {
57
+ daily.at(date)
58
+ }.to raise_error(Kisyo::WeatherInformationNotAvailable)
59
+ end
60
+ end
61
+
62
+ context 'value is "--"' do
63
+ let(:date) {
64
+ Date.parse('2016-11-04')
65
+ }
66
+
67
+ it '"--" is converted to nil' do
68
+ info = daily.at(date)
53
69
 
54
- it 'raises error' do
55
- expect {
56
- daily.at(date)
57
- }.to raise_error(Kisyo::WeatherInformationNotAvailable)
70
+ expect(info.precipitation_total).to be_nil
71
+ expect(info.precipitation_hourly_max).to be_nil
72
+ expect(info.precipitation_10min_max).to be_nil
73
+ end
58
74
  end
59
75
  end
76
+ context 'multiple request' do
77
+ before do
78
+ stub_request(:get, url).
79
+ to_return(:body => read_fixture_file(fixture_file_name)).
80
+ times(number_of_request)
81
+ end
82
+
83
+ context '2016-11-01 - 2016-11-05' do
84
+ let(:number_of_request) { 1 }
60
85
 
61
- context 'value is "--"' do
62
- let(:date) {
63
- Date.parse('2016-11-04')
64
- }
86
+ it 'caches and reuses first response' do
87
+ date = Date.parse('2016-11-01')
65
88
 
66
- it '"--" is converted to nil' do
67
- info = daily.at(date)
89
+ expect(daily.at(date).precipitation_total).to eql(10.0)
90
+ expect(daily.at(date + 1).precipitation_total).to eql(1.5)
91
+ expect(daily.at(date + 2).precipitation_total).to eql(3.0)
92
+ expect(daily.at(date + 3).precipitation_total).to be_nil
93
+ expect(daily.at(date + 4).precipitation_total).to eql(0.0)
68
94
 
69
- expect(info.precipitation_total).to be_nil
70
- expect(info.precipitation_hourly_max).to be_nil
71
- expect(info.precipitation_10min_max).to be_nil
95
+ expect(a_request(:get, url)).to have_been_made.times(1)
96
+ end
72
97
  end
73
98
  end
74
99
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kisyo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - youpy
@@ -95,6 +95,7 @@ files:
95
95
  - Rakefile
96
96
  - kisyo.gemspec
97
97
  - lib/kisyo.rb
98
+ - lib/kisyo/cache.rb
98
99
  - lib/kisyo/daily.rb
99
100
  - lib/kisyo/elements/day.rb
100
101
  - lib/kisyo/error.rb