Class: Calagator::Event::IcalRenderer

Inherits:
Object
  • Object
show all
Defined in:
app/models/calagator/event/ical_renderer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event, opts) ⇒ IcalRenderer

Returns a new instance of IcalRenderer.



48
49
50
51
# File 'app/models/calagator/event/ical_renderer.rb', line 48

def initialize(event, opts)
  @event = event
  @imported_from = opts[:url_helper].call(event).to_s if opts[:url_helper]
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



46
47
48
# File 'app/models/calagator/event/ical_renderer.rb', line 46

def event
  @event
end

#imported_fromObject (readonly)

Returns the value of attribute imported_from.



46
47
48
# File 'app/models/calagator/event/ical_renderer.rb', line 46

def imported_from
  @imported_from
end

Class Method Details

.add_name(output) ⇒ Object



38
39
40
# File 'app/models/calagator/event/ical_renderer.rb', line 38

def self.add_name(output)
  output.sub(/(CALSCALE:\w+)/i, "\\1\nX-WR-CALNAME:#{Calagator.title}\nMETHOD:PUBLISH")
end

.normalize_line_endings(output) ⇒ Object



42
43
44
# File 'app/models/calagator/event/ical_renderer.rb', line 42

def self.normalize_line_endings(output)
  output.gsub(/\r?\n/, "\r\n")
end

.render(events, opts = {}) ⇒ Object



20
21
22
23
24
# File 'app/models/calagator/event/ical_renderer.rb', line 20

def self.render(events, opts = {})
  output = render_icalendar(events, opts)
  output = add_name(output)
  output = normalize_line_endings(output)
end

.render_icalendar(events, opts) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/calagator/event/ical_renderer.rb', line 26

def self.render_icalendar(events, opts)
  RiCal.Calendar do |calendar|
    calendar.prodid = '-//Calagator//EN'

    Array(events).each do |event|
      calendar.event do |entry|
        new(event, opts).add_event_to(entry)
      end
    end
  end.export
end

Instance Method Details

#add_event_to(entry) ⇒ Object



53
54
55
56
57
58
# File 'app/models/calagator/event/ical_renderer.rb', line 53

def add_event_to(entry)
  fields.each do |field|
    value = send(field)              # value = summary
    entry.send field, value if value # entry.summary summary if summary
  end
end