jsonapi_responses 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9316ce4149c0d7a3e51d384918dac5519a52ff37877d5719b96d876b99d46584
4
- data.tar.gz: 1659827871a95256fd7dce04ca0abe42831c8441531fc6c78ab2a36b462c7499
3
+ metadata.gz: d9f6ed5b04014e832e884a2425118c2d4d1e54e14d61f4d21050d35b5f6ef152
4
+ data.tar.gz: bf9f7e6ea7fcc880ff5771578d9f35a996bc3122404740a7a7bd3ddb8a712d05
5
5
  SHA512:
6
- metadata.gz: 333f45bcd3074895bd86528e2ae42e00f446f35e2ce25961b719fef191c8152553da5bd142608d81444a2ce873ddd819521b622bb3a68e3bd8af63015f16f75f
7
- data.tar.gz: cda1a85c89d7166ad767420c7baa2acfadbfc9edabe5ada776b209c759c6d7fe2595e3a26a0eebb220352bfffa838314d2cb347054f637f208f3d24c0e2d16c1
6
+ metadata.gz: dfa25f35fd6297541bf8ff12e8453e3434b600156f2f1a266cc0516c0ce055fb4677a2463de9cbeb03217f0c60b32870a3a2e35c9562e4166e638607dfc1b9db
7
+ data.tar.gz: 968e736d4b13ab8f2e8b1903667d7131e396e3ba6b0eca66d9084ae822cd95d02569b6a5976a9e1563642d9326cd3eb20f726dad9efcc0a7f46091facc67b031
data/README.md CHANGED
@@ -1,28 +1,156 @@
1
1
  # JsonapiResponses
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
4
-
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/jsonapi_responses`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ JsonapiResponses is a Ruby gem that simplifies API response handling by allowing multiple response formats from a single endpoint. Instead of creating separate endpoints for different data requirements, this gem enables frontend applications to request varying levels of detail using the same endpoint.
6
4
 
7
5
  ## Installation
8
6
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'jsonapi_responses'
11
+ ```
10
12
 
11
- Install the gem and add to the application's Gemfile by executing:
13
+ And then execute:
12
14
 
13
15
  ```bash
14
- bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
16
+ $ bundle install
15
17
  ```
16
18
 
17
- If bundler is not being used to manage dependencies, install the gem by executing:
19
+ Or install it yourself as:
18
20
 
19
21
  ```bash
20
- gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
22
+ $ gem install jsonapi_responses
21
23
  ```
22
24
 
23
25
  ## Usage
24
26
 
25
- TODO: Write usage instructions here
27
+ ### Setup
28
+
29
+ 1. Include the respondable module in your `ApplicationController`:
30
+
31
+ ```ruby
32
+ class ApplicationController < ActionController::API
33
+ include JsonapiResponses::Respondable
34
+ end
35
+ ```
36
+
37
+ ### Creating Serializers
38
+
39
+ 1. Create a base serializer:
40
+
41
+ ```ruby
42
+ class BaseSerializer
43
+ attr_reader :resource, :context
44
+
45
+ def initialize(resource, context = {})
46
+ @resource = resource
47
+ @context = context
48
+ end
49
+
50
+ def current_user
51
+ @context[:current_user]
52
+ end
53
+ end
54
+ ```
55
+
56
+ 2. Create your model serializers with different view formats:
57
+
58
+ ```ruby
59
+ class DigitalProductSerializer < BaseSerializer
60
+ def serializable_hash
61
+ case context[:view]
62
+ when :summary
63
+ summary_hash
64
+ when :minimal
65
+ minimal_hash
66
+ else
67
+ full_hash
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ def full_hash
74
+ {
75
+ id: resource.id,
76
+ name: resource.name,
77
+ description: resource.description,
78
+ # ... more attributes
79
+ }
80
+ end
81
+
82
+ def summary_hash
83
+ {
84
+ id: resource.id,
85
+ name: resource.name,
86
+ price: resource.price,
87
+ # ... fewer attributes
88
+ }
89
+ end
90
+
91
+ def minimal_hash
92
+ {
93
+ id: resource.id,
94
+ name: resource.name,
95
+ # ... minimal attributes
96
+ }
97
+ end
98
+ end
99
+ ```
100
+
101
+ ### Controller Implementation
102
+
103
+ Use `render_with` in your controllers to handle responses. The view parameter is automatically handled from the request params:
104
+
105
+ ```ruby
106
+ class Api::V1::ProductsController < ApplicationController
107
+ def index
108
+ products = Product.includes(:categories, :attachments)
109
+ render_with(products)
110
+ end
111
+
112
+ def create
113
+ @product = Product.new(product_params)
114
+ render_with(@product)
115
+ end
116
+
117
+ def show
118
+ render_with(@product)
119
+ end
120
+
121
+ def update
122
+ @product.update(product_params)
123
+ render_with(@product)
124
+ end
125
+
126
+ def destroy
127
+ render_with(@product)
128
+ end
129
+
130
+ # Optional: Override the view if needed
131
+ def custom_action
132
+ render_with(@product, context: { view: :custom_view })
133
+ end
134
+ end
135
+ ```
136
+
137
+ ### Making Requests
138
+
139
+ You can request different view formats by adding the `view` parameter:
140
+
141
+ ```
142
+ GET /api/v1/digital_products # Returns full response
143
+ GET /api/v1/digital_products?view=summary # Returns summary response
144
+ GET /api/v1/digital_products?view=minimal # Returns minimal response
145
+ ```
146
+
147
+ ### Performance Benefits
148
+
149
+ By allowing the frontend to request only the needed data, you can:
150
+ - Reduce response payload size
151
+ - Improve API performance
152
+ - Avoid creating multiple endpoints for different data requirements
153
+ - Optimize database queries based on the requested view
26
154
 
27
155
  ## Development
28
156
 
@@ -1,14 +1,10 @@
1
1
  # lib/jsonapi_responses/engine.rb
2
2
  require 'active_support'
3
+ require 'rails'
4
+
3
5
  module JsonapiResponses
4
6
  # Define Engine
5
7
  class Engine < ::Rails::Engine
6
8
  isolate_namespace JsonapiResponses
7
-
8
- initializer 'jsonapi_responses.initialize' do
9
- Rails.application.config.to_prepare do
10
- ApplicationController.include JsonapiResponses::Respondable
11
- end
12
- end
13
9
  end
14
10
  end
@@ -1,17 +1,20 @@
1
1
  require 'active_support/concern'
2
2
  require 'jsonapi_responses/serializable'
3
+ require 'jsonapi_responses/user_context_provider'
3
4
 
4
5
  module JsonapiResponses
5
6
  # Respondable module
6
7
  module Respondable
7
8
  extend ActiveSupport::Concern
8
9
  include JsonapiResponses::Serializable
10
+ include JsonapiResponses::UserContextProvider
9
11
 
10
12
  private
11
13
 
12
- def custom_response(record, options = {})
14
+ def render_with(record, options = {})
13
15
  action = options[:action] || action_name.to_sym
14
- context = options[:context] || {}
16
+ context = (options[:context] || {}).merge(serialization_user)
17
+ context = context.merge(view: params[:view]&.to_sym) unless context.key?(:view)
15
18
  serializer_class = "#{controller_name.singularize.camelize}Serializer".constantize
16
19
  send("respond_for_#{action}", record, serializer_class, context)
17
20
  rescue NoMethodError
@@ -42,7 +45,7 @@ module JsonapiResponses
42
45
  end
43
46
  end
44
47
 
45
- def respond_for_destroy(record)
48
+ def respond_for_destroy(record, _, _)
46
49
  if record.destroy
47
50
  render json: { message: 'register destroyed successfully' }, status: :ok
48
51
  else
@@ -0,0 +1,10 @@
1
+ # JsonapiResponses folder
2
+ module JsonapiResponses
3
+ # Create a helper to intercept de current_user
4
+ module UserContextProvider
5
+ # Este método se incluirá en el controlador y será accesible en los serializadores
6
+ def serialization_user
7
+ { current_user: current_user }
8
+ end
9
+ end
10
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonapiResponses
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.3'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi_responses
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Ortega
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-20 00:00:00.000000000 Z
11
+ date: 2025-05-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: My first gem which tries to get simplier the way you responde in your
14
14
  API
@@ -27,12 +27,12 @@ files:
27
27
  - Rakefile
28
28
  - app/models/item.rb
29
29
  - app/serializers/item_serializer.rb
30
- - builds/jsonapi_responses-0.1.0.gem
31
30
  - config/database.yml
32
31
  - lib/jsonapi_responses.rb
33
32
  - lib/jsonapi_responses/engine.rb
34
33
  - lib/jsonapi_responses/respondable.rb
35
34
  - lib/jsonapi_responses/serializable.rb
35
+ - lib/jsonapi_responses/user_context_provider.rb
36
36
  - lib/jsonapi_responses/version.rb
37
37
  homepage: https://www.oortega.dev/gems
38
38
  licenses:
Binary file