Class: Ical

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActionView::Helpers::TextHelper
Defined in:
app/models/ical.rb

Constant Summary collapse

@@calendars_path =
Radiant::Config["event_calendar.icals_path"]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_refreshmentsObject



123
124
125
126
127
# File 'app/models/ical.rb', line 123

def self.check_refreshments
  find(:all).each do |i|
    i.refresh if i.refresh_automatically? && i.needs_refreshment?
  end
end

.refresh_allObject



129
130
131
132
133
134
# File 'app/models/ical.rb', line 129

def self.refresh_all
  find(:all).each do |i|
    i.refresh 
  end
  return true
end

Instance Method Details

#filenameObject



93
94
95
# File 'app/models/ical.rb', line 93

def filename
  File.join filepath, ics_file
end

#filepathObject



89
90
91
# File 'app/models/ical.rb', line 89

def filepath
  File.join RAILS_ROOT, "public", ics_path
end

#ics_fileObject



101
102
103
# File 'app/models/ical.rb', line 101

def ics_file
  "#{self.calendar.slug}.ics"
end

#ics_pathObject



97
98
99
# File 'app/models/ical.rb', line 97

def ics_path
  File.join @@calendars_path, self.calendar.category
end

#needs_refreshment?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'app/models/ical.rb', line 119

def needs_refreshment?
  last_refresh_date.nil? || Time.now > last_refresh_date + refresh_interval
end

#parse_file(thisfile = filename) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/ical.rb', line 44

def parse_file(thisfile=filename)
  affected = []
  begin
    Ical.transaction do
      self.last_refresh_count = 0
      count = { :created => 0, :updated => 0, :deleted => 0 }
      uuids_seen = []
      File.open(thisfile, "r") do |file|
        components = RiCal.parse(file)
        cal = components.first
        cal.events.each do |cal_event|
          if event = Event.find_by_uuid(cal_event.uid)
            if cal_event.dtstamp > event.updated_at
              affected.push event.update_from(cal_event) 
              count[:updated] += 1
            else
            end
          else
            event = Event.create_from(cal_event)
            event.site = self.calendar.site if event.respond_to? :site=
            self.calendar.events << event
            event.save!
            count[:created] += 1
          end
          uuids_seen.push(cal_event.uid)
        end
      end
      self.last_refresh_count = uuids_seen.length
      self.last_refresh_date = Time.now.utc
      self.save!
      
      self.calendar.events.except_these(uuids_seen).each do |event| 
        event.destroy 
        count[:deleted] += 1
      end
      response = [:created, :updated, :deleted].collect { |counter| 
        "#{pluralize(count[counter], 'event')} #{counter}. "
      }.join('')
    end
  rescue => error
    logger.error "RiCal parse error with: #{self.calendar.name}: #{error}."
    raise
  end 
end

#refreshObject



15
16
17
18
19
20
# File 'app/models/ical.rb', line 15

def refresh
  retrieve_file
  updated = parse_file
  logger.info self.calendar.category + "/" + self.calendar.name + " - iCalendar subscription refreshed on " + Time.now.strftime("%m/%d at %H:%M")
  updated
end

#refresh_automatically?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'app/models/ical.rb', line 115

def refresh_automatically?
  refresh_interval.nil? || refresh_interval.to_i != 0
end

#refresh_intervalObject

I’ve changed this to make the ical refresh decision a simple yes or no and to make the refresh interval a global value



111
112
113
# File 'app/models/ical.rb', line 111

def refresh_interval
  (Radiant::Config['event_calendar.refresh_interval'] || 3600).to_i.seconds
end

#retrieve_fileObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/ical.rb', line 22

def retrieve_file
  File.makedirs filepath
  begin
    uri = URI.parse(url)
    if authenticated?
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = uri.scheme == 'https'
      req = Net::HTTP::Get.new(uri.path)
      req.basic_auth username, password
      response = http.request(req)
    else
      response = Net::HTTP.get_response(uri)
    end
    File.open(filename, "w") do |file|
      file << response.body
    end
  rescue => error
    logger.error "iCal url or file error with: #{self.calendar.name} - #{url} -> (#{filename}): #{error}."
    raise
  end
end

#to_icsObject



105
106
107
# File 'app/models/ical.rb', line 105

def to_ics
  File.join "", ics_path, ics_file
end