Sha256: 554e0667eefa7ce3190894bf1d47a1d55e57638ad3baf1b99f01c1e8c48848cc

Contents?: true

Size: 1.54 KB

Versions: 5

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module Calagator
  module GoogleEventExportHelper
    # Return a Google Calendar export URL.

    def google_event_export_link(event)
      GoogleEventExportLink.new(event, self).render
    end

    class GoogleEventExportLink < Struct.new(:event, :context)
      def render
        truncate(url + query)
      end

      private

      def url
        "https://www.google.com/calendar/event?action=TEMPLATE&trp=true&"
      end

      def query
        fields.collect do |field|
          send(field).to_query(field)
        end.compact.join("&")
      end

      def fields
        %i[text dates location sprop details]
      end

      def text
        event.title
      end

      def dates
        google_time_format = "%Y%m%dT%H%M%SZ"
        end_time = event.end_time || event.start_time
        "#{event.start_time.utc.strftime(google_time_format)}/#{end_time.utc.strftime(google_time_format)}"
      end

      def location
        location = event.venue.try(:title)
        if (address = event.venue.try(:geocode_address))
          location += ", #{address}" if address.present?
        end
        location
      end

      def sprop
        "website:#{event.url.sub(%r{^http.?://}, "")}" if event.url.present?
      end

      def details
        "Imported from: #{context.event_url(event)} \n\n#{event.description}"
      end

      def truncate(string)
        omission = "...[truncated]"
        length = 1024 - omission.length
        context.truncate(string, length: length, omission: omission)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
koalagator-3.0.0 app/helpers/calagator/google_event_export_helper.rb
koalagator-2.1.1 app/helpers/calagator/google_event_export_helper.rb
koalagator-2.1.0 app/helpers/calagator/google_event_export_helper.rb
calagator2-2.1.1 app/helpers/calagator/google_event_export_helper.rb
calagator2-2.1.0 app/helpers/calagator/google_event_export_helper.rb