Skip to content

Commit

Permalink
Allow imported ical events to be recurring
Browse files Browse the repository at this point in the history
  • Loading branch information
dhedlund committed Sep 19, 2013
1 parent f4bd9d1 commit 4c50928
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 309 deletions.
11 changes: 8 additions & 3 deletions app/models/source_parser/ical.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def self.to_abstract_events(opts={})
# Skip old events by default

opts[:skip_old] = true unless opts[:skip_old] == false
cutoff = Time.now.yesterday
range_start = opts[:skip_old] ? 1.day.ago : 5.years.ago
range_end = 6.months.from_now # needed for infinite recurring events

content = content_for(opts).gsub(/\r\n/, "\n")

Expand All @@ -73,8 +74,12 @@ def self.to_abstract_events(opts={})
end
end
content_calendars.each do |content_calendar|
content_calendar.events.each_with_index do |component, index|
next if opts[:skip_old] && (component.dtend || component.dtstart).to_time < cutoff
# convert recurring events into discrete events, filter to date range
components = content_calendar.events.flat_map do |component|
component.occurrences(:overlapping => [range_start, range_end])
end

components.each do |component|
event = AbstractEvent.new
event.title = component.summary
event.description = component.description
Expand Down
2 changes: 1 addition & 1 deletion spec/models/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
@basic_event = Event.new(
:title => 'Web 2.0 Conference',
:url => 'http://www.web2con.com/',
:start_time => Time.zone.parse('2007-10-05'),
:start_time => Time.zone.parse('2013-04-05'),
:end_time => nil,
:venue => @basic_venue)
end
Expand Down
46 changes: 23 additions & 23 deletions spec/models/source_parser_ical_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def events_from_ical_at(filename)
events.size.should eq 1
event = events.first
event.title.should be_blank
event.start_time.should eq Time.parse('Wed Jan 17 00:00:00 2007')
event.start_time.should eq Time.zone.parse('Thu Jan 17 00:00:00 2013')
event.venue.should be_nil
end

Expand All @@ -114,20 +114,20 @@ def events_from_ical_at(filename)
event = events.first
event.title.should eq "XPDX (eXtreme Programming) at CubeSpace"
event.description.should be_blank
event.start_time.should eq Time.parse("2007-10-24 18:30:00")
event.end_time.should eq Time.parse("2007-10-24 19:30:00")
event.start_time.should eq Time.zone.parse("2012-10-24 15:30:00")
event.end_time.should eq Time.zone.parse("2012-10-24 20:30:00")

event = events[17]
event.title.should eq "Code Sprint/Coding Dojo at CubeSpace"
event.description.should be_blank
event.start_time.should eq Time.parse("2007-10-17 19:00:00")
event.end_time.should eq Time.parse("2007-10-17 21:00:00")
event.start_time.should eq Time.zone.parse("2012-10-17 20:00:00")
event.end_time.should eq Time.zone.parse("2012-10-17 22:00:00")

event = events.last
event.title.should eq "Adobe Developer User Group"
event.description.should eq "http://pdxria.com/"
event.start_time.should eq Time.parse("2007-01-16 17:30:00")
event.end_time.should eq Time.parse("2007-01-16 18:30:00")
event.start_time.should eq Time.zone.parse("2012-01-16 17:30:00")
event.end_time.should eq Time.zone.parse("2012-01-16 18:30:00")
end

it "should parse non-Vcard locations" do
Expand All @@ -152,23 +152,23 @@ def events_from_ical_at(filename)
events = @source.create_events!(:skip_old => false)
event = events.first

event.start_time.should eq Time.parse('Thu Jul 01 08:00:00 +0000 2010')
event.end_time.should eq Time.parse('Thu Jul 01 09:00:00 +0000 2010')
event.start_time.should eq Time.zone.parse('Thu Jul 01 08:00:00 +0000 2010')
event.end_time.should eq Time.zone.parse('Thu Jul 01 09:00:00 +0000 2010')

# time should be the same after saving event to, and getting it from, database
event.save
e = Event.find(event)
e.start_time.should eq Time.parse('Thu Jul 01 08:00:00 +0000 2010')
e.end_time.should eq Time.parse('Thu Jul 01 09:00:00 +0000 2010')
e.start_time.should eq Time.zone.parse('Thu Jul 01 08:00:00 +0000 2010')
e.end_time.should eq Time.zone.parse('Thu Jul 01 09:00:00 +0000 2010')
end

it "should store time with TZID=GMT in UTC" do
pending "RiCal doesn't consider the time zone data in this file valid"
events = events_from_ical_at('ical_gmt.ics')
events.size.should eq 1
abstract_event = events.first
abstract_event.start_time.should eq Time.parse('Fri May 07 08:00:00 +0000 2020')
abstract_event.end_time.should eq Time.parse('Fri May 07 09:00:00 +0000 2020')
abstract_event.start_time.should eq Time.zone.parse('Fri May 07 08:00:00 +0000 2020')
abstract_event.end_time.should eq Time.zone.parse('Fri May 07 09:00:00 +0000 2020')
end

end
Expand All @@ -186,48 +186,48 @@ def events_from_ical_at(filename)
UID:Unit-01
SUMMARY:Past start and no end
DESCRIPTION:Ayanami
DTSTART:#{(Time.now-1.year).strftime("%Y%m%d")}
DTSTART:#{(Time.zone.now-3.months).strftime("%Y%m%d")}
DTSTAMP:040425
SEQ:0
END:VEVENT
BEGIN:VEVENT
UID:Unit-02
SUMMARY:Current start and no end
DESCRIPTION:Soryu
DTSTART:#{(Time.now+1.year).strftime("%Y%m%d")}
DTSTART:#{(Time.zone.now+3.months).strftime("%Y%m%d")}
DTSTAMP:040425
SEQ:1
END:VEVENT
BEGIN:VEVENT
UID:Unit-03
SUMMARY:Past start and current end
DESCRIPTION:Soryu a
DTSTART:#{(Time.now-1.year).strftime("%Y%m%d")}
DTEND:#{(Time.now+1.year).strftime("%Y%m%d")}
DTSTART:#{(Time.zone.now-3.months).strftime("%Y%m%d")}
DTEND:#{(Time.zone.now+3.months).strftime("%Y%m%d")}
DTSTAMP:040425
END:VEVENT
BEGIN:VEVENT
UID:Unit-04
SUMMARY:Current start and current end
DESCRIPTION:Soryu as
DTSTART:#{Time.now.strftime("%Y%m%d")}
DTEND:#{(Time.now+1.year).strftime("%Y%m%d")}
DTSTART:#{Time.zone.now.strftime("%Y%m%d")}
DTEND:#{(Time.zone.now+3.months).strftime("%Y%m%d")}
DTSTAMP:040425
END:VEVENT
BEGIN:VEVENT
UID:Unit-05
SUMMARY:Past start and past end
DESCRIPTION:Soryu qewr
DTSTART:#{(Time.now-1.year).strftime("%Y%m%d")}
DTEND:#{(Time.now-1.year).strftime("%Y%m%d")}
DTSTART:#{(Time.zone.now-3.months).strftime("%Y%m%d")}
DTEND:#{(Time.zone.now-3.months).strftime("%Y%m%d")}
DTSTAMP:040425
END:VEVENT
BEGIN:VEVENT
UID:Unit-06
SUMMARY:Current start and past end
DESCRIPTION:Not a valid event
DTSTART:#{Time.now.strftime("%Y%m%d")}
DTEND:#{(Time.now-1.year).strftime("%Y%m%d")}
DTSTART:#{Time.zone.now.strftime("%Y%m%d")}
DTEND:#{(Time.zone.now-3.months).strftime("%Y%m%d")}
DTSTAMP:040425
END:VEVENT
END:VCALENDAR
Expand Down
2 changes: 1 addition & 1 deletion spec/samples/hcal_basic.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="vevent">
<a class="url" href="http://www.web2con.com/">http://www.web2con.com/</a>
<span class="summary">Web 2.0 Conference</span>:
<abbr class="dtstart" title="2007-10-05">Friday, October 5, 2007</abbr>,
<abbr class="dtstart" title="2013-04-05">Friday, April 5, 2013</abbr>,
at the <span class="location">Argent Hotel, San Francisco, CA</span>
</div>
2 changes: 1 addition & 1 deletion spec/samples/ical_basic.ics
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ BEGIN:VEVENT
UID:dclicio.us-243
DESCRIPTION:with http://dclicio.us
SUMMARY:
DTSTART:20070117
DTSTART:20130117
DTSTAMP:040425
SEQ:0
END:VEVENT
Expand Down
Loading

0 comments on commit 4c50928

Please sign in to comment.