taiwan_city.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. require "taiwan_city/engine"
  2. module TaiwanCity
  3. TAIWAN = '00000' # 全国
  4. PATTERN = /(\d{2})(\d{3})(\d?)/
  5. class << self
  6. def list(parent_id = '00000')
  7. result = []
  8. return result if parent_id.blank?
  9. city_id = city(parent_id)
  10. children = data
  11. children = children[city_id][:children] if children.has_key?(city_id)
  12. children.each_key do |id|
  13. result.push [ children[id][:text], id]
  14. end
  15. #sort
  16. # result.sort! {|a, b| a[1] <=> b[1]}
  17. result
  18. end
  19. # @options[:prepend_parent] 是否显示上级区域
  20. def get(id, options = {})
  21. return '' if id.blank?
  22. prepend_parent = options[:prepend_parent] || false
  23. children = data
  24. return children[id][:text] if children.has_key?(id)
  25. city_id = city(id)
  26. city_text = children[city_id][:text]
  27. children = children[city_id][:children]
  28. return "#{prepend_parent ? (city_text) : ''}#{children[id][:text]}"
  29. end
  30. def city(code)
  31. match(code)[1].ljust(5, '0')
  32. end
  33. def get_info_from_address(address)
  34. return '' if address.blank?
  35. address.gsub!('臺', '台')
  36. addr = {
  37. city_code:'', city: '',
  38. dist_code:'', dist: '',
  39. post_code:'', address: '',
  40. full_address: ''
  41. }
  42. data.each do |city_code, city|
  43. next if address.exclude?(city[:text])
  44. addr[:city] = city[:text]
  45. addr[:city_code] = city_code
  46. city[:children].each do |dist_code, dist|
  47. next if address.exclude?(dist[:text])
  48. addr[:dist] = dist[:text]
  49. addr[:dist_code] = dist_code
  50. addr[:post_code] = dist_code[2..4]
  51. break
  52. end
  53. break
  54. end
  55. dist_position = address.index(addr[:dist]) ? address.index(addr[:dist]) + addr[:dist].size : nil
  56. city_position = address.index(addr[:city]) ? address.index(addr[:city]) + addr[:city].size : nil
  57. del_position = dist_position || city_position || 0
  58. addr[:address] = address[del_position..-1]
  59. addr[:full_address] = address
  60. addr
  61. end
  62. private
  63. def data
  64. unless @list
  65. #{ '440000' =>
  66. # {
  67. # :text => '广东',
  68. # :children =>
  69. # {
  70. # '440300' =>
  71. # {
  72. # :text => '深圳',
  73. # :children =>
  74. # {
  75. # '440305' => { :text => '南山' }
  76. # }
  77. # }
  78. # }
  79. # }
  80. # }
  81. @list = {}
  82. #@see: http://github.com/RobinQu/LocationSelect-Plugin/raw/master/areas_1.0.json
  83. json = JSON.parse(File.read("#{Engine.root}/db/areas.json"))
  84. districts = json.values.flatten
  85. districts.each do |district|
  86. id = district['id']
  87. text = district['text']
  88. if id.end_with?('000')
  89. @list[id] = {:text => text, :children => {}}
  90. else
  91. city_id = city(id)
  92. @list[city_id] = {:text => text, :children => {}} unless @list.has_key?(city_id)
  93. @list[city_id][:children][id] = {:text => text}
  94. end
  95. end
  96. end
  97. @list
  98. end
  99. def match(code)
  100. code.match(PATTERN)
  101. end
  102. end
  103. end