kisyo 0.0.3 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/kisyo/cache.rb CHANGED
@@ -5,6 +5,7 @@ module Kisyo
5
5
  def initialize
6
6
  @keys = []
7
7
  @values = {}
8
+ @m = Mutex.new
8
9
  end
9
10
 
10
11
  def get(key)
@@ -12,17 +13,23 @@ module Kisyo
12
13
  end
13
14
 
14
15
  def set(key, value)
15
- keys << key
16
- values[key] = value
16
+ m.synchronize do
17
+ if values[key]
18
+ return
19
+ end
17
20
 
18
- if keys.size > CACHE_SIZE
19
- oldest_key = keys.shift
20
- values.delete(oldest_key)
21
+ keys << key
22
+ values[key] = value
23
+
24
+ if keys.size > CACHE_SIZE
25
+ oldest_key = keys.shift
26
+ values.delete(oldest_key)
27
+ end
21
28
  end
22
29
  end
23
30
 
24
31
  private
25
32
 
26
- attr_reader :keys, :values
33
+ attr_reader :keys, :values, :m
27
34
  end
28
35
  end
data/lib/kisyo/daily.rb CHANGED
@@ -17,15 +17,16 @@ module Kisyo
17
17
  return value
18
18
  end
19
19
 
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' % [
21
- location.prefecture_id,
22
- location.block_id,
23
- date.year,
24
- date.month
25
- ]
26
-
27
- content = open(url).read
28
- doc = Nokogiri::HTML(content)
20
+ url =
21
+ 'http://www.data.jma.go.jp/obd/stats/etrn/view/daily_s1.php?prec_no=%s&block_no=%s&year=%i&month=%i&day=01&view=p1' % [
22
+ location.prefecture_id,
23
+ location.block_id,
24
+ date.year,
25
+ date.month
26
+ ]
27
+
28
+ content = URI.open(url).read
29
+ doc = Nokogiri.HTML(content)
29
30
  days = doc.css('div.a_print')
30
31
 
31
32
  raise WeatherInformationNotAvailable if days.size == 0
@@ -35,7 +36,7 @@ module Kisyo
35
36
  values = tr.css('td').map(&:text)
36
37
 
37
38
  k = [date.year, date.month, values[0]].join(',')
38
- cache.set(k, Element::Day.new(*values[1 .. -1]))
39
+ cache.set(k, Element::Day.new(*values[1..-1]))
39
40
  end
40
41
 
41
42
  cache.get(key)
@@ -1,7 +1,32 @@
1
+ require 'csv'
2
+ require 'geocoder'
3
+
1
4
  module Kisyo
2
5
  class Location
3
6
  attr_reader :prefecture_id, :block_id
4
7
 
8
+ def self.distance(lat1, lng1, lat2, lng2)
9
+ Geocoder::Calculations.distance_between([lat1, lng1], [lat2, lng2])
10
+ end
11
+
12
+ def self.dms_to_degrees(d, m, s = 0)
13
+ d + m / 60.0 + s / 3600.0
14
+ end
15
+
16
+ def self.nearest(lat, lng)
17
+ distances =
18
+ BLOCKS.map do |block|
19
+ la = dms_to_degrees(block[3].to_f, block[4].to_f)
20
+ ln = dms_to_degrees(block[5].to_f, block[6].to_f)
21
+
22
+ [distance(lat, lng, la, ln), block]
23
+ end
24
+
25
+ block = distances.min_by { |(dist, _)| dist }[1]
26
+
27
+ new(block[1], block[2])
28
+ end
29
+
5
30
  def initialize(prefecture_id, block_id)
6
31
  @prefecture_id = prefecture_id
7
32
  @block_id = block_id
data/lib/kisyo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kisyo
2
- VERSION = "0.0.3"
2
+ VERSION = '0.1.1'
3
3
  end
data/lib/kisyo.rb CHANGED
@@ -1,9 +1,10 @@
1
- require "kisyo/error"
2
- require "kisyo/location"
3
- require "kisyo/cache"
4
- require "kisyo/daily"
5
- require "kisyo/elements/day"
6
- require "kisyo/version"
1
+ require 'kisyo/error'
2
+ require 'kisyo/blocks'
3
+ require 'kisyo/location'
4
+ require 'kisyo/cache'
5
+ require 'kisyo/daily'
6
+ require 'kisyo/elements/day'
7
+ require 'kisyo/version'
7
8
 
8
9
  module Kisyo
9
10
  end
@@ -2,31 +2,22 @@
2
2
  require File.dirname(__FILE__) + '/../spec_helper'
3
3
 
4
4
  describe Kisyo::Daily do
5
- let(:date) {
6
- Date.parse('2016-11-02')
7
- }
8
-
9
- let(:location) {
10
- Kisyo::Location.new(44, 47662)
11
- }
12
-
13
- let(:daily) {
14
- Kisyo::Daily.new(location)
15
- }
5
+ let(:date) { Date.parse('2016-11-02') }
6
+ let(:location) { Kisyo::Location.new('44', '47662') }
7
+ let(:daily) { Kisyo::Daily.new(location) }
16
8
 
17
9
  describe '#at' do
18
- let(:url) {
10
+ let(:url) do
19
11
  'http://www.data.jma.go.jp/obd/stats/etrn/view/daily_s1.php?block_no=47662&day=01&month=11&prec_no=44&view=p1&year=2016'
20
- }
12
+ end
21
13
 
22
- let(:fixture_file_name) {
23
- '201611.html'
24
- }
14
+ let(:fixture_file_name) { '201611.html' }
25
15
 
26
16
  context 'single request' do
27
17
  before do
28
- stub_request(:get, url).
29
- to_return(:body => read_fixture_file(fixture_file_name))
18
+ stub_request(:get, url).to_return(
19
+ body: read_fixture_file(fixture_file_name)
20
+ )
30
21
  end
31
22
 
32
23
  context 'information is available' do
@@ -48,21 +39,17 @@ describe Kisyo::Daily do
48
39
  end
49
40
 
50
41
  context 'information is not available' do
51
- let(:fixture_file_name) {
52
- 'ng.html'
53
- }
42
+ let(:fixture_file_name) { 'ng.html' }
54
43
 
55
44
  it 'raises error' do
56
- expect {
57
- daily.at(date)
58
- }.to raise_error(Kisyo::WeatherInformationNotAvailable)
45
+ expect { daily.at(date) }.to raise_error(
46
+ Kisyo::WeatherInformationNotAvailable
47
+ )
59
48
  end
60
49
  end
61
50
 
62
51
  context 'value is "--"' do
63
- let(:date) {
64
- Date.parse('2016-11-04')
65
- }
52
+ let(:date) { Date.parse('2016-11-04') }
66
53
 
67
54
  it '"--" is converted to nil' do
68
55
  info = daily.at(date)
@@ -75,9 +62,9 @@ describe Kisyo::Daily do
75
62
  end
76
63
  context 'multiple request' do
77
64
  before do
78
- stub_request(:get, url).
79
- to_return(:body => read_fixture_file(fixture_file_name)).
80
- times(number_of_request)
65
+ stub_request(:get, url)
66
+ .to_return(body: read_fixture_file(fixture_file_name))
67
+ .times(number_of_request)
81
68
  end
82
69
 
83
70
  context '2016-11-01 - 2016-11-05' do
@@ -0,0 +1,13 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ describe Kisyo::Location do
5
+ describe '.nearest' do
6
+ it 'returns the location closest to the specified latitude and longitude' do
7
+ loc = Kisyo::Location.nearest(35.6809591, 139.7673068)
8
+
9
+ expect(loc.prefecture_id).to eql('44')
10
+ expect(loc.block_id).to eql('47662')
11
+ end
12
+ end
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kisyo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - youpy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-09 00:00:00.000000000 Z
11
+ date: 2022-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -25,33 +25,33 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: bundler
28
+ name: geocoder
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '1.7'
34
- type: :development
33
+ version: '0'
34
+ type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '1.7'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: 12.3.3
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: 12.3.3
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -95,6 +95,7 @@ files:
95
95
  - Rakefile
96
96
  - kisyo.gemspec
97
97
  - lib/kisyo.rb
98
+ - lib/kisyo/blocks.rb
98
99
  - lib/kisyo/cache.rb
99
100
  - lib/kisyo/daily.rb
100
101
  - lib/kisyo/elements/day.rb
@@ -104,6 +105,7 @@ files:
104
105
  - spec/fixtures/201611.html
105
106
  - spec/fixtures/ng.html
106
107
  - spec/kisyo/daily_spec.rb
108
+ - spec/kisyo/location_spec.rb
107
109
  - spec/spec_helper.rb
108
110
  homepage: ''
109
111
  licenses:
@@ -124,8 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
126
  - !ruby/object:Gem::Version
125
127
  version: '0'
126
128
  requirements: []
127
- rubyforge_project:
128
- rubygems_version: 2.5.1
129
+ rubygems_version: 3.1.6
129
130
  signing_key:
130
131
  specification_version: 4
131
132
  summary: A ruby tool for getting weather information from www.data.jma.go.jp
@@ -133,4 +134,5 @@ test_files:
133
134
  - spec/fixtures/201611.html
134
135
  - spec/fixtures/ng.html
135
136
  - spec/kisyo/daily_spec.rb
137
+ - spec/kisyo/location_spec.rb
136
138
  - spec/spec_helper.rb