Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zipline method not working from Grape API endpoint #106

Open
sra-wtag opened this issue May 1, 2024 · 1 comment
Open

zipline method not working from Grape API endpoint #106

sra-wtag opened this issue May 1, 2024 · 1 comment

Comments

@sra-wtag
Copy link

sra-wtag commented May 1, 2024

I am using Grape for some of my APIs. I am trying to implement an export API. Now I am getting an error like:
undefined method 'zipline' for #<Class:0x000000012385c270> in '/api/test/:id/export' endpoint

My API resource is:

class Test < Api
      # enable zipline
      include Zipline

      resource :test do
          get '/export' do
            files = JSON.parse Test.find(params[:id]).exportfiles
            zipline(files, "zip_file_name")
          end
      end
end
@julik
Copy link
Contributor

julik commented Jul 16, 2024

Sadly Grape does not support ActionController methods, so zipline() won't work there. The Zipline module relies on a number of methods being available on ActionController and uses them. You need to do some work to wire zipline (and zip_kit) into Grape. I suspect it can be done somewhat like so (I doubt that JSON.parse in your code is appropriate, what does exportfiles return exactly?):

get '/export' do
  files_and_names = Test.find(params[:id]).exportfiles
  ZipKit::OutputEnumerator.streaming_http_headers.each_pair do |header_name, header_value|
    header(header_name, header_value)
  end
  enum = ZipKit::OutputEnumerator.new do |zip_kit_streamer|
    handler = Zipline::ZipHandler.new(zip_kit_streamer, Rails.logger)
    files_and_names.each do |file, name, options = {}|
      handler.handle_file(file, name.to_s, options)
    end
  end

  Grape::ServeStream::StreamResponse.new(enum)
end

Basically, you need to find a way to serve a standard Rack enumerable body using Grape. You might want to inquire in Grape support channels on how to do this (I never used Grape myself) - https://github.com/ruby-grape/grape?tab=readme-ov-file#project-resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants