diff options
| author | hukl <contact@smyck.org> | 2009-03-30 23:49:11 +0200 |
|---|---|---|
| committer | hukl <contact@smyck.org> | 2009-03-30 23:49:11 +0200 |
| commit | d9c2a33aed53679c2fbd4f2e69660a16fe35fbb3 (patch) | |
| tree | 5a3c8ee95f0518aa756f5885fe9e55f4a97b9360 /lib | |
| parent | 6150357ded46ab20dc73095c89eda64c008c3be5 (diff) | |
okay its done - complete rewrite of the importer. you will need nokogiri gem installed. i will test this a little more and extend it with new features i wanted in the first place. but it had to be refactored first.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/chaos_xml.rb | 105 |
1 files changed, 101 insertions, 4 deletions
diff --git a/lib/chaos_xml.rb b/lib/chaos_xml.rb index f06d750..94dbe10 100644 --- a/lib/chaos_xml.rb +++ b/lib/chaos_xml.rb | |||
| @@ -1,5 +1,7 @@ | |||
| 1 | require 'iconv' | 1 | require 'iconv' |
| 2 | require 'nokogiri' | 2 | require 'nokogiri' |
| 3 | require 'lib/chaos_calendar/ical_occurrences' | ||
| 4 | |||
| 3 | 5 | ||
| 4 | class ChaosXml | 6 | class ChaosXml |
| 5 | include Enumerable | 7 | include Enumerable |
| @@ -25,10 +27,12 @@ class ChaosXml | |||
| 25 | page = fill_draft_with_content(node.draft, html, lang) | 27 | page = fill_draft_with_content(node.draft, html, lang) |
| 26 | 28 | ||
| 27 | add_tags_to_page page, chaospage, "update" | 29 | add_tags_to_page page, chaospage, "update" |
| 28 | add_events_to_page page, chaospage | 30 | add_event_to_node node, chaospage if page.tag_list.include?("event") |
| 29 | 31 | page.save | |
| 30 | puts node.unique_name | 32 | puts node.unique_name |
| 31 | end | 33 | end |
| 34 | |||
| 35 | Node.all.each {|node| node.publish_draft!} | ||
| 32 | end | 36 | end |
| 33 | 37 | ||
| 34 | def each | 38 | def each |
| @@ -115,10 +119,10 @@ class ChaosXml | |||
| 115 | body | 119 | body |
| 116 | end | 120 | end |
| 117 | 121 | ||
| 118 | def add_tags_to_page page, xml, *custom_tags | 122 | def add_tags_to_page page, chaospage, *custom_tags |
| 119 | tag_list = custom_tags | 123 | tag_list = custom_tags |
| 120 | 124 | ||
| 121 | xml.xpath("//flags").each do |node| | 125 | chaospage.xpath("//flags").each do |node| |
| 122 | node.each do |k,v| | 126 | node.each do |k,v| |
| 123 | case k | 127 | case k |
| 124 | when "calendar" | 128 | when "calendar" |
| @@ -136,6 +140,99 @@ class ChaosXml | |||
| 136 | page.save | 140 | page.save |
| 137 | end | 141 | end |
| 138 | 142 | ||
| 143 | def add_event_to_node node, chaospage | ||
| 144 | rrule = get_rrule(chaospage) | ||
| 145 | |||
| 146 | event_options = { | ||
| 147 | :start_time => get_start_time(chaospage), | ||
| 148 | :end_time => get_end_time(chaospage), | ||
| 149 | :allday => is_allday?(chaospage), | ||
| 150 | :rrule => rrule, | ||
| 151 | :custom_rrule => is_custom_rrule?(rrule), | ||
| 152 | :location => get_location(chaospage), | ||
| 153 | :url => get_url(chaospage), | ||
| 154 | :latitude => get_latitude(chaospage), | ||
| 155 | :longitude => get_logitude(chaospage) | ||
| 156 | } | ||
| 157 | |||
| 158 | unless tmp_event = node.event | ||
| 159 | tmp_event = Event.create! event_options.merge({:node_id => node.id}) | ||
| 160 | else | ||
| 161 | tmp_event.update_attributes event_options | ||
| 162 | end | ||
| 163 | end | ||
| 164 | |||
| 165 | def get_start_time chaospage | ||
| 166 | chaospage.at("//ical:DTSTART").content || raise("DTSTART not found") | ||
| 167 | end | ||
| 168 | |||
| 169 | def get_end_time chaospage | ||
| 170 | dtstart = chaospage.at("//ical:DTSTART") | ||
| 171 | dtend = chaospage.at("//ical:DTEND") | ||
| 172 | duration = chaospage.at("//ical:DURATION") | ||
| 173 | |||
| 174 | if dtend | ||
| 175 | return dtend.content | ||
| 176 | elsif duration | ||
| 177 | parsed_duration = Ical_occurrences.duration_to_fixnum(duration.content) | ||
| 178 | return (dtstart.content.to_time + parsed_duration) | ||
| 179 | else | ||
| 180 | raise("Neiter DTEND nor DURATION found") | ||
| 181 | end | ||
| 182 | end | ||
| 183 | |||
| 184 | def is_allday? chaospage | ||
| 185 | !chaospage.at("//ical:DTSTART").[]("VALUE").nil? | ||
| 186 | end | ||
| 187 | |||
| 188 | def get_rrule chaospage | ||
| 189 | if rrule = chaospage.at("//ical:RRULE") | ||
| 190 | rrtxt = '' | ||
| 191 | rrule.children.each do |subrule| | ||
| 192 | rule_name = subrule.name | ||
| 193 | rule_content = subrule.content.sub(/\W/,'') | ||
| 194 | |||
| 195 | next if rule_content.blank? | ||
| 196 | |||
| 197 | rrtxt += "#{rule_name}=#{rule_content};" | ||
| 198 | end | ||
| 199 | rrtxt.chomp!(';') | ||
| 200 | rrtxt | ||
| 201 | else | ||
| 202 | nil | ||
| 203 | end | ||
| 204 | end | ||
| 205 | |||
| 206 | def is_custom_rrule? rrule | ||
| 207 | default_rules = [ | ||
| 208 | "FREQ=WEEKLY;INTERVAL=1", | ||
| 209 | "FREQ=MONTHLY;INTERVAL=1", | ||
| 210 | "FREQ=YEARLY;INTERVAL=1" | ||
| 211 | ] | ||
| 212 | |||
| 213 | rrule && !default_rules.include?(rrule) ? true : false | ||
| 214 | end | ||
| 215 | |||
| 216 | def get_location chaospage | ||
| 217 | location = chaospage.at("//ical:LOCATION") | ||
| 218 | location ? location.content : nil | ||
| 219 | end | ||
| 220 | |||
| 221 | def get_url chaospage | ||
| 222 | location = chaospage.at("//ical:LOCATION") | ||
| 223 | location.[]("ALTREP") if location | ||
| 224 | end | ||
| 225 | |||
| 226 | def get_latitude chaospage | ||
| 227 | geo = chaospage.at("//ical:GEO") | ||
| 228 | geo.text.split(";")[0] if geo | ||
| 229 | end | ||
| 230 | |||
| 231 | def get_logitude chaospage | ||
| 232 | geo = chaospage.at("//ical:GEO") | ||
| 233 | geo.text.split(";")[1] if geo | ||
| 234 | end | ||
| 235 | |||
| 139 | def convert_to_html chaospage | 236 | def convert_to_html chaospage |
| 140 | 237 | ||
| 141 | chaospage.xpath('//paragraph').each {|sub| sub.name = "p"} | 238 | chaospage.xpath('//paragraph').each {|sub| sub.name = "p"} |
