Class: JpAddressComplement::Normalizer
- Inherits:
-
Object
- Object
- JpAddressComplement::Normalizer
- Defined in:
- lib/jp_address_complement/normalizer.rb
Overview
郵便番号の正規化処理を担当するクラス 全角→半角変換・〒記号除去・ハイフン除去を行う
Constant Summary collapse
- DIGIT_ONLY =
半角数字以外の文字を除去するためのパターン
/\A\d{7}\z/- PREFIX_MIN_LENGTH =
: Integer
4- TOWN_DISPLAY_REMOVAL_STRINGS =
町域から除去する「通常住所に含まれない」固定文字列(漢字・かな両方)
[ '以下に掲載がない場合', 'イカニケイサイガナイバアイ' # 以下に掲載がない場合(カナ) ].freeze
Class Method Summary collapse
-
.normalize_postal_code(code) ⇒ String?
郵便番号文字列を正規化して7桁の半角数字文字列を返す.
-
.normalize_prefix(prefix) ⇒ String?
郵便番号プレフィックスを正規化する.
-
.normalize_town_for_display(town_str) ⇒ String?
町域文字列から「通常住所に含まれない情報」を除いた表示用文字列を返す 除去対象: 「以下に掲載がない場合」、全角括弧()で囲まれた部分全体.
Class Method Details
.normalize_postal_code(code) ⇒ String?
郵便番号文字列を正規化して7桁の半角数字文字列を返す
23 24 25 26 27 28 29 30 |
# File 'lib/jp_address_complement/normalizer.rb', line 23 def normalize_postal_code(code) return nil if code.nil? || code.strip.empty? normalized = normalize_string(code) return nil unless normalized.match?(DIGIT_ONLY) normalized end |
.normalize_prefix(prefix) ⇒ String?
郵便番号プレフィックスを正規化する
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/jp_address_complement/normalizer.rb', line 36 def normalize_prefix(prefix) return nil if prefix.nil? || prefix.strip.empty? normalized = normalize_string(prefix) return nil if normalized.empty? return nil unless normalized.match?(/\A\d+\z/) return nil if normalized.length < PREFIX_MIN_LENGTH normalized end |
.normalize_town_for_display(town_str) ⇒ String?
町域文字列から「通常住所に含まれない情報」を除いた表示用文字列を返す 除去対象: 「以下に掲載がない場合」、全角括弧()で囲まれた部分全体
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/jp_address_complement/normalizer.rb', line 52 def normalize_town_for_display(town_str) return nil if town_str.nil? s = town_str.to_s.strip return nil if s.empty? # 全角括弧()で囲まれた部分をすべて除去 s = s.gsub(/([^)]*)/, '') TOWN_DISPLAY_REMOVAL_STRINGS.each { |rem| s = s.gsub(rem, '') } s = s.strip s.empty? ? nil : s end |