10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/paras_in_mongo.rb', line 10
def file_in_mongo(filename,options={})
options = options.stringify_keys
session = Moped::Session.new([ "#{options['host']}:#{options['port']}" ])
session.use options['database']
doc =Nokogiri::XML(File.open(filename).read)
book = (doc)
source ={book: book}
paras = doc.search("para[key=yes]")
section = nil
section_title = ''
para_mongo_attrs = paras.map do |para|
unless para.parent == section
section_title = para.parent.search("info[1]/title").text rescue nil
section = para.parent
end
(para,source.merge(location: {section: section_title})) rescue nil
end
para_mongo_attrs.compact.each do |attrs|
session[options['collection']].insert(attrs)
end
end
|