Sha256: 2bd2b0e61eb00d7e5ea02dffb1682c061ccdb266e34589e9cdb66452a21babcb

Contents?: true

Size: 1.22 KB

Versions: 25

Compression:

Stored size: 1.22 KB

Contents

# Response

Let's say you have some JSON payload which can is constructed using Panko serialization result,
like this:

```ruby
class PostsController < ApplicationController
  def index
   posts = Post.all
   render json: {
     success: true,
     total_count: posts.count,
     posts: Panko::ArraySerializer.new(posts, each_serializer: PostSerializer).to_json
   }
  end
end
```

The output of the above will be json string (for `posts`) inside json string and this were `Panko::Response` shines.

```ruby
class PostsController < ApplicationController
  def index
   posts = Post.all
   render json: Panko::Response.new(
     success: true,
     total_count: posts.count,
     posts: Panko::ArraySerializer.new(posts, each_serializer: PostSerializer)
   )
  end
end
```

And everything will work as expected!

## JsonValue

Let's take the above example further, we serialized the posts and cached it as JSON string in our Cache.
Now, you can wrap the cached value with `Panko::JsonValue`, like here -

```ruby
class PostsController < ApplicationController
  def index
   posts = Cache.get("/posts")

   render json: Panko::Response.new(
     success: true,
     total_count: posts.count,
     posts: Panko::JsonValue.from(posts)
   )
  end
end
```

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
panko_serializer-0.3.7 docs/response-bag.md
panko_serializer-0.3.6 docs/response-bag.md
panko_serializer-0.3.5 docs/response-bag.md
panko_serializer-0.3.4 docs/response-bag.md
panko_serializer-0.3.3 docs/response-bag.md