2

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

Faysal
  • 47
  • 1
  • 9
  • did i asked so much difficult question??? no one ans yet......!!!! – Faysal Dec 29 '14 at 10:29
  • You can use simple unix command to zip your files "tar -zcvf archive.tar.gz directory/" – RubyOnRails Dec 29 '14 at 11:03
  • can you fix my code??? can you give me your skype? – Faysal Dec 29 '14 at 11:17
  • Can you tell me what you are achieving??? – RubyOnRails Dec 29 '14 at 11:29
  • 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 t = Tempfile.new("my-temp-filename-#{Time.now}") Zip::OutputStream.open(t.path,"w") do |z| z.put_next_entry(t) end end respond_to do |format| format.json { render :json =>{:tvs=>JSON.parse(@tvs.to_json(request_type: "index")), :count=>count}} format.zip { send_file t.path, :type => 'application/zip',:disposition => 'attachment',:filename => "tv.zip"} end end – Faysal Dec 29 '14 at 11:43
  • from the above ,,, i have got to download zip file. but how i write @tvs name in file – Faysal Dec 29 '14 at 11:44

1 Answers1

0

Try 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
  request.env['HTTP_ACCEPT_ENCODING'] = 'gzip'
  respond_to do |format|
    format.json { render :json =>{:tvs=>@tvs, :count=>count}}
  end
end 
Rajarshi Das
  • 11,778
  • 6
  • 46
  • 74
Chaitali
  • 1
  • 6