i am new in Rails . i want to compress JSON response in zip format and download it when user hit a specific url. my controller code is below
api :GET, '/tv/latest', 'Get latest tvs.'
error :code => 401, :desc => "Unauthorized"
description "Get latest tvs."
example 'curl -i -H "Accept: application/json" -d "auth_token=nu8vh5GqmVxMHurhRyYz" -X GET http://localhost:3000/tv/latest'
def latest
@tvs = Tv.where(:approved_cd => 0).order('tvs.first_air_date DESC').page(params[:page]).per(12)
count = Tv.where(:approved_cd=>0).count
respond_to do |format|
format.json { render :json =>{:tvs=>JSON.parse(@tvs.to_json(request_type: "index")), :count=>count}}
end
end
and i am trying this.
def latest
@tvs = Tv.where(:approved_cd => 0).order('tvs.first_air_date DESC').page(params[:page]).per(12)
count = Tv.where(:approved_cd=>0).count
require 'rubygems'
require 'zip'
folder = "Users/me/Desktop/stuff_to_zip"
input_filenames = ['image.jpg', 'description.txt', 'stats.csv']
zipfile_name = "/Users/me/Desktop/archive.zip"
Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
input_filenames.each do |filename|
# Two arguments:
# - The name of the file as it will appear in the archive
# - The original file, including the path to find it
zipfile.add(filename, folder + '/' + filename)
end
zipfile.get_output_stream("myFile") { |os| os.write "myFile contains just this" }
end
respond_to do |format|
format.json { render :json =>{:tvs=>JSON.parse(@tvs.to_json(request_type: "index")), :count=>count}}
format.zip { send_file file }
end
end
modify it please so i can get @tvs
array in zip format and download it when i hit
http://localhost:3000/tv/latest
thanks