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

JSONP breaks presenting Entities #345

Closed
deckchairhq opened this issue Feb 22, 2013 · 3 comments
Closed

JSONP breaks presenting Entities #345

deckchairhq opened this issue Feb 22, 2013 · 3 comments

Comments

@deckchairhq
Copy link
Contributor

To support JSONP requests I am using Rack::JSONP from the 'rack-jsonp' gem:

class MyRouter < Grape::API
  use Rack::JSONP
  use Rack::ContentType, "application/json"

However, when this is enabled my GrapEntities don't 'present' properly, the following is what I get:

http://0.0.0.0:9000/v1/cameras/10?callback=abcDef

returns:

abcDef(#<MyAPI::Entities::V1::Camera:0x007f9d5e82f610>)

However

http://0.0.0.0:9000/v1/cameras/10

returns:

{
  id: 10,
  title: "Test Camera",
  description: "Testing...",
  active: true,
  ...
  minimum_interval: 15
}

The function below illustrates the core of what i'm doing for that route:

    get '/cameras/:camera_id' do
        camera_id = params[:camera_id]
        camera = Camera.includes(:location, :importer).find( camera_id )
        present camera, with: MyAPI::Entities::V1::Camera
    end

Any ideas? I'll keep digging, i'm presuming the issue is the content-type declaration preventing the entity being printed out as JSON. Though I haven't worked my way through Grape's or Grape Entites internals enough yet to be sure.

Cheers,

Gareth

@dblock
Copy link
Member

dblock commented Feb 22, 2013

Start by writing a test in Grape that reproduces this. Shouldn't be hard to fix.

@deckchairhq
Copy link
Contributor Author

Added further description in Pull: #346

@dblock
Copy link
Member

dblock commented Feb 22, 2013

JSONP also needs a formatter:

formatter :jsonp, lambda { |object, env| object.to_json }

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