kisyo 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,30 @@
1
+ require 'csv'
2
+
1
3
  module Kisyo
2
4
  class Location
3
5
  attr_reader :prefecture_id, :block_id
4
6
 
7
+ def self.dms_to_degrees(d, m, s = 0)
8
+ d + m / 60.0 + s / 3600.0
9
+ end
10
+
11
+ def self.nearest(lat, lng)
12
+ CSV.open(File.dirname(__FILE__) + '/../blocks.csv') do |csv|
13
+ distances =
14
+ csv.map do |row|
15
+ la = dms_to_degrees(row[5].to_f, row[6].to_f)
16
+ ln = dms_to_degrees(row[7].to_f, row[8].to_f)
17
+
18
+ [Math.sqrt((lat - la)**2 + (lng - ln)**2), row]
19
+ end
20
+
21
+ distance = distances.min_by { |(dist, _)| dist }
22
+ row = distance[1]
23
+
24
+ new(row[3], row[4])
25
+ end
26
+ end
27
+
5
28
  def initialize(prefecture_id, block_id)
6
29
  @prefecture_id = prefecture_id
7
30
  @block_id = block_id
data/lib/kisyo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kisyo
2
- VERSION = "0.0.4"
2
+ VERSION = '0.1.0'
3
3
  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.4
4
+ version: 0.1.0
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-10 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
@@ -24,34 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '1.7'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '1.7'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: rake
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
- - - "~>"
31
+ - - ">="
46
32
  - !ruby/object:Gem::Version
47
- version: '10.0'
33
+ version: 12.3.3
48
34
  type: :development
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
52
- - - "~>"
38
+ - - ">="
53
39
  - !ruby/object:Gem::Version
54
- version: '10.0'
40
+ version: 12.3.3
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: rspec
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +80,7 @@ files:
94
80
  - README.md
95
81
  - Rakefile
96
82
  - kisyo.gemspec
83
+ - lib/blocks.csv
97
84
  - lib/kisyo.rb
98
85
  - lib/kisyo/cache.rb
99
86
  - lib/kisyo/daily.rb
@@ -104,6 +91,7 @@ files:
104
91
  - spec/fixtures/201611.html
105
92
  - spec/fixtures/ng.html
106
93
  - spec/kisyo/daily_spec.rb
94
+ - spec/kisyo/location_spec.rb
107
95
  - spec/spec_helper.rb
108
96
  homepage: ''
109
97
  licenses:
@@ -124,8 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
112
  - !ruby/object:Gem::Version
125
113
  version: '0'
126
114
  requirements: []
127
- rubyforge_project:
128
- rubygems_version: 2.5.1
115
+ rubygems_version: 3.1.6
129
116
  signing_key:
130
117
  specification_version: 4
131
118
  summary: A ruby tool for getting weather information from www.data.jma.go.jp
@@ -133,4 +120,5 @@ test_files:
133
120
  - spec/fixtures/201611.html
134
121
  - spec/fixtures/ng.html
135
122
  - spec/kisyo/daily_spec.rb
123
+ - spec/kisyo/location_spec.rb
136
124
  - spec/spec_helper.rb