Search code examples
ruby-on-railsfile-format

rails - Render template and zip


I'm trying to build a KML file in Rails, which I have done successfully, but now I want to provide a KMZ format as well which would render the index.kml file and zip it. Here is where I get stumped. I have updated the MIME Types as follows.

Mime::Type.register_alias "application/vnd.google-earth.kml+xml", :kml
Mime::Type.register_alias "application/vnd.google-earth.kmz", :kmz

Here is my format block

def index
    @map_items = Items.all
    respond_with(@map_items) do |format|  
      format.kml 
      format.kmz { NOT SURE WHAT IS BEST TO DO }
      format.georss 
    end
  end

ANy help would be much appreciated. Thanks!


Solution

  • I figured out a way to do this with Delayed Job. Every time the points are updated or created I fire off the MapOverlayJob.

    class MapsController < ApplicationController
    
      def overlay
        @points = Points.all
        return render_to_string("overlay.kml")
      end
    
    end
    
    
    class MapOverlayJob
    
      def initialize
        @s3_filename ||= "maps/overlay.kmz"
        @zip_filename ||= "overlay.kml"
      end
    
      def perform
        AWS::S3::S3Object.store(@s3_filename, 
                                build_kmz_file, 
                                S3_BUCKET, 
                                :access => S3_ACL, 
                                :content_type => Mime::KMZ)
      end
    
      private
        def build_kmz_file
          Zippy.new(@zip_filename => MapsController.new.overlay).data
        end
    
    end