Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

issues with latest Grape API - is this project still maintained? #7

Open
bluegod opened this issue May 13, 2015 · 4 comments
Open

issues with latest Grape API - is this project still maintained? #7

bluegod opened this issue May 13, 2015 · 4 comments

Comments

@bluegod
Copy link

bluegod commented May 13, 2015

Hi,

Similar to the example, I have setup rack-stream requiring the lib and adding "use Rack::Stream" to my config.ru. I'm using rackup but I have some issues getting the DSL methods to work:

class:API': undefined method `after_open' for #Grape::Endpoint:0x0000010142b218 (NoMethodError)

Ans using stream:

class:API': undefined method `stream' for #Grape::Endpoint:0x000001022bfe78 (NoMethodError)

Any idea whether I'm missing something here ?

In my API class I do something like:

class API < Grape::API
format :json
prefix 'api'
version 'v1'
include Rack::Stream::DSL
resource :messages do
  post do
    #does stuff
  end

  get do
      after_open do
        chunk my_data_method
      end
      status 200
      header 'Content-Type', 'application/json'
      chunk 'bye!'
      ""
      end
  end
end
@croeck
Copy link

croeck commented May 18, 2015

I think you need to include the Rack::Stream::DSL within a helpers block.
The following snippet is taken from one of the examples:

helpers do
    include Rack::Stream::DSL
end

@bluegod
Copy link
Author

bluegod commented May 19, 2015

Thanks - Actually my app changed a bit and I'm calling a @data.get_stuff method inside the "get do". Data is an object that handles the SQL bit and the long polling waiting. I have tried to include or extend the module Rack::Stream::DSL from this other class Data, but still have no success in finding the DSL methods there ?

@bluegod
Copy link
Author

bluegod commented May 19, 2015

Ok I made some progress. I managed to pass the env to the method in the other class as a parameter and I can call env['rack.stream'].chunk or env['rack.stream'].close directly from there.

A small issue I have now is when calling:

env['rack.stream'].chunk 'nothing to say'

In the actual body response the content is something like
<Rack::App...>nothing to say

Returning "" at the end of the get do method the response body is set to:
""nothing to say, which is still not ideal. Any idea how to avoid returning anything from there?

Another question I have is, do I have to set status/headers as in the example? Previously Grape would handle this for you.

@bluegod
Copy link
Author

bluegod commented May 19, 2015

Another update on this. I managed to get it working. I realised in the example the default_format in Grape is set to :txt, but then manually set to json using the headers. That worked, as using text seems to merge that final "" while in JSON it expects a JSON object and returning back something like {} will get merged into an array in the body.

Anyway, I do see this as a workaround and perhaps a better solution would be to somehow override the Grape method that process the body - possibly not easy to do.

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

No branches or pull requests

2 participants