envek@envek-work:~$ apt-cache search dbf pgdbf - converter of XBase / FoxPro tables to PostgreSQL dbf2mysql - xBase <--> MySQL
mv {PIndx08,post_indices}.dbf # , pgdbf -u post_indices.dbf | iconv -f CP866 > post_indices.sql #
rails g model PostIndex
class CreatePostIndices < ActiveRecord::Migration def change create_table :post_indices, id: false do |t| t.string :index, limit: 6 t.string :ops_name, limit: 60 t.string :ops_type, limit: 50 t.string :ops_subm, limit: 6 t.string :region, limit: 60 t.string :autonom, limit: 60 t.string :area, limit: 60 t.string :city, limit: 60 t.string :city_1, limit: 60 t.date :act_date t.string :index_old, limit: 6 t.index :index_old end reversible do |to| to.up do execute 'ALTER TABLE post_indices ADD PRIMARY KEY (index);' end end end end
class PostIndex < ActiveRecord::Base self.primary_key = 'index' end
# : rails generate controller PostIndices class PostIndicesController < ApplicationController def get @index = PostIndex.where(index: params[:index]).first @index = PostIndex.where(index_old: params[:index]).order(:index).first! unless @index respond_to do |format| format.json { render json: @index.to_json(only: [:index, :region, :area, :city]) } end end end
get '/post_index/:index(.:format)', controller: :post_indices, action: :get
<form id='address_form'> <table> <tr> <td><label for='address_postcode'> </label></td> <td> <input class='postcode_field' id='address_postcode' name='address[postcode]'> <p class='description'> , «», «» «» .</p> </td> </tr> <tr> <td><label for='address_region'>//</label></td> <td><input class='region_field' id='address_region' name='address[region]'></td> </tr> <tr> <td><label for='address_area'></label></td> <td><input class='area_field' id='address_area' name='address[area]'></td> </tr> <tr> <td><label for='address_city'>/</label></td> <td><input class='city_field' id='address_city' name='address[city]'></td> </tr> </table> </form>
jQuery(document).ready(function($){ $('.postcode_field').on('keyup change', function () { // var postcode_field = $(this); var form = postcode_field.parents("form"); var region_field = $('.region_field', form); var area_field = $('.area_field', form); var city_field = $('.city_field', form); // region_field.val(''); area_field.val(''); city_field.val(''); // - var postcode = this.value; if (postcode.length == 6) { jQuery.ajax({ dataType: "jsonp", url: 'http://postindexapi.ru/'+postcode+'.json?callback=?', beforeSend: function() { // , $("td:last-child p.description.notice, td:last-child p.description.alert", postcode_field.parents("tr")).remove(); $('<p class="description notice loading"></p>').text("…").appendTo($("td:last-child", postcode_field.parents("tr"))) }, success: function(data){ postcode_field.val(data.index); region_field.val(data.region); area_field.val(data.area); city_field.val(data.city); if (data.index != postcode) { var message = " : "+postcode+", : "+data.index; $('<p class="description notice"></p>').text(message).appendTo($("td:last-child", postcode_field.parents("tr"))) } }, error: function (jqxhr, status, e) { var message = ' !'+e; if (e == 'Not Found') message = ' '; if (status == 'timeout') message = ' . .'; $('<p class="description alert"></p>').text(message).appendTo($("td:last-child", postcode_field.parents("tr"))) console.debug(jqxhr, status, e); }, complete: function () { // $("td:last-child p.description.loading", postcode_field.parents("tr")).remove(); } }); } }); });
gem 'nokogiri'
, you can with require: false
) : require 'open-uri' require 'fileutils' require 'nokogiri' namespace :post_index do desc 'Update used post indices database to latest' task update: :environment do # Get info about post indices database url_prefix = 'http://info.russianpost.ru/database' doc = Nokogiri::HTML(open("#{url_prefix}/ops.html")) file = doc.at_css('a[name=newdbdata]+table tr:last-child td:nth-child(4) a').attr :href FileUtils.mkdir_p "#{Rails.root}/tmp/post_indices" dir = Pathname.new("#{Rails.root}/tmp/post_indices") filepath = Pathname.new("#{dir}/#{file}") filepath_success = Pathname.new("#{dir}/#{file}.success") if filepath.exist? and filepath_success.exist? puts 'Already up-to-date.' else # Download, unzip, rename and convert post indices file sh "wget #{url_prefix}/#{file} -O #{filepath}" sh "unzip -o #{filepath} -d #{dir}" dbf_filename = filepath.to_s.gsub /\.zip$/, '.DBF' sh "cp -f #{dbf_filename} #{dir}/post_indices.dbf" sh "pgdbf -u #{dir}/post_indices.dbf | iconv -f CP866 > #{dir}/post_indices.sql" # Import in database config = Abitur::Application.config.database_configuration[::Rails.env] dbh, dbu, dbp, db = config['host'], config['username'], config['password'], config['database'] sh "PGPASSWORD=#{dbp} psql -U #{dbu} -w -h #{dbh} #{db} < #{dir}/post_indices.sql" # Clean up FileUtils.rm [dbf_filename, "#{dir}/post_indices.dbf", "#{dir}/post_indices.sql"], force: true FileUtils.touch filepath_success end end end
Source: https://habr.com/ru/post/190122/
All Articles