Class: FiasParser::Parser
- Inherits:
-
Object
- Object
- FiasParser::Parser
- Defined in:
- lib/fias_parser.rb
Instance Attribute Summary collapse
-
#date ⇒ Object
Returns the value of attribute date.
Instance Method Summary collapse
- #archive_file_path ⇒ Object
- #archive_path ⇒ Object
- #check_executables ⇒ Object
- #download_latest ⇒ Object
- #get_archive_file_path ⇒ Object
- #get_archive_path ⇒ Object
- #get_latest ⇒ Object
-
#initialize(options = {}) ⇒ Parser
constructor
A new instance of Parser.
- #process(term, options = {}, &block) ⇒ Object
- #unpack ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Parser
Returns a new instance of Parser.
53 54 55 |
# File 'lib/fias_parser.rb', line 53 def initialize( = {} ) @base_dir = [:base_dir] || Dir.tmpdir end |
Instance Attribute Details
#date ⇒ Object
Returns the value of attribute date.
51 52 53 |
# File 'lib/fias_parser.rb', line 51 def date @date end |
Instance Method Details
#archive_file_path ⇒ Object
156 157 158 |
# File 'lib/fias_parser.rb', line 156 def archive_file_path @archive_file_path ||= get_archive_file_path end |
#archive_path ⇒ Object
148 149 150 |
# File 'lib/fias_parser.rb', line 148 def archive_path @archive_path ||= get_archive_path end |
#check_executables ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/fias_parser.rb', line 78 def check_executables line = Cocaine::CommandLine.new( "wget", "-h" ) begin line.run rescue Cocaine::CommandNotFoundError => e puts 'wget is not installed. Run "sudo apt-get install wget"' return false end line = Cocaine::CommandLine.new( "unar", "-h" ) begin line.run rescue Cocaine::CommandNotFoundError => e puts 'unar is not installed. Run "sudo apt-get install unar"' return false end true end |
#download_latest ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/fias_parser.rb', line 102 def download_latest agent = Mechanize.new agent.user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1' url = 'http://fias.nalog.ru/Public/DownloadPage.aspx' doc = agent.get( url ).root post_data = {} doc.css( 'form input[type="hidden"]' ).each do |input| post_data[ input['name'] ] = input['value'] end @date = doc.css( '#ctl00_contentPlaceHolder_downloadRadGrid_ctl00__0 tr td:first-child' ).text.gsub( /[^\d]/, '' ) post_data['__EVENTTARGET'] = 'ctl00$contentPlaceHolder$downloadRadGrid$ctl00$ctl04$fullSZLinkButton' if File.exists?( self.archive_file_path ) puts "File '#{self.archive_file_path}' already exists." return end line = Cocaine::CommandLine.new( "wget", "--output-document=:out --post-data=:post_data :url" ) line.run( { out: self.archive_file_path, post_data: Mechanize::Util.build_query_string( post_data ), url: url, } ) end |
#get_archive_file_path ⇒ Object
160 161 162 |
# File 'lib/fias_parser.rb', line 160 def get_archive_file_path File.join( @base_dir, "#{@date}.rar" ) end |
#get_archive_path ⇒ Object
152 153 154 |
# File 'lib/fias_parser.rb', line 152 def get_archive_path File.join( @base_dir, @date ) end |
#get_latest ⇒ Object
57 58 59 60 61 62 |
# File 'lib/fias_parser.rb', line 57 def get_latest return unless self.check_executables self.download_latest self.unpack end |
#process(term, options = {}, &block) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/fias_parser.rb', line 64 def process( term, = {}, &block ) file_name = Dir.entries( self.archive_path ).find { |f| f =~ /#{term}/i } if file_name.nil? puts "File /#{term}/ not found in '#{self.archive_path}'" return end File.open( File.join( self.archive_path, file_name ) ) do |file| ::Ox.sax_parse( Document.new( , block ), file ) end end |
#unpack ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/fias_parser.rb', line 135 def unpack dir = File.join( @base_dir ) FileUtils.mkdir_p ( dir ) unless File.exists?( dir ) line = Cocaine::CommandLine.new( "unar", "-o :dir :archive" ) line.run( { dir: dir, archive: self.archive_file_path, } ) end |