Browse Source

fix: 臺to台 & add: method get_info_from_address & add: 新竹市/嘉義市

ANX 5 years ago
parent
commit
e0bd97a80d
5 changed files with 36 additions and 11 deletions
  1. 11 5
      db/areas.json
  2. 24 3
      lib/taiwan_city.rb
  3. 1 1
      lib/taiwan_city/version.rb
  4. 0 1
      spec/features/taiwan_city_spec.rb
  5. 0 1
      spec/lib/taiwan_city_spec.rb

+ 11 - 5
db/areas.json

@@ -1,16 +1,18 @@
 {
     "city": [
-        {"text": "北市","id": "01000"},
+        {"text": "北市","id": "01000"},
         {"text": "新北市","id": "02000"},
         {"text": "基隆市","id": "03000"},
         {"text": "桃園市","id": "04000"},
         {"text": "新竹縣","id": "05000"},
+        {"text": "新竹市","id": "23000"},
         {"text": "苗栗縣","id": "06000"},
         {"text": "台中市","id": "07000"},
         {"text": "彰化縣","id": "08000"},
         {"text": "南投縣","id": "09000"},
         {"text": "雲林縣","id": "10000"},
-        {"text": "嘉義","id": "11000"},
+        {"text": "嘉義縣","id": "11000"},
+        {"text": "嘉義市","id": "22000"},
         {"text": "台南市","id": "12000"},
         {"text": "高雄市","id": "13000"},
         {"text": "屏東縣","id": "14000"},
@@ -87,7 +89,6 @@
         {"text": "蘇澳鎮","id": "15270"},
         {"text": "南澳鄉","id": "15272"},
 
-        {"text": "新竹市","id": "05300"},
         {"text": "竹北市","id": "05302"},
         {"text": "湖口鄉","id": "05303"},
         {"text": "新豐鄉","id": "05304"},
@@ -102,6 +103,10 @@
         {"text": "北埔鄉","id": "05314"},
         {"text": "峨眉鄉","id": "05315"},
 
+        {"text": "東區","id": "233001"},
+        {"text": "北區","id": "233002"},
+        {"text": "香山區","id": "233003"},
+
         {"text": "中壢區","id": "04320"},
         {"text": "平鎮區","id": "04324"},
         {"text": "龍潭區","id": "04325"},
@@ -227,8 +232,9 @@
         {"text": "四湖鄉","id": "10654"},
         {"text": "元長鄉","id": "10655"},
 
-        {"text": "西區"    ,"id": "11600"},
-        {"text": "東區"    ,"id": "11600"},
+        {"text": "西區"    ,"id": "226001"},
+        {"text": "東區"    ,"id": "226002"},
+
         {"text": "番路鄉"  ,"id": "11602"},
         {"text": "梅山鄉"  ,"id": "11603"},
         {"text": "竹崎鄉"  ,"id": "11604"},

+ 24 - 3
lib/taiwan_city.rb

@@ -1,9 +1,8 @@
-# encoding: utf-8
 require "taiwan_city/engine"
 
 module TaiwanCity
   TAIWAN = '00000' # 全国
-  PATTERN = /(\d{2})(\d{3})/
+  PATTERN = /(\d{2})(\d{3})(\d?)/
 
   class << self
     def list(parent_id = '00000')
@@ -17,7 +16,7 @@ module TaiwanCity
       end
 
       #sort
-      result.sort! {|a, b| a[1] <=> b[1]}
+      # result.sort! {|a, b| a[1] <=> b[1]}
       result
     end
 
@@ -37,6 +36,28 @@ module TaiwanCity
       match(code)[1].ljust(5, '0')
     end
 
+    def get_info_from_address(address)
+      return '' if address.blank?
+      address.gsub!('臺', '台')
+      addr = {}
+      data.each do |city_code, city|
+        next if address.exclude?(city[:text])
+        addr[:city] = city[:text] 
+        addr[:city_code] = city_code
+        city[:children].each do |dist_code, dist| 
+          next if address.exclude?(dist[:text])
+          addr[:dist] = dist[:text]
+          addr[:dist_code] = dist_code
+          addr[:post_code] = dist_code[2..5]
+          break
+        end
+        break
+      end
+      addr[:address] = address.gsub(addr[:city], '').gsub(addr[:dist], '')
+      addr[:full_address] = address
+      addr
+    end
+
     private
     def data
       unless @list

+ 1 - 1
lib/taiwan_city/version.rb

@@ -1,3 +1,3 @@
 module TaiwanCity
-  VERSION = "0.0.4"
+  VERSION = "0.0.5"
 end

+ 0 - 1
spec/features/taiwan_city_spec.rb

@@ -1,4 +1,3 @@
-# encoding: utf-8
 require 'spec_helper'
 
 feature 'taiwan city', js: true do

+ 0 - 1
spec/lib/taiwan_city_spec.rb

@@ -1,4 +1,3 @@
-# encoding: utf-8
 require 'spec_helper'
 
 describe TaiwanCity do