Skip to content

Commit 02b8ed0

Browse files
committed
feat: taiwan
1 parent 670f0b2 commit 02b8ed0

File tree

5 files changed

+120
-3
lines changed

5 files changed

+120
-3
lines changed

app/importers/base_importer.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ def fetch
44
end
55

66
def import!
7-
filename = Rails.root.join('db', 'countries', self.class.to_s.underscore.gsub('_importer', '') + '.json')
87
IO.write(filename, fetch.to_json)
98
end
109

10+
def read
11+
IO.read(filename)
12+
end
13+
1114
private
1215

16+
def filename
17+
Rails.root.join('db', 'countries', self.class.to_s.underscore.gsub('_importer', '') + '.json')
18+
end
19+
1320
def request_get(url)
1421
response = Net::HTTP.get_response(URI.parse(url))
1522
response.body

app/importers/taiwan_importer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def array_cities
4949

5050
def parse_dist_type_and_zip_code(city_name, dist_name)
5151
res = content.scan(/<tr align="center">.+?<small>#{city_name}<\/small>.+?>#{dist_name}<.+?title="[^"]+">([^<]+)<\/a>.+?<td>([0-9]+)<\/td>[\n\r]+<\/tr>/m).first
52-
{ type_name: res[0], zip_code: res[1] }
52+
{ type_name: res[0], zipcode: res[1] }
5353
end
5454

5555
def parse_city_type_name(city_name)
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var loadjQuery = function(callback){
2+
url = '//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'
3+
var script = document.createElement("script")
4+
script.type = "text/javascript";
5+
if (script.readyState){ //IE
6+
script.onreadystatechange = function(){
7+
if (script.readyState == "loaded" ||
8+
script.readyState == "complete"){
9+
script.onreadystatechange = null;
10+
callback();
11+
}
12+
};
13+
} else { //Others
14+
script.onload = function(){
15+
callback();
16+
};
17+
}
18+
script.src = url;
19+
document.getElementsByTagName("head")[0].appendChild(script);
20+
}
21+
if(typeof(jQuery) == 'undefined') {
22+
console.log('loading jquery')
23+
loadjQuery(main);
24+
} else {
25+
main();
26+
}

app/views/templates/selects.js.erb

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
var data = <%== @data %>;
2+
var random_id = (Date.now() + Math.random()).toString().replace('.', '-')
3+
document.write('<div class="gaia-selects" id="gaia-selects-' + random_id + '"></div>');
4+
var main = function() {
5+
var selects = $('#gaia-selects-' + random_id);
6+
var city = $('<select>').attr('id', 'gaia-city-select-' + random_id)
7+
var dist = $('<select>').attr('id', 'gaia-dist-select-' + random_id)
8+
var zipcode = $('<input type="text">').attr('id', 'gaia-zipcode-' + random_id)
9+
10+
var script = document.currentScript || selects.parent().find('script')[0];
11+
var params = {
12+
city_input: script.getAttribute('city-input') || 'city',
13+
dist_input: script.getAttribute('dist-input') || 'dist',
14+
zipcode_input: script.getAttribute('zipcode-input' || 'zipcode'),
15+
zipcode_value: script.getAttribute('zipcode-value' || ''),
16+
city_value: script.getAttribute('city-value' || ''),
17+
dist_value: script.getAttribute('dist-value' || ''),
18+
zipcode_prefix: script.getAttribute('zipcode-prefix'),
19+
disable_zipcode: script.getAttribute('disable-zipcode')
20+
}
21+
city.attr('name', params['city_input']);
22+
dist.attr('name', params['dist_input']);
23+
24+
zipcode.attr('name', params['zipcode_input']);
25+
zipcode.val(params['zipcode_value']);
26+
zipcode.attr('placeholder', '郵遞區號')
27+
28+
var selected_city = params['city_value'];
29+
var selected_dist = params['dist_value'];
30+
var dist_name_with_zipcode = !(params['zipcode_prefix'] == 'false');
31+
var disable_zipcode_input = params['disable_zipcode'] == 'true'
32+
33+
// bind events
34+
city.on('change', function() {
35+
dist.trigger('reset');
36+
if(city.val() == '') return;
37+
var cell = data.filter(function(cell) { return cell.city.name == city.val(); })[0];
38+
cell.dists.forEach(function(dist_obj) {
39+
var text = dist_name_with_zipcode ? dist_obj.zipcode + ' ' + dist_obj.name : dist_obj.name;
40+
dist.append($('<option>', {
41+
'value': dist_obj.name,
42+
'text': text
43+
}).attr('data-zipcode', dist_obj.zipcode));
44+
})
45+
});
46+
47+
dist.on('reset', function() {
48+
dist.html('<option>請選擇鄉鎮區</option>')
49+
});
50+
51+
dist.on('change', function() {
52+
if(disable_zipcode_input || dist.val() == '') return;
53+
var zipcode_value = dist.find(':selected').attr('data-zipcode');
54+
zipcode.val(zipcode_value);
55+
});
56+
57+
// insert city select
58+
city.append('<option>請選擇城市</option>')
59+
dist.trigger('reset');
60+
data.forEach(function(cell) {
61+
var city_name = cell.city.name;
62+
var selected = city_name == selected_city;
63+
city.append($('<option>', {
64+
'value': city_name,
65+
'text': city_name,
66+
'selected': selected
67+
}));
68+
if(selected) {
69+
city.trigger('change');
70+
if(selected_dist != '') {
71+
dist.find('[value=' + selected_dist + ']').attr('selected', true);
72+
dist.trigger('change');
73+
}
74+
}
75+
});
76+
77+
selects.append($('<div>').addClass('gaia-cities').append(city));
78+
selects.append($('<div>').addClass('gaia-dists').append(dist));
79+
if(!disable_zipcode_input) {
80+
selects.append($('<div>').addClass('gaia-zipcode').append(zipcode));
81+
}
82+
$('#gaia-selects-' + random_id).parent().trigger('gaia-loaded')
83+
}
84+
<%= render partial: 'templates/load_jquery' %>

0 commit comments

Comments
 (0)