jp_address 0.0.6 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/jp_address/zipcodes_controller.rb +1 -1
- data/lib/jp_address/version.rb +1 -1
- metadata +3 -4
- data/README.rdoc +0 -175
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47f6300a25d529f2ec5b5448d584b57c7c21acdf5da71e8e1cdbf7c1f2ca2324
|
4
|
+
data.tar.gz: 1779a70d87802bf6c76ea248e82ff1e06ab519ec566bcca644707115bbb8525a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65c7442c0e8a62f79c0b22730fb61ddcac2fd734fa8fb83abff2d6759c7462f83de449b9d7d83ff1b1cc198215b216b4e72e81061e8d8874b6d10714bc7a9bec
|
7
|
+
data.tar.gz: 3ef126b87da8a5271f315190f9e76a5c83f93cf5b4d0998417ba7747ade6ecc44efb3a17d21e8b94dd23b7d47cb090bbf16eb9b189501cb35f3e9a32f52b0963
|
@@ -3,7 +3,7 @@ require_dependency "jp_address/application_controller"
|
|
3
3
|
module JpAddress
|
4
4
|
class ZipcodesController < ApplicationController
|
5
5
|
def search
|
6
|
-
@zipcode = Zipcode.find_by(:zip => params[:zip]) || Zipcode.new
|
6
|
+
@zipcode = Zipcode.find_by(:zip => params[:zip].to_s.gsub(/[^0-9]/, '')) || Zipcode.new
|
7
7
|
render plain: @zipcode.to_json
|
8
8
|
end
|
9
9
|
end
|
data/lib/jp_address/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jp_address
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tad Kam
|
@@ -108,8 +108,8 @@ dependencies:
|
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
-
description: JpAddress is simple japan-zipcode-
|
112
|
-
and
|
111
|
+
description: JpAddress is simple japan-zipcode-address-search API. You can load master-data
|
112
|
+
from JapanPost and mount address-search-api to your rails application.
|
113
113
|
email:
|
114
114
|
- densya203@skult.jp
|
115
115
|
executables: []
|
@@ -117,7 +117,6 @@ extensions: []
|
|
117
117
|
extra_rdoc_files: []
|
118
118
|
files:
|
119
119
|
- MIT-LICENSE
|
120
|
-
- README.rdoc
|
121
120
|
- Rakefile
|
122
121
|
- app/assets/javascripts/jp_address/application.js
|
123
122
|
- app/assets/stylesheets/jp_address/application.css
|
data/README.rdoc
DELETED
@@ -1,175 +0,0 @@
|
|
1
|
-
= Ruby on Rails 6 で 郵便番号住所検索 な gem
|
2
|
-
|
3
|
-
== JpAddressとは
|
4
|
-
日本郵便の「郵便番号データ(https://www.post.japanpost.jp/zipcode/dl/kogaki/zip/ken_all.zip)」を Rails 6.1 で使用するための gem です。
|
5
|
-
以下の機能を提供します。
|
6
|
-
|
7
|
-
* 郵便局提供の郵便番号データをダウンロードして自前DBのテーブル(jp_address_zipcodes)にロードするクラスメソッド。(JpAddress::Zipcode.load_master_data)
|
8
|
-
* 郵便番号を受け取り住所情報をJSONで返却するAPI(jp_address/zipcodes#search)。
|
9
|
-
|
10
|
-
要するに、「郵便番号住所検索 ruby gem」でググった人向けの gem です。
|
11
|
-
お使いのRailsアプリケーションにマウントして使えますので、後必要なのは戻ってくるJSONを加工する手順だけです。
|
12
|
-
|
13
|
-
== インストール
|
14
|
-
% gem install jp_address
|
15
|
-
|
16
|
-
GemFileに追記
|
17
|
-
gem 'jp_address'
|
18
|
-
|
19
|
-
テーブルの作成
|
20
|
-
% bundle exec rails jp_address:install:migrations
|
21
|
-
% bundle exec rails db:migrate
|
22
|
-
|
23
|
-
== マスターデータのDBへのロード
|
24
|
-
開発環境
|
25
|
-
% rails runner -e development 'JpAddress::Zipcode.load_master_data'
|
26
|
-
本番環境
|
27
|
-
% rails runner -e production 'JpAddress::Zipcode.load_master_data'
|
28
|
-
|
29
|
-
環境にもよりますが、1~3分ぐらいかかると思います。
|
30
|
-
|
31
|
-
※ APP_ROOT/tmp/ を作業ディレクトリに使用しています。
|
32
|
-
※ 最初にテーブルをトランケートしますので、毎回「全件insert」になります。
|
33
|
-
|
34
|
-
== APIのマウント
|
35
|
-
マウントしたいRailsアプリの config/routes.rb に追記。
|
36
|
-
mount JpAddress::Engine, at: "/jp_address"
|
37
|
-
|
38
|
-
== APIの利用
|
39
|
-
/jp_address にマウントした場合、下記URLへGETリクエストをすることで、JSONを取得できます。
|
40
|
-
後はこれを好きに加工してテキストボックスなどにセットして使ってください。
|
41
|
-
|
42
|
-
get リクエスト先
|
43
|
-
http://localhost:3000/jp_address/zipcodes/search?zip=5330033
|
44
|
-
|
45
|
-
戻り値 JSON
|
46
|
-
{"id":84280,"zip":"5330033","prefecture":"大阪府","city":"大阪市東淀川区","town":"東中島"}
|
47
|
-
|
48
|
-
== APIを利用するためのサンプル JavaScript
|
49
|
-
フォームには
|
50
|
-
* #zipcode(テキストボックス)
|
51
|
-
* #prefecture_id(セレクトボックス。いわゆる都道府県プルダウン)
|
52
|
-
* #address(テキストボックス)
|
53
|
-
の3要素があり、#zipcodeに入れられた値を元にAPIを叩き、#prefecture_id と #address に
|
54
|
-
値をセットするサンプルです。
|
55
|
-
|
56
|
-
prefecture_id の選択は、都道府県名で行っています。
|
57
|
-
なので、お使いの都道府県マスターのID値に影響を受けることなく選択できるはずです。
|
58
|
-
|
59
|
-
※ JQuery の存在を前提にしています。
|
60
|
-
※ もともと CoffeeScript で書いてあったソースを decaffeinate したものです。
|
61
|
-
|
62
|
-
// ここから
|
63
|
-
function _classCallCheck(instance, Constructor) {
|
64
|
-
if (!(instance instanceof Constructor)) {
|
65
|
-
throw new TypeError("Cannot call a class as a function");
|
66
|
-
}
|
67
|
-
}
|
68
|
-
|
69
|
-
function _defineProperties(target, props) {
|
70
|
-
for (var i = 0; i < props.length; i++) {
|
71
|
-
var descriptor = props[i];
|
72
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
73
|
-
descriptor.configurable = true;
|
74
|
-
|
75
|
-
if ("value" in descriptor)
|
76
|
-
descriptor.writable = true;
|
77
|
-
|
78
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
79
|
-
}
|
80
|
-
}
|
81
|
-
|
82
|
-
function _createClass(Constructor, protoProps, staticProps) {
|
83
|
-
if (protoProps)
|
84
|
-
_defineProperties(Constructor.prototype, protoProps);
|
85
|
-
|
86
|
-
if (staticProps)
|
87
|
-
_defineProperties(Constructor, staticProps);
|
88
|
-
|
89
|
-
return Constructor;
|
90
|
-
}
|
91
|
-
|
92
|
-
var AddressSearch = function() {
|
93
|
-
"use strict";
|
94
|
-
function AddressSearch(zip_elem_id, prefecture_elem_id, address_elem_id) {
|
95
|
-
_classCallCheck(this, AddressSearch);
|
96
|
-
this.zip = $(zip_elem_id);
|
97
|
-
this.prefecture = $(prefecture_elem_id);
|
98
|
-
this.address = $(address_elem_id);
|
99
|
-
this.prefecture_elem_id = prefecture_elem_id;
|
100
|
-
}
|
101
|
-
|
102
|
-
_createClass(AddressSearch, [{
|
103
|
-
key: "_remove_hyphen",
|
104
|
-
|
105
|
-
value: function _remove_hyphen() {
|
106
|
-
return this.zip.val(this.zip.val().replace(/-/, ''));
|
107
|
-
}
|
108
|
-
}, {
|
109
|
-
key: "_clear_current_value",
|
110
|
-
|
111
|
-
value: function _clear_current_value() {
|
112
|
-
$(this.prefecture_elem_id + ' >option:eq(0)').prop('selected', true);
|
113
|
-
return this.address.val('');
|
114
|
-
}
|
115
|
-
}, {
|
116
|
-
key: "_set_prefecture",
|
117
|
-
|
118
|
-
value: function _set_prefecture(json) {
|
119
|
-
return $(this.prefecture_elem_id + ' > option').each(function() {
|
120
|
-
if ($(this).text() === json['prefecture']) {
|
121
|
-
return $(this).prop('selected', true);
|
122
|
-
}
|
123
|
-
});
|
124
|
-
}
|
125
|
-
}, {
|
126
|
-
key: "_set_address",
|
127
|
-
|
128
|
-
value: function _set_address(json) {
|
129
|
-
return this.address.val(json['city'] + json['town']);
|
130
|
-
}
|
131
|
-
}, {
|
132
|
-
key: "_call_api",
|
133
|
-
|
134
|
-
value: function _call_api() {
|
135
|
-
var _this = this;
|
136
|
-
return $.getJSON('/jp_address/zipcodes/search', {zip: this.zip.val()}, function(json) {
|
137
|
-
if (json['id'] === null) {
|
138
|
-
return _this._clear_current_value();
|
139
|
-
} else {
|
140
|
-
_this._set_prefecture(json);
|
141
|
-
return _this._set_address(json);
|
142
|
-
}
|
143
|
-
});
|
144
|
-
}
|
145
|
-
}, {
|
146
|
-
key: "execute",
|
147
|
-
|
148
|
-
value: function execute() {
|
149
|
-
this._remove_hyphen();
|
150
|
-
if (this.zip.val().length === 7) {
|
151
|
-
return this._call_api();
|
152
|
-
}
|
153
|
-
}
|
154
|
-
}]);
|
155
|
-
|
156
|
-
return AddressSearch;
|
157
|
-
}();
|
158
|
-
// ここまでを application.js など共通に読み込まれるファイルに配置。
|
159
|
-
|
160
|
-
// ここから
|
161
|
-
// #zipcode, #prefecture_id, #address を各自の環境に合わせて書き換えてください。
|
162
|
-
$(function() {
|
163
|
-
var address_search = new AddressSearch('#zipcode', '#prefecture_id', '#address');
|
164
|
-
$('#zipcode').keyup(function() {
|
165
|
-
address_search.execute();
|
166
|
-
});
|
167
|
-
});
|
168
|
-
// ここまでをフォームのあるページに配置。
|
169
|
-
|
170
|
-
|
171
|
-
== 作者
|
172
|
-
|
173
|
-
Copyright 2016 (c) Tad Kam, under MIT License.
|
174
|
-
|
175
|
-
Tad Kam <densya203@skult.jp>
|