Module: DistrictHelper
- Included in:
- ChinaDistrictCode
- Defined in:
- lib/china_district_code/helpers/district_helper.rb
Instance Attribute Summary collapse
-
#areas ⇒ Object
Returns the value of attribute areas.
-
#citys ⇒ Object
Returns the value of attribute citys.
-
#provinces ⇒ Object
Returns the value of attribute provinces.
Class Method Summary collapse
-
.find_areas_by_city(name) ⇒ Object
find the area all the city bwlow.
-
.find_citys_by_province(name) ⇒ Object
find the province all the city below.
-
.find_province_by_city(name) ⇒ Object
find province by city.
-
.find_province_city_by_area(name) ⇒ Object
find province,city by area.
-
.get_china_district_code ⇒ Object
get data from ‘www.stats.gov.cn/tjsj/tjbz/xzqhdm/201401/t20140116_501070.html’.
- .load_all_csv_data ⇒ Object
-
.scan_area(html) ⇒ Object
find the area and formatting.
-
.scan_city(html) ⇒ Object
find the city and formatting.
-
.scan_province(html) ⇒ Object
find the province and formatting.
Instance Attribute Details
#areas ⇒ Object
Returns the value of attribute areas.
4 5 6 |
# File 'lib/china_district_code/helpers/district_helper.rb', line 4 def areas @areas end |
#citys ⇒ Object
Returns the value of attribute citys.
4 5 6 |
# File 'lib/china_district_code/helpers/district_helper.rb', line 4 def citys @citys end |
#provinces ⇒ Object
Returns the value of attribute provinces.
4 5 6 |
# File 'lib/china_district_code/helpers/district_helper.rb', line 4 def provinces @provinces end |
Class Method Details
.find_areas_by_city(name) ⇒ Object
find the area all the city bwlow
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/china_district_code/helpers/district_helper.rb', line 98 def self.find_areas_by_city(name) load_all_csv_data city = [] areas = [] @citys.each do |ci| if ci[3].include?(name) city = ci break end end @areas.each do |ar| if ar[0].eql?(city[0]) and ar[1].eql?(city[1]) areas.push [ar[3],ar[4]] end end areas end |
.find_citys_by_province(name) ⇒ Object
find the province all the city below
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/china_district_code/helpers/district_helper.rb', line 73 def self.find_citys_by_province(name) load_all_csv_data province = [] citys = [] @provinces.each do |pro| if pro[2].include? name province = pro break end end # LOGGER.debug province # LOGGER.debug province[0] if province @citys.each do |ci| LOGGER.debug ci[0] if ci[0].eql?(province[0]) # LOGGER.debug ci citys.push [ci[2],ci[3]] end end end citys end |
.find_province_by_city(name) ⇒ Object
find province by city
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/china_district_code/helpers/district_helper.rb', line 117 def self.find_province_by_city(name) load_all_csv_data city = [] province = [] @citys.each do |ci| if ci[3].include?(name) city = ci break end end @provinces.each do |pro| if pro[0].eql?(city[0]) province = [pro[1],pro[2]] break end end province end |
.find_province_city_by_area(name) ⇒ Object
find province,city by area
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/china_district_code/helpers/district_helper.rb', line 137 def self.find_province_city_by_area(name) load_all_csv_data area = [] city = [] province = [] @areas.each do |ar| if ar[4].include?(name) area = ar break end end @provinces.each do |pro| if pro[0].eql?(area[0]) province = [pro[1],pro[2]] break end end @citys.each do |ci| if ci[0].eql?(area[0]) and ci[1].eql?(area[1]) city = [ci[2],ci[3]] break end end [province,city] end |
.get_china_district_code ⇒ Object
get data from ‘www.stats.gov.cn/tjsj/tjbz/xzqhdm/201401/t20140116_501070.html’
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/china_district_code/helpers/district_helper.rb', line 7 def self.get_china_district_code code_html = HTTP.get('http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201401/t20140116_501070.html').to_s.force_encoding('UTF-8') provinces = scan_province code_html citys = scan_city code_html areas = scan_area code_html # LOGGER.debug citys #first of all,write provinces to csv CsvHelper::data_to_csv 'province',provinces CsvHelper::data_to_csv 'city',citys CsvHelper::data_to_csv 'area',areas end |
.load_all_csv_data ⇒ Object
67 68 69 70 71 |
# File 'lib/china_district_code/helpers/district_helper.rb', line 67 def self.load_all_csv_data @provinces = CsvHelper::data_from_csv 'province' @citys = CsvHelper::data_from_csv 'city' @areas = CsvHelper::data_from_csv 'area' end |
.scan_area(html) ⇒ Object
find the area and formatting
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/china_district_code/helpers/district_helper.rb', line 51 def self.scan_area(html) areas = html.scan /\d{6} [\u4e00-\u9fa5]+/ result = [] areas.each do |e| tmp = e.split(/ /) name = tmp[1] next if name.eql?('市辖区') code = tmp[0] pro_prefix = code[0,2] city_prefix = code[2,2] area_prefix = code[4,2] result.push [pro_prefix,city_prefix,area_prefix,code,name] end result end |
.scan_city(html) ⇒ Object
find the city and formatting
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/china_district_code/helpers/district_helper.rb', line 35 def self.scan_city(html) citys = html.scan /\d{6} [\u4e00-\u9fa5]+/ result = [] citys.each do |e| tmp = e.split(/ /) name = tmp[1] next if name.eql?('市辖区') or name.eql?('县') code = tmp[0] pro_prefix = code[0,2] city_prefix = code[2,2] result.push [pro_prefix,city_prefix,code,name] end result end |
.scan_province(html) ⇒ Object
find the province and formatting
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/china_district_code/helpers/district_helper.rb', line 21 def self.scan_province(html) provinces = html.scan /\d{6} [\u4e00-\u9fa5]+/ result = [] provinces.each do |e| tmp = e.split(/ /) code = tmp[0] name = tmp[1] pro_prefix = code[0,2] result.push [pro_prefix,code,name] end result end |