soaring_china_city 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +31 -0
  4. data/app/assets/javascripts/china_city/application.js.coffee +3 -0
  5. data/app/assets/javascripts/china_city/jquery.china_city.js.coffee +18 -0
  6. data/app/assets/stylesheets/china_city/application.css +13 -0
  7. data/app/controllers/china_city/application_controller.rb +4 -0
  8. data/app/controllers/china_city/data_controller.rb +13 -0
  9. data/app/helpers/china_city/application_helper.rb +4 -0
  10. data/app/views/china_city/data/index.html.erb +37 -0
  11. data/app/views/layouts/china_city/application.html.erb +12 -0
  12. data/config/routes.rb +4 -0
  13. data/db/areas.json +191326 -0
  14. data/db/district_gb2260_taobao.yml +17 -0
  15. data/lib/china_city.rb +124 -0
  16. data/lib/china_city/engine.rb +13 -0
  17. data/lib/china_city/version.rb +3 -0
  18. data/lib/china_city_tasks.rake +137 -0
  19. data/spec/controllers/china_city/data_controller_spec.rb +12 -0
  20. data/spec/dummy/README.rdoc +28 -0
  21. data/spec/dummy/Rakefile +6 -0
  22. data/spec/dummy/app/assets/config/manifest.js +5 -0
  23. data/spec/dummy/app/assets/javascripts/application.js +14 -0
  24. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  25. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  26. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  27. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  28. data/spec/dummy/bin/bundle +3 -0
  29. data/spec/dummy/bin/rails +4 -0
  30. data/spec/dummy/bin/rake +4 -0
  31. data/spec/dummy/config.ru +4 -0
  32. data/spec/dummy/config/application.rb +35 -0
  33. data/spec/dummy/config/boot.rb +5 -0
  34. data/spec/dummy/config/database.yml +25 -0
  35. data/spec/dummy/config/environment.rb +5 -0
  36. data/spec/dummy/config/environments/development.rb +29 -0
  37. data/spec/dummy/config/environments/production.rb +80 -0
  38. data/spec/dummy/config/environments/test.rb +36 -0
  39. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  40. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  41. data/spec/dummy/config/initializers/inflections.rb +16 -0
  42. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  43. data/spec/dummy/config/initializers/secret_token.rb +13 -0
  44. data/spec/dummy/config/initializers/session_store.rb +3 -0
  45. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  46. data/spec/dummy/config/locales/en.yml +23 -0
  47. data/spec/dummy/config/routes.rb +4 -0
  48. data/spec/dummy/public/404.html +58 -0
  49. data/spec/dummy/public/422.html +58 -0
  50. data/spec/dummy/public/500.html +57 -0
  51. data/spec/dummy/public/favicon.ico +0 -0
  52. data/spec/dummy/script/rails +6 -0
  53. data/spec/features/china_city_spec.rb +69 -0
  54. data/spec/lib/china_city_spec.rb +64 -0
  55. data/spec/spec_helper.rb +29 -0
  56. metadata +288 -0
@@ -0,0 +1,17 @@
1
+ ---
2
+ '130421': '130421'
3
+ '139001': '130682'
4
+ '139002': '130181'
5
+ '140211': '140211'
6
+ '230883': '230833'
7
+ '231086': '231024'
8
+ '330204': '330204'
9
+ '340522': '341423'
10
+ '340722': '340823'
11
+ '350527': '350527'
12
+ '360482': '360483'
13
+ '410211': '410211'
14
+ '500110': '500222'
15
+ '500111': '500225'
16
+ '510185': '512081'
17
+ '542431': '542432'
data/lib/china_city.rb ADDED
@@ -0,0 +1,124 @@
1
+ # encoding: utf-8
2
+ require "china_city/engine"
3
+
4
+ module ChinaCity
5
+ CHINA = '000000' # 全国
6
+ PATTERN = /(\d{2})(\d{2})(\d{2})/
7
+
8
+ class << self
9
+ # @options[:show_all] 是否显示港澳台这三处敏感地区
10
+ def list(parent_id='000000', options = {})
11
+ # parent_id ||= '000000'
12
+ show_all = options[:show_all] || false
13
+
14
+ result = []
15
+ return result if parent_id.blank?
16
+ province_id = province(parent_id)
17
+ city_id = city(parent_id)
18
+ district_id = district(parent_id)
19
+ children = data
20
+ children = children[province_id][:children] if children.has_key?(province_id)
21
+ children = children[city_id][:children] if children.has_key?(city_id)
22
+ children = children[district_id][:children] if children.has_key?(district_id)
23
+ children.each_key do |id|
24
+ next if (!show_all && children[id][:sensitive_areas])
25
+ result.push [children[id][:text], id]
26
+ end
27
+
28
+ #sort
29
+ result.sort! {|a, b| a[1] <=> b[1]}
30
+ result
31
+ end
32
+
33
+ # @options[:prepend_parent] 是否显示上级区域
34
+ def get(id, options = {})
35
+ return '' if id.blank?
36
+ prepend_parent = options[:prepend_parent] || false
37
+ children = data
38
+ return children[id][:text] if children.has_key?(id)
39
+ province_id = province(id)
40
+ province_text = children[province_id][:text]
41
+ children = children[province_id][:children]
42
+ return "#{prepend_parent ? province_text : ''}#{children[id][:text]}" if children.has_key?(id)
43
+ city_id = city(id)
44
+ city_text = children[city_id][:text]
45
+ children = children[city_id][:children]
46
+ return "#{prepend_parent ? (province_text + city_text) : ''}#{children[id][:text]}" if children.has_key?(id)
47
+ district_id = district(id)
48
+ district_text = children[district_id][:text]
49
+ children = children[district_id][:children]
50
+ return "#{prepend_parent ? (province_text + city_text + district_text) : ''}#{children[id][:text]}"
51
+ end
52
+
53
+ def province(code)
54
+ match(code)[1].ljust(6, '0')
55
+ end
56
+
57
+ def city(code)
58
+ id_match = match(code)
59
+ "#{id_match[1]}#{id_match[2]}".ljust(6, '0')
60
+ end
61
+
62
+ def district(code)
63
+ code[0..5].rjust(6,'0')
64
+ end
65
+
66
+ def data
67
+ unless @list
68
+ #{ '440000' =>
69
+ # {
70
+ # :text => '广东',
71
+ # :children =>
72
+ # {
73
+ # '440300' =>
74
+ # {
75
+ # :text => '深圳',
76
+ # :children =>
77
+ # {
78
+ # '440305' => { :text => '南山' }
79
+ # }
80
+ # }
81
+ # }
82
+ # }
83
+ # }
84
+ @list = {}
85
+ #@see: https://github.com/cn/GB2260
86
+ json = JSON.parse(File.read("#{Engine.root}/db/areas.json"))
87
+ streets = json.values.flatten
88
+ streets.each do |street|
89
+ id = street['id']
90
+ text = street['text']
91
+ sensitive_areas = street['sensitive_areas'] || false
92
+ if id.size == 6 # 省市区
93
+ if id.end_with?('0000') # 省
94
+ @list[id] = {:text => text, :sensitive_areas => sensitive_areas, :children => {}}
95
+ elsif id.end_with?('00') # 市
96
+ province_id = province(id)
97
+ @list[province_id] = {:text => nil, :children => {}} unless @list.has_key?(province_id)
98
+ @list[province_id][:children][id] = {:text => text, :sensitive_areas => sensitive_areas, :children => {}}
99
+ else
100
+ province_id = province(id)
101
+ city_id = city(id)
102
+ @list[province_id] = {:text => text, :sensitive_areas => sensitive_areas, :children => {}} unless @list.has_key?(province_id)
103
+ @list[province_id][:children][city_id] = {:text => text, :sensitive_areas => sensitive_areas, :children => {}} unless @list[province_id][:children].has_key?(city_id)
104
+ @list[province_id][:children][city_id][:children][id] = {:text => text, :sensitive_areas => sensitive_areas, :children => {}}
105
+ end
106
+ else # 街道
107
+ province_id = province(id)
108
+ city_id = city(id)
109
+ district_id = district(id)
110
+ @list[province_id] = {:text => text, :sensitive_areas => sensitive_areas, :children => {}} unless @list.has_key?(province_id)
111
+ @list[province_id][:children][city_id] = {:text => text, :sensitive_areas => sensitive_areas, :children => {}} unless @list[province_id][:children].has_key?(city_id)
112
+ @list[province_id][:children][city_id][:children][district_id] = {:text => text, :sensitive_areas => sensitive_areas, :children => {}} unless @list[province_id][:children][city_id][:children].has_key?(district_id)
113
+ @list[province_id][:children][city_id][:children][district_id][:children][id] = {:text => text, :sensitive_areas => sensitive_areas}
114
+ end
115
+ end
116
+ end
117
+ @list
118
+ end
119
+
120
+ def match(code)
121
+ code.match(PATTERN)
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,13 @@
1
+ module ChinaCity
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace ChinaCity
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec, fixture: false
7
+ # g.fixture_replacement :factory_girl, dir: 'spec/factories'
8
+ g.assets false
9
+ g.helper false
10
+ g.view_specs false
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module ChinaCity
2
+ VERSION = "0.0.6"
3
+ end
@@ -0,0 +1,137 @@
1
+ require 'GB2260'
2
+ require 'json'
3
+
4
+ namespace :gem do
5
+
6
+ desc '更新 areas.json 数据'
7
+ task :update_data do
8
+ # 1. 获取淘宝所有的街道
9
+ gb2260 = GB2260.new
10
+
11
+ provinces = []
12
+ cities = []
13
+ districts = []
14
+ streets = []
15
+
16
+ gb2260.provinces.each do |province| # 省
17
+ gb_cities = gb2260.prefectures(province.code)
18
+ gb_cities.each do |city| # 市
19
+ cities << { text:city.name, id: city.code }
20
+ gb_districts = gb2260.counties(city.code)
21
+ gb_districts << GB2260::Division.new("#{city.code[0,4]}99", city.name) if gb_districts.empty? # 中山市等没有县级,需要自动补上
22
+ gb_districts.each do |district| # 区
23
+ districts << { text: district.name, id: district.code }
24
+ end
25
+ end
26
+ provinces << { text:province.name, id: province.code } unless gb_cities.empty? # 台湾省、香港、澳门没有市
27
+ # provinces << { text:province.name, id: province.code } #unless gb_cities.empty? # 显示台湾省、香港、澳门
28
+ end
29
+
30
+ map_codes = YAML.load_file("db/district_gb2260_taobao.yml")
31
+ districts.each do |district|
32
+ code = district[:id]
33
+ result = get_remote_streets(map_codes[code] || code)
34
+ result = [{text: "全境", id: "#{code}001"}] unless result
35
+ result.each do |tstreet| # 街道
36
+ street_code = "#{code}#{tstreet[:id][-3,3]}" # 保证编码与区一致
37
+ streets << {text: tstreet[:text], id: street_code}
38
+ end
39
+ end
40
+
41
+ content = JSON.pretty_generate(province: provinces, city: cities, district: districts, street: streets)
42
+
43
+ path = File.join("db/areas.json")
44
+ File.write(path, content)
45
+ end
46
+
47
+ desc "显示国标与淘宝区划代码不匹配的记录."
48
+ task :list_mismatch_districts do
49
+ gb2260 = GB2260.new
50
+ map_codes = YAML.load_file("db/district_gb2260_taobao.yml")
51
+ map_codes.each do |key, value|
52
+ division = gb2260.get(key)
53
+ puts [division.province, division.prefecture, division.county].compact.map(&:name).join('/')
54
+ end
55
+ end
56
+
57
+ desc "获取淘宝街道信息."
58
+ task :list_streets, [:code] do |t, args|
59
+ code = args.code
60
+ puts get_streets code
61
+ end
62
+
63
+
64
+ desc '处理淘宝区代码与国标不一致的情况,将对应关系保存到 db/district_gb2260_taobao'
65
+ task :save_district_gb2260_taobao_map do
66
+ gb2260 = GB2260.new
67
+ districts = []
68
+ empty_districts = []
69
+
70
+ # 1. 汇总所有区记录
71
+ gb2260.provinces.each do |province|
72
+ gb2260.prefectures(province.code).each do |city|
73
+ data = gb2260.counties(city.code)
74
+ GB2260::Division.new("#{city.code[0,4]}99", city.name) if data.empty? # 中山市没有县级,需要自动补上
75
+ data.each do |district|
76
+ districts << district
77
+ end
78
+ end
79
+ end
80
+
81
+ # 2. 获取街道记录
82
+ # districts.select{|district| district.code.start_with?('1301')}.each do |district|
83
+ districts.each do |district|
84
+ result = get_remote_streets(district.code)
85
+ empty_districts << district unless result
86
+ end
87
+
88
+ # 3. 获取淘宝省市区记录
89
+ response = HTTParty.get("https://g.alicdn.com/kg/??address/6.0.4/index-min.js?t=1449112049369.js")
90
+ tdata = response.to_s.sub(/.+return\st=e\}\(\),r=function\(t\)\{var\se=/, '')
91
+ tdata = tdata.sub(/;\nreturn\st=e\}\(\),c=function\(t\).+/m, '')
92
+ cities = JSON.parse(tdata)
93
+ result = {}
94
+ cities.each do |city|
95
+ result[city[1][0]] = city[0]
96
+ end
97
+
98
+ # 4. 输出结果
99
+ map_data = {}
100
+ empty_districts.each do |district|
101
+ code = district.code
102
+ text = district.name
103
+ next if %w(市辖区 城区 郊区).include?(text)
104
+ text = text.gsub(/(自治县|市|区|县)$/,'')
105
+ text = text.gsub(/(黎族苗族|黎族苗族|侗族|土家族苗族|彝族回族苗族|土家族|苗族|回族土族|土族|回族|撒拉族)$/,'')
106
+ text = '和县' if text == '和'
107
+ if tid = result[text]
108
+ map_data[code] = tid
109
+ else
110
+ puts text
111
+ end
112
+ end
113
+
114
+ path = "db/district_gb2260_taobao.yml"
115
+ File.write(path, map_data.to_yaml)
116
+ end
117
+
118
+ private
119
+ def get_streets(code)
120
+ map_codes = YAML.load_file("db/district_gb2260_taobao.yml")
121
+ get_remote_streets(map_codes[code] || code)
122
+ end
123
+
124
+ def get_remote_streets(code)
125
+ l1 = code[0..1].ljust(6,'0')
126
+ l2 = code[0..3].ljust(6,'0')
127
+ l3 = code
128
+ l3 = l2 if l3.end_with?('99') # 特殊处理:县级如果以99结尾,则需要取市级
129
+ puts "fetching: #{l3}"
130
+ response = HTTParty.get("https://lsp.wuliu.taobao.com/locationservice/addr/output_address_town_array.do?l1=#{l1}&l2=#{l2}&l3=#{l3}&lang=zh-S&_ksTS=1440400908112_7583")
131
+ if response.size > 40
132
+ ret = response[33..-5].split(',').map{|i| i.gsub(/[\[|\]|\"|\']+/,'')}.each_slice(4).to_a
133
+ ret.map{|i| {text:i[1], id:i[0]}}
134
+ end
135
+ end
136
+
137
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChinaCity::DataController do
4
+
5
+ describe "GET 'show'" do
6
+ it "returns http success" do
7
+ get 'show', id: '000000', use_route: 'china_city'
8
+ response.should be_success
9
+ end
10
+ end
11
+
12
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
@@ -0,0 +1,5 @@
1
+ //= link_tree ../images
2
+ //= link_directory ../stylesheets .css
3
+ //= link_directory ../javascripts .js
4
+ //= link china_city/application.css
5
+ //= link china_city/application.js
@@ -0,0 +1,14 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //= require 'jquery'
13
+ //= require 'china_city/jquery.china_city'
14
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,35 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ # Pick the frameworks you want:
4
+ # require "active_record/railtie"
5
+ require "action_controller/railtie"
6
+ # require "action_mailer/railtie"
7
+ require "sprockets/railtie"
8
+ # require "rails/test_unit/railtie"
9
+
10
+ Bundler.require(*Rails.groups)
11
+ require "china_city"
12
+
13
+ module Dummy
14
+ class Application < Rails::Application
15
+ # Settings in config/environments/* take precedence over those specified here.
16
+ # Application configuration should go into files in config/initializers
17
+ # -- all .rb files in that directory are automatically loaded.
18
+
19
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
20
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
21
+ # config.time_zone = 'Central Time (US & Canada)'
22
+
23
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
24
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
25
+ # config.i18n.default_locale = :de
26
+
27
+ # Rails 3 config
28
+ # Enable the asset pipeline
29
+ config.assets.enabled = true
30
+
31
+ # Version of your assets, change this if you want to expire all your assets
32
+ config.assets.version = '1.0'
33
+ end
34
+ end
35
+