jsonapi_responses 0.1.2 → 0.1.4

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: 90ed37460147f9a795a6297a734d47b55a7bcb4bcbb98025c294684fc9ad56c9
4
- data.tar.gz: cbac58151075f09b2b62a2cecf921722f476970c6ac3ea3013869038fea9386e
3
+ metadata.gz: e111bc098643181daa0fe2da8c85c2fd2bc0d67f5814445312c04e505b59109b
4
+ data.tar.gz: 5d8d3588735b9a009e0bca5491145941b35a899c979b8076901c1088e92a3db4
5
5
  SHA512:
6
- metadata.gz: bd2261e66bc0f85192da72a1a6386bac3623776e32b0dba1708c9d57cb001a3f990dbbede2c34125af9eefe2fe4bb8366f0f7bab359bf2ee971b402fa09487cc
7
- data.tar.gz: 6af64f7c8dc1ac440a3b8ca1acec86bf18556cad62b0dab4fe59689431e86d3a54cc1d6ce306170e9a1eaf22f5496eb450a953fef6d5c34754cbf6a607213c22
6
+ metadata.gz: 74700984e823853a22dbf34c9fd50b48f66adfbc05e9789e31e4e534fd25ec8f6d62c30173e0facb54969175d025b6c97a31ebde6c77bbf56b21b4449da1ced6
7
+ data.tar.gz: cfdafe5edba8bea145fd890ff03175b1597aff1e51b8738b8880c0c65b1d92e598261e579a120a74e7912b4aa8833b90b14ce254f1ddb4e05d345afdd37f5c49
data/CODE_OF_CONDUCT.md CHANGED
@@ -59,8 +59,7 @@ representative at an online or offline event.
59
59
  ## Enforcement
60
60
 
61
61
  Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
- reported to the community leaders responsible for enforcement at
63
- [INSERT CONTACT METHOD].
62
+ reported to the community leaders responsible for enforcement by opening an issue at https://github.com/oortega14/jsonapi_responses/issues.
64
63
  All complaints will be reviewed and investigated promptly and fairly.
65
64
 
66
65
  All community leaders are obligated to respect the privacy and security of the
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
 
@@ -14,6 +14,7 @@ module JsonapiResponses
14
14
  def render_with(record, options = {})
15
15
  action = options[:action] || action_name.to_sym
16
16
  context = (options[:context] || {}).merge(serialization_user)
17
+ context = context.merge(view: params[:view]&.to_sym) unless context.key?(:view)
17
18
  serializer_class = "#{controller_name.singularize.camelize}Serializer".constantize
18
19
  send("respond_for_#{action}", record, serializer_class, context)
19
20
  rescue NoMethodError
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonapiResponses
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.4'
5
5
  end
metadata CHANGED
@@ -1,17 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi_responses
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
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-28 00:00:00.000000000 Z
11
+ date: 2025-05-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: My first gem which tries to get simplier the way you responde in your
14
- API
13
+ description: JsonapiResponses simplifies API response handling by allowing multiple
14
+ response formats from a single endpoint, improving performance and reducing endpoint
15
+ duplication
15
16
  email:
16
17
  - ortegaoscar14@gmail.com
17
18
  executables: []
@@ -40,7 +41,11 @@ licenses:
40
41
  metadata:
41
42
  homepage_uri: https://www.oortega.dev/gems
42
43
  source_code_uri: https://github.com/oortega14/jsonapi_responses
44
+ documentation_uri: https://github.com/oortega14/jsonapi_responses#readme
43
45
  changelog_uri: https://github.com/oortega14/jsonapi_responses/blob/main/CHANGELOG.md
46
+ maintainer: Oscar Ortega
47
+ owner: Oscar Ortega
48
+ rubygems_mfa_required: 'true'
44
49
  post_install_message:
45
50
  rdoc_options: []
46
51
  require_paths:
@@ -59,5 +64,5 @@ requirements: []
59
64
  rubygems_version: 3.5.22
60
65
  signing_key:
61
66
  specification_version: 4
62
- summary: A simple way to respond with JSON in an API
67
+ summary: A simple way to handle multiple JSON response formats in Rails APIs
63
68
  test_files: []