diff options
| author | hukl <contact@smyck.org> | 2009-03-31 22:56:23 +0200 |
|---|---|---|
| committer | hukl <contact@smyck.org> | 2009-03-31 22:56:23 +0200 |
| commit | 54d31710a4976163d74e4040cd6c2ad8c055c43d (patch) | |
| tree | bf20a11c40920ae53bb6260da999bfab24eaa02d /lib | |
| parent | d9c2a33aed53679c2fbd4f2e69660a16fe35fbb3 (diff) | |
renamed and refactored even further
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/chaos_importer.rb (renamed from lib/chaos_xml.rb) | 102 |
1 files changed, 72 insertions, 30 deletions
diff --git a/lib/chaos_xml.rb b/lib/chaos_importer.rb index 94dbe10..3a307a3 100644 --- a/lib/chaos_xml.rb +++ b/lib/chaos_importer.rb | |||
| @@ -1,9 +1,21 @@ | |||
| 1 | require 'iconv' | 1 | require 'iconv' |
| 2 | require 'nokogiri' | 2 | require 'nokogiri' |
| 3 | require 'lib/chaos_calendar/ical_occurrences' | 3 | require 'lib/chaos_calendar/ical_occurrences' |
| 4 | require 'digest/sha1' | ||
| 4 | 5 | ||
| 5 | 6 | ||
| 6 | class ChaosXml | 7 | class ChaosXml |
| 8 | attr_reader :xml, :unique_name, :locale, :date, :slug | ||
| 9 | |||
| 10 | def initialize options = {} | ||
| 11 | options.each_pair do |key,value| | ||
| 12 | instance_variable_set "@#{key}", value | ||
| 13 | instance_eval("def #{key}; @#{key};end") | ||
| 14 | end | ||
| 15 | end | ||
| 16 | end | ||
| 17 | |||
| 18 | class ChaosImporter | ||
| 7 | include Enumerable | 19 | include Enumerable |
| 8 | 20 | ||
| 9 | def initialize path | 21 | def initialize path |
| @@ -11,23 +23,50 @@ class ChaosXml | |||
| 11 | Node.create! | 23 | Node.create! |
| 12 | end | 24 | end |
| 13 | 25 | ||
| 14 | @path = path | 26 | @directory = path |
| 15 | @years = {} | 27 | @years = {} |
| 28 | end | ||
| 29 | |||
| 30 | # Iterates through all xml files within the @directory and yields ChaosXml | ||
| 31 | # objects with the parsed attributes. These attributes are the basic data | ||
| 32 | # needed for further processing / parsing. | ||
| 33 | def each | ||
| 34 | directories = Dir.glob("#{@directory}/*/*.xml{,.de,.en}") | ||
| 35 | |||
| 36 | directories.each do |path| | ||
| 37 | next if path =~ /index\.xml/ | ||
| 38 | |||
| 39 | chaos_id = chaos_id_from_path( path ) | ||
| 40 | options = {} | ||
| 41 | options[:xml] = Nokogiri::XML( File.new(path).read ) | ||
| 42 | options[:locale] = lang_from_path( path ) | ||
| 43 | options[:date] = options[:xml].at("//date").content.to_date | ||
| 44 | options[:slug] = chaos_id | ||
| 45 | options[:unique_name] = "updates/#{options[:date]}/#{options[:slug]}" | ||
| 46 | xml = ChaosXml.new options | ||
| 47 | |||
| 48 | yield xml | ||
| 49 | end | ||
| 16 | end | 50 | end |
| 17 | 51 | ||
| 52 | # Uses the each method to loop over the xml files and uses the attrubutes of | ||
| 53 | # the returned ChaosXml objects to do some further processing which is needed | ||
| 54 | # to create proper ActiveRecord records | ||
| 18 | def import_updates | 55 | def import_updates |
| 19 | unless @updates = Node.find_by_unique_name('updates') | 56 | unless @updates = Node.find_by_unique_name('updates') |
| 20 | @updates = Node.create!( :slug => 'updates' ) | 57 | @updates = Node.create!( :slug => 'updates' ) |
| 21 | @updates.move_to_child_of Node.root | 58 | @updates.move_to_child_of Node.root |
| 22 | end | 59 | end |
| 23 | 60 | ||
| 24 | self.each do |chaospage, chaos_id, lang| | 61 | self.each do |update| |
| 25 | node = find_or_create_node( chaospage, chaos_id ) | 62 | author = find_or_create_author( update ) |
| 26 | html = convert_to_html( chaospage ) | 63 | node = find_or_create_node( update ) |
| 27 | page = fill_draft_with_content(node.draft, html, lang) | 64 | html = convert_to_html( update.xml ) |
| 65 | page = fill_draft_with_content(node.draft, html, update.locale) | ||
| 28 | 66 | ||
| 29 | add_tags_to_page page, chaospage, "update" | 67 | add_tags_to_page page, update.xml, "update" |
| 30 | add_event_to_node node, chaospage if page.tag_list.include?("event") | 68 | add_event_to_node node, update.xml if page.tag_list.include?("event") |
| 69 | page.user = author | ||
| 31 | page.save | 70 | page.save |
| 32 | puts node.unique_name | 71 | puts node.unique_name |
| 33 | end | 72 | end |
| @@ -35,19 +74,6 @@ class ChaosXml | |||
| 35 | Node.all.each {|node| node.publish_draft!} | 74 | Node.all.each {|node| node.publish_draft!} |
| 36 | end | 75 | end |
| 37 | 76 | ||
| 38 | def each | ||
| 39 | directories = Dir.glob("#{@path}/*/*.xml{,.de,.en}") | ||
| 40 | |||
| 41 | directories.each do |path| | ||
| 42 | next if path =~ /index\.xml/ | ||
| 43 | chaospage = Nokogiri::XML( File.new(path).read ) | ||
| 44 | lang = lang_from_path( path ) | ||
| 45 | chaos_id = chaos_id_from_path( path ) | ||
| 46 | |||
| 47 | yield chaospage, chaos_id, lang | ||
| 48 | end | ||
| 49 | end | ||
| 50 | |||
| 51 | def lang_from_path path | 77 | def lang_from_path path |
| 52 | case path | 78 | case path |
| 53 | when /\.de$/ then :de | 79 | when /\.de$/ then :de |
| @@ -58,24 +84,40 @@ class ChaosXml | |||
| 58 | end | 84 | end |
| 59 | 85 | ||
| 60 | def chaos_id_from_path path | 86 | def chaos_id_from_path path |
| 61 | path.sub(@path, "").split(/\//).last.split(/\./)[0] | 87 | path.sub(@directory, "").split(/\//).last.split(/\./)[0] |
| 62 | end | 88 | end |
| 63 | 89 | ||
| 64 | def find_or_create_node chaospage, chaos_id | 90 | def find_or_create_author update |
| 65 | 91 | login = update.xml.at("//author").content rescue "webmaster" | |
| 66 | date = chaospage.xpath("//date").first.content.to_date | 92 | puts login |
| 67 | unique_name = "updates/#{date.year}/#{chaos_id}" | 93 | |
| 68 | year = date.year | 94 | # password = Digest::SHA1.hexdigest("#{Time.now+rand(100).days}") |
| 95 | # unless author = User.find_by_login(login) | ||
| 96 | # author = User.create!( | ||
| 97 | # :login => login, | ||
| 98 | # :email => "#{login}@example.com", | ||
| 99 | # :password => password, | ||
| 100 | # :password_confirmation => password | ||
| 101 | # ) | ||
| 102 | # end | ||
| 103 | |||
| 104 | # author | ||
| 105 | |||
| 106 | |||
| 107 | end | ||
| 108 | |||
| 109 | def find_or_create_node update | ||
| 110 | year = update.date.year | ||
| 69 | 111 | ||
| 70 | unique_name_array = unique_name.split("/") | 112 | unique_name_array = update.unique_name.split("/") |
| 71 | 113 | ||
| 72 | unless @years[year] || (@years[year] = Node.find_by_unique_name("updates/#{year}")) | 114 | unless @years[year] || (@years[year] = Node.find_by_unique_name("updates/#{year}")) |
| 73 | @years[year] = Node.create :slug => year | 115 | @years[year] = Node.create :slug => year |
| 74 | @years[year].move_to_child_of @updates | 116 | @years[year].move_to_child_of @updates |
| 75 | end | 117 | end |
| 76 | 118 | ||
| 77 | unless node = Node.find_by_unique_name(unique_name) | 119 | unless node = Node.find_by_unique_name(update.unique_name) |
| 78 | node = Node.create :slug => chaos_id | 120 | node = Node.create :slug => update.slug |
| 79 | node.move_to_child_of @years[year] | 121 | node.move_to_child_of @years[year] |
| 80 | end | 122 | end |
| 81 | 123 | ||
