Module: CalInvite::Providers::IcsDownload

Defined in:
lib/cal_invite/providers/ics_content.rb

Overview

Module for handling ICS file downloads with proper headers and file naming. Provides utility methods for preparing ICS content for HTTP download.

Class Method Summary collapse

Class Method Details

.headers(filename, method: nil) ⇒ Hash

Generates appropriate HTTP headers for ICS file download.

Parameters:

  • filename (String)

    The desired filename for the download

  • method (Symbol, nil) (defaults to: 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.

Returns:

  • (Hash)

    HTTP headers for the ICS file download



157
158
159
160
161
162
163
164
165
# File 'lib/cal_invite/providers/ics_content.rb', line 157

def self.headers(filename, method: nil)
  content_type = 'text/calendar; charset=UTF-8'
  content_type += "; method=#{method == :decline_counter ? 'DECLINECOUNTER' : method.to_s.upcase}" if method

  {
    'Content-Type' => content_type,
    'Content-Disposition' => "attachment; filename=#{sanitize_filename(filename)}"
  }
end

.sanitize_filename(filename) ⇒ String

Sanitizes a filename by removing potentially problematic characters.

Parameters:

  • filename (String)

    The filename to sanitize

Returns:

  • (String)

    The sanitized filename



171
172
173
# File 'lib/cal_invite/providers/ics_content.rb', line 171

def self.sanitize_filename(filename)
  filename.gsub(/[^0-9A-Za-z.\-]/, '_')
end

.wrap_for_download(content, title, method: nil) ⇒ Hash

Wraps ICS content with appropriate download information.

Examples:

result = IcsDownload.wrap_for_download(ics_content, "team-meeting")
# => { content: "BEGIN:VCALENDAR...", headers: { 'Content-Type' => '...' } }

Parameters:

  • content (String)

    The ICS calendar content

  • title (String)

    The event title to use in the filename

  • method (Symbol, nil) (defaults to: nil)

    The iCalendar METHOD used to generate content (:publish or :request); forwarded to headers. Must match the METHOD used when generating content itself.

Returns:

  • (Hash)

    Hash containing content and headers for download



186
187
188
189
190
191
192
# File 'lib/cal_invite/providers/ics_content.rb', line 186

def self.wrap_for_download(content, title, method: nil)
  filename = sanitize_filename("#{title.downcase}_#{Time.now.strftime('%Y%m%d')}.ics")
  {
    content: content,
    headers: headers(filename, method: method)
  }
end