module CalInvite::Providers::IcsDownload
Module for handling ICS file downloads with proper headers and file naming. Provides utility methods for preparing ICS content for HTTP download.
Public Class Methods
# File lib/cal_invite/providers/ics_content.rb 35 def self.headers(filename, method: nil) 36 content_type = 'text/calendar; charset=UTF-8' 37 content_type += "; method=#{method == :decline_counter ? 'DECLINECOUNTER' : method.to_s.upcase}" if method 38 39 { 40 'Content-Type' => content_type, 41 'Content-Disposition' => "attachment; filename=#{sanitize_filename(filename)}" 42 } 43 end
Generates appropriate HTTP headers for ICS file download.
@param filename [String] The desired filename for the download @param method [Symbol, nil] The iCalendar METHOD used to generate the content (:publish or :request). When given, it’s added as a method parameter on the Content-Type header — this is what lets mail clients (Gmail, Outlook, Apple Mail) recognize the attachment as a meeting invite and render RSVP actions instead of a plain file attachment. Must match the METHOD in the .ics content itself. @return [Hash] HTTP headers for the ICS file download
Source
# File lib/cal_invite/providers/ics_content.rb 49 def self.sanitize_filename(filename) 50 filename.gsub(/[^0-9A-Za-z.\-]/, '_') 51 end
Sanitizes a filename by removing potentially problematic characters.
@param filename [String] The filename to sanitize @return [String] The sanitized filename
# File lib/cal_invite/providers/ics_content.rb 64 def self.wrap_for_download(content, title, method: nil) 65 filename = sanitize_filename("#{title.downcase}_#{Time.now.strftime('%Y%m%d')}.ics") 66 { 67 content: content, 68 headers: headers(filename, method: method) 69 } 70 end
Wraps ICS content with appropriate download information.
@param content [String] The ICS calendar content @param title [String] The event title to use in the filename @param method [Symbol, nil] The iCalendar METHOD used to generate content (:publish or :request); forwarded to {headers}. Must match the METHOD used when generating content itself. @return [Hash] Hash containing content and headers for download @example result = IcsDownload.wrap_for_download(ics_content, “team-meeting”) # => { content: “BEGIN:VCALENDAR…”, headers: { ‘Content-Type’ => ‘…’ } }