jipcode-address_locator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 29133c15968ba53a9a3184f33d98e3758abf44b9ff0e6d17cd2c168e50b6dea4
4
+ data.tar.gz: a73ff328a9fb2dbcbee58606f18ecb9a79fceb245f0537500ac2edcf54deeac1
5
+ SHA512:
6
+ metadata.gz: 74a9c74f430c5e1d03df327f15577e3f3447d3c76c41b994a8101fcfb264c010a3c9bdd8d3949ac7f6a99b81c7eaf729bd20e578c9d9ff63975d3ec5ede21361
7
+ data.tar.gz: adfc4b5bbb956dc2d5b866427fcc461ca1c2b5b79ef6e732a27b69a89fee37c94704493b926103ffed07ec00a04ffe102e4cc375d88e38bf834e80d4d617ecf5
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .byebug_history
14
+
15
+ /zipcode/by_prefecture/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,15 @@
1
+ Naming/FileName:
2
+ Enabled: false
3
+
4
+ Metrics/BlockLength:
5
+ Exclude:
6
+ - 'spec/**/*'
7
+
8
+ Metrics/LineLength:
9
+ Max: 130
10
+
11
+ Style/Documentation:
12
+ Enabled: false
13
+
14
+ Style/AsciiComments:
15
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ sudo: false
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.5.0
6
+ before_install:
7
+ - gem install bundler -v 1.16.1
8
+ script:
9
+ - bundle exec rubocop
10
+ - bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in jipcode.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,56 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ jipcode-address_locator (0.0.1)
5
+ jaro_winkler (~> 1.5.3)
6
+ jipcode (~> 1.5.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.0)
12
+ byebug (11.0.1)
13
+ diff-lcs (1.3)
14
+ jaro_winkler (1.5.3)
15
+ jipcode (1.5.0)
16
+ parallel (1.17.0)
17
+ parser (2.6.3.0)
18
+ ast (~> 2.4.0)
19
+ rainbow (3.0.0)
20
+ rake (10.5.0)
21
+ rspec (3.8.0)
22
+ rspec-core (~> 3.8.0)
23
+ rspec-expectations (~> 3.8.0)
24
+ rspec-mocks (~> 3.8.0)
25
+ rspec-core (3.8.2)
26
+ rspec-support (~> 3.8.0)
27
+ rspec-expectations (3.8.4)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.8.0)
30
+ rspec-mocks (3.8.1)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.8.0)
33
+ rspec-support (3.8.2)
34
+ rubocop (0.73.0)
35
+ jaro_winkler (~> 1.5.1)
36
+ parallel (~> 1.10)
37
+ parser (>= 2.6)
38
+ rainbow (>= 2.2.2, < 4.0)
39
+ ruby-progressbar (~> 1.7)
40
+ unicode-display_width (>= 1.4.0, < 1.7)
41
+ ruby-progressbar (1.10.1)
42
+ unicode-display_width (1.6.0)
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ bundler (~> 1.16)
49
+ byebug
50
+ jipcode-address_locator!
51
+ rake (~> 10.0)
52
+ rspec (~> 3.0)
53
+ rubocop
54
+
55
+ BUNDLED WITH
56
+ 1.17.2
data/LICENSE ADDED
@@ -0,0 +1,43 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Kei Hayashi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+
23
+ MIT License
24
+
25
+ Copyright (c) 2019 oieioi
26
+
27
+ Permission is hereby granted, free of charge, to any person obtaining a copy
28
+ of this software and associated documentation files (the "Software"), to deal
29
+ in the Software without restriction, including without limitation the rights
30
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
31
+ copies of the Software, and to permit persons to whom the Software is
32
+ furnished to do so, subject to the following conditions:
33
+
34
+ The above copyright notice and this permission notice shall be included in all
35
+ copies or substantial portions of the Software.
36
+
37
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ [![Build Status](https://travis-ci.com/oieioi/jipcode-address_locator.svg?branch=master)](https://travis-ci.com/oieioi/jipcode-address_locator)
2
+
3
+ # Jipcode::AddressLocator
4
+
5
+ [Jipcode](http://rubygems.org/gems/jipcode)に住所から郵便番号データを検索するメソッド(`locate_by_address`)を追加します。
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'jipcode-address_locator'
13
+ ```
14
+ And then execute:
15
+
16
+ ```
17
+ $ bundle install
18
+ ```
19
+
20
+ Or install it yourself as:
21
+
22
+ ```
23
+ $ gem install jipcode-address_locator
24
+ ```
25
+
26
+ ## 使用方法
27
+
28
+ ### 住所から郵便番号情報を検索する
29
+
30
+ [Jaro-Winkler](https://rubygems.org/gems/jaro_winkler)距離計算の結果から近い順に郵便番号情報を返します。
31
+
32
+ [Jipcode](http://rubygems.org/gems/jipcode)の最新郵便番号のインデックスをまだ持っていない場合インデックスファイルを作成します。なので初回の処理は重いです
33
+
34
+ ```ruby
35
+ require 'jipcode'
36
+ require 'jipcode/address_locator'
37
+
38
+ Jipcode.locate_by_address '東京都千代田区千代田1-1', prefecture_code: true, distance: true
39
+ # [
40
+ # {:zipcode=>"1000001", :prefecture=>"東京都", :city=>"千代田区", :town=>"千代田", :prefecture_code=>13, :distance=>0.9538461538461538},
41
+ # {:zipcode=>"1000000", :prefecture=>"東京都", :city=>"千代田区", :town=>nil, :prefecture_code=>13, :distance=>0.9076923076923077}
42
+ # ]
43
+ ```
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'jipcode-address_locator'
6
+ require 'byebug'
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ # (If you use this, don't forget to add pry to your Gemfile!)
12
+ # require "pry"
13
+ # Pry.start
14
+
15
+ require 'irb'
16
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'jipcode/address_locator/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'jipcode-address_locator'
9
+ spec.version = Jipcode::AddressLocator::VERSION
10
+ spec.authors = ['oieioi']
11
+ spec.email = ['atsuinatsu.samuifuyu@gmail.com']
12
+
13
+ spec.summary = 'Extend jipcode.gem to locate zipcode data by address'
14
+ spec.description = 'Extend jipcode.gem to locate zipcode data by address'
15
+ spec.homepage = 'https://github.com/oieioi/jipcode-address_locator'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'jaro_winkler', '~> 1.5.3'
24
+ spec.add_dependency 'jipcode', '~> 1.5.0'
25
+ spec.add_development_dependency 'bundler', '~> 1.16'
26
+ spec.add_development_dependency 'byebug'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ spec.add_development_dependency 'rubocop'
30
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jipcode
4
+ module AddressLocator
5
+ # @private
6
+ def self.extract_prefecture_code(address)
7
+ prefecture_name = address.match(/\A(#{prefecture_names.join('|')})/).to_s
8
+ PREFECTURE_CODE.invert[prefecture_name]
9
+ end
10
+
11
+ # @private
12
+ def self.prefecture_names
13
+ PREFECTURE_CODE.values
14
+ end
15
+
16
+ private_class_method :prefecture_names, :extract_prefecture_code
17
+ end
18
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'csv'
4
+ require 'jipcode/address_locator/helper'
5
+
6
+ module Jipcode
7
+ module AddressLocator
8
+ # Make sure Jipcode version and index version are the same.
9
+ # @return [Boolean] result
10
+ def self.exist_latest_index?
11
+ return false unless File.exist?(INDEX_VERSION_FILE)
12
+
13
+ version = File.read(INDEX_VERSION_FILE)
14
+ version == Jipcode::VERSION
15
+ end
16
+
17
+ # Create index files by Jipcode data.
18
+ def self.create_index!
19
+ refresh_index_dir
20
+
21
+ index = collect_index
22
+
23
+ export_index(index)
24
+
25
+ File.open(INDEX_VERSION_FILE, 'w') do |f|
26
+ f.write(Jipcode::VERSION)
27
+ end
28
+ rescue StandardError => e
29
+ raise e, 'Failed to create index'
30
+ end
31
+
32
+ # @private
33
+ def self.refresh_index_dir
34
+ FileUtils.rm_rf(INDEX_PATH) if File.exist?(INDEX_PATH)
35
+ Dir.mkdir(INDEX_PATH)
36
+ end
37
+
38
+ # @private
39
+ def self.collect_index
40
+ # 都道府県コードは1から始まるので一つ余計に作る
41
+ index = Array.new(PREFECTURE_CODE.size + 1) { [] }
42
+
43
+ Dir.glob("#{ZIPCODE_PATH}/*.csv").each do |file_name|
44
+ CSV.read(file_name).each do |row|
45
+ _zipcode, prefecture_name, _city, _town = row
46
+ prefecture_code = extract_prefecture_code(prefecture_name)
47
+ index[prefecture_code] << row
48
+ end
49
+ end
50
+ index.shift
51
+ index
52
+ end
53
+
54
+ # @private
55
+ def self.export_index(index)
56
+ index.each.with_index(1) do |rows, prefecture_code|
57
+ rows.sort_by! { |row| [row[0], row[2], row[3]] }
58
+ CSV.open("#{INDEX_PATH}/#{prefecture_code}.csv", 'wb') do |csv|
59
+ rows.each { |row| csv << row }
60
+ end
61
+ end
62
+ end
63
+
64
+ private_class_method :refresh_index_dir, :collect_index, :export_index
65
+ end
66
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'csv'
4
+ require 'jaro_winkler'
5
+ require 'jipcode/address_locator/helper'
6
+
7
+ module Jipcode
8
+ module AddressLocator
9
+ # Locate zipcode data by address
10
+ # @param [String] search_address
11
+ # @return [Array<Hash>] zipcode data
12
+ def self.locate(search_address)
13
+ find_by_address(search_address)
14
+ .yield_self { |addresses| calc_and_add_distance(addresses, search_address) }
15
+ .sort_by(&:last)
16
+ .reverse
17
+ end
18
+
19
+ # @private
20
+ def self.find_by_address(search_address)
21
+ prefecture_code = extract_prefecture_code(search_address)
22
+ path = "#{INDEX_PATH}/#{prefecture_code}.csv"
23
+ return [] if prefecture_code.nil?
24
+
25
+ CSV.read(path).select do |row|
26
+ address = row[1..3].join('')
27
+ # 長いほうが短い方に含まれてるか判別
28
+ long = [address, search_address].max
29
+ short = [address, search_address].min
30
+ long.start_with?(short)
31
+ end
32
+ end
33
+
34
+ # @private
35
+ def self.calc_and_add_distance(addresses, search_address)
36
+ addresses.map do |row|
37
+ combined = row[1..3].join('')
38
+ distance = JaroWinkler.distance(combined, search_address)
39
+ row << distance
40
+ end
41
+ end
42
+
43
+ private_class_method :calc_and_add_distance, :find_by_address
44
+ end
45
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jipcode
4
+ module AddressLocator
5
+ VERSION = '0.0.1'
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jipcode/address_locator/locator'
4
+ require 'jipcode/address_locator/indexer'
5
+ require 'jipcode/address_locator/version'
6
+
7
+ module Jipcode
8
+ module AddressLocator
9
+ INDEX_PATH = "#{File.dirname(__FILE__)}/../../zipcode/by_prefecture/latest"
10
+ INDEX_VERSION_FILE = "#{File.dirname(__FILE__)}/../../zipcode/by_prefecture/version"
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jipcode/address_locator'
4
+
5
+ module Jipcode
6
+ # Find zipcode data by address.
7
+ # @note If there are no index files, this method creates them from jipcode.gem data.
8
+ # @param [String] search_address
9
+ # @param [Hash] opt the options to create results.
10
+ # @option opt [String] :distance return with jaro winkler distance
11
+ # @return [Array<Hash>] zipcode data
12
+ def self.locate_by_address(search_address, opt = {})
13
+ AddressLocator.create_index! unless AddressLocator.exist_latest_index?
14
+
15
+ AddressLocator.locate(search_address).map do |address_param|
16
+ address = extended_address_from(address_param, opt)
17
+ address[:distance] = address_param[4] if opt[:distance]
18
+ address
19
+ end
20
+ end
21
+ end
File without changes
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jipcode-address_locator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - oieioi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jaro_winkler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.5.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.5.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: jipcode
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.5.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.5.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Extend jipcode.gem to locate zipcode data by address
112
+ email:
113
+ - atsuinatsu.samuifuyu@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".rubocop.yml"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - Gemfile.lock
124
+ - LICENSE
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - jipcode-address_locator.gemspec
130
+ - lib/jipcode-address_locator.rb
131
+ - lib/jipcode/address_locator.rb
132
+ - lib/jipcode/address_locator/helper.rb
133
+ - lib/jipcode/address_locator/indexer.rb
134
+ - lib/jipcode/address_locator/locator.rb
135
+ - lib/jipcode/address_locator/version.rb
136
+ - zipcode/by_prefecture/latest/.gitkeep
137
+ homepage: https://github.com/oieioi/jipcode-address_locator
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubygems_version: 3.0.3
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Extend jipcode.gem to locate zipcode data by address
160
+ test_files: []