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

present nestedly? #89

Closed
limbo-lab opened this issue Aug 24, 2014 · 3 comments
Closed

present nestedly? #89

limbo-lab opened this issue Aug 24, 2014 · 3 comments

Comments

@limbo-lab
Copy link

I expect each response should contains code & msg in addition to normal data. Looks like this:

{
    code: 1000,
    msg: "OK",
    data: {
        // normal data goes here if exists
    }
}

I have this code:

resources :books do
    get do
        present :code, 1000
        present :msg, "OK"
        present :data do
            present :items, Book.all, with: Entities::Book
        end
    end
end

Of course this won't work & not DRY. Is there an approach instead?

@lanrion
Copy link

lanrion commented Aug 25, 2014

I define a method:

    def format_result(result, return_code=0)
      {
        API::RESPONSE_CODE   => return_code,
        API::RESPONSE_RESULT => result.as_json
      }
    end

and then to use it like this:

format_result(::Entities::User.represent(agents))

grape-entity has a root method, but it looks doesn't work in subclass。

Anybody else have better solution?

@limbo-lab
Copy link
Author

This is exactly what I want, thanks so much!

@dblock
Copy link
Member

dblock commented Aug 25, 2014

This works. I encourage you to use a proper presenter framework like grape-entity or Roar and to stick to a JSON format like HAL.

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

3 participants