Created
May 11, 2012 16:13
-
-
Save TMaYaD/2660704 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 def parse_and_update(file_name)$ | |
102 Rails.logger.info "============================== Parsing: #{file_name} Begin =============================="$ | |
103 piper, pipew = IO.pipe$ | |
104 Thread.new do$ | |
105 begin$ | |
106 @remote_archive.get(file_name) do |chunk|$ | |
107 puts chunk$ | |
108 pipew << chunk$ | |
109 end$ | |
110 rescue Exception => e$ | |
111 Rails.logger.error "fetching #{file_name} raised #{e.message}"$ | |
112 e.backtrace.each { |l| Rails.logger.error l }$ | |
113 ensure$ | |
114 pipew.close$ | |
115 end$ | |
116 end$ | |
117 begin$ | |
118 parse_and_update_record_from(file_name, piper)$ | |
119 rescue Exception => e$ | |
120 Rails.logger.error "parsing #{file_name} raised #{e.message}"$ | |
121 e.backtrace.each { |l| Rails.logger.error l }$ | |
122 ensure$ | |
123 piper.close$ | |
124 end$ | |
125 Rails.logger.info "============================== Parsing: #{file_name} End =============================="$ | |
126 end$ | |
127 $ | |
128 ##$ | |
129 # {$ | |
130 # "AccountingPolicy" => {$ | |
131 # "Item" => [<Hashes of records>]$ | |
132 # }$ | |
133 # }$ | |
134 #$ | |
135 def parse_and_update_record_from(file_name, io_or_string)$ | |
136 model = "Dion::#{File.basename(file_name, ".xml")}".constantize$ | |
137 reader = Nokogiri::XML::Reader(io_or_string)$ | |
138 reader.each do |node|$ | |
139 if node.name == "Item" && node.inner_xml$ | |
140 record = Hash.from_xml(node.outer_xml)["Item"]$ | |
141 if record$ | |
142 record.underscore_keys!.symbolize_keys!$ | |
143 model.find_and_update_attributes record$ | |
144 end$ | |
145 end$ | |
146 end$ | |
147 end$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment