Module: JsonApiable
- Extended by:
- ActiveSupport::Concern
- Includes:
- Errors, Renderers
- Defined in:
- lib/json_apiable/errors.rb,
lib/json_apiable/version.rb,
lib/json_apiable/renderers.rb,
lib/json_apiable/base_filter.rb,
lib/json_apiable/json_apiable.rb,
lib/json_apiable/configuration.rb,
lib/json_apiable/filter_parser.rb,
lib/json_apiable/params_parser.rb,
lib/json_apiable/filter_matchers.rb,
lib/json_apiable/pagination_parser.rb
Defined Under Namespace
Modules: Errors, FilterMatchers, Renderers
Classes: BaseFilter, Configuration, FilterParser, PaginationParser, ParamsParser
Constant Summary
collapse
- VERSION =
"0.6.2"
- JSONAPI_CONTENT_TYPE =
'application/vnd.api+json'
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Renderers
#json_render_errors, #respond_to_bad_argument, #respond_to_capability_error, #respond_to_exception_raised, #respond_to_forbidden, #respond_to_malformed_request, #respond_to_not_found, #respond_to_unauthorized, #respond_to_unprocessable_entity, #respond_to_unsupported_media_type
Class Attribute Details
.configuration ⇒ Object
35
36
37
|
# File 'lib/json_apiable/json_apiable.rb', line 35
def self.configuration
@configuration ||= Configuration.new
end
|
Instance Attribute Details
#jsonapi_assign_params ⇒ Object
Returns the value of attribute jsonapi_assign_params.
10
11
12
|
# File 'lib/json_apiable/json_apiable.rb', line 10
def jsonapi_assign_params
@jsonapi_assign_params
end
|
#jsonapi_build_params ⇒ Object
Should be overwritten in specific controllers. If you need to manipulate params before they are parsed, that’s the place to do it
104
105
106
|
# File 'lib/json_apiable/json_apiable.rb', line 104
def jsonapi_build_params
@jsonapi_build_params
end
|
#jsonapi_default_page_size ⇒ Object
Should be overwritten in specific controllers
109
110
111
|
# File 'lib/json_apiable/json_apiable.rb', line 109
def jsonapi_default_page_size
@jsonapi_default_page_size
end
|
#jsonapi_exclude_attributes ⇒ Object
Returns the value of attribute jsonapi_exclude_attributes.
10
11
12
|
# File 'lib/json_apiable/json_apiable.rb', line 10
def jsonapi_exclude_attributes
@jsonapi_exclude_attributes
end
|
#jsonapi_exclude_relationships ⇒ Object
Returns the value of attribute jsonapi_exclude_relationships.
10
11
12
|
# File 'lib/json_apiable/json_apiable.rb', line 10
def jsonapi_exclude_relationships
@jsonapi_exclude_relationships
end
|
#jsonapi_filter_class ⇒ Object
Returns the value of attribute jsonapi_filter_class.
10
11
12
|
# File 'lib/json_apiable/json_apiable.rb', line 10
def jsonapi_filter_class
@jsonapi_filter_class
end
|
#jsonapi_filter_hash ⇒ Object
Returns the value of attribute jsonapi_filter_hash.
10
11
12
|
# File 'lib/json_apiable/json_apiable.rb', line 10
def jsonapi_filter_hash
@jsonapi_filter_hash
end
|
#jsonapi_include_array ⇒ Object
Returns the value of attribute jsonapi_include_array.
10
11
12
|
# File 'lib/json_apiable/json_apiable.rb', line 10
def jsonapi_include_array
@jsonapi_include_array
end
|
#jsonapi_page_hash ⇒ Object
Returns the value of attribute jsonapi_page_hash.
10
11
12
|
# File 'lib/json_apiable/json_apiable.rb', line 10
def jsonapi_page_hash
@jsonapi_page_hash
end
|
Class Method Details
43
44
45
|
# File 'lib/json_apiable/json_apiable.rb', line 43
def self.configure
yield(configuration)
end
|
.reset ⇒ Object
39
40
41
|
# File 'lib/json_apiable/json_apiable.rb', line 39
def self.reset
@configuration = Configuration.new
end
|
Instance Method Details
#ensure_jsonapi_content_type ⇒ Object
123
124
125
|
# File 'lib/json_apiable/json_apiable.rb', line 123
def ensure_jsonapi_content_type
respond_to_unsupported_media_type unless supported_media_type?
end
|
#ensure_jsonapi_valid_query_params ⇒ Object
127
128
129
130
|
# File 'lib/json_apiable/json_apiable.rb', line 127
def ensure_jsonapi_valid_query_params
invalid_params = request.query_parameters.keys.reject { |k| JsonApiable.configuration.valid_query_params.include?(k) }
respond_to_bad_argument(invalid_params.first) if invalid_params.present?
end
|
#jsonapi_allowed_attributes ⇒ Object
Should be overwritten in specific controllers
114
115
116
|
# File 'lib/json_apiable/json_apiable.rb', line 114
def jsonapi_allowed_attributes
%i[]
end
|
#jsonapi_allowed_relationships ⇒ Object
Should be overwritten in specific controllers
119
120
121
|
# File 'lib/json_apiable/json_apiable.rb', line 119
def jsonapi_allowed_relationships
%i[]
end
|
#jsonapi_attribute(attrib_key) ⇒ Object
47
48
49
|
# File 'lib/json_apiable/json_apiable.rb', line 47
def jsonapi_attribute(attrib_key)
jsonapi_build_params.dig(:data, :attributes, attrib_key)
end
|
#jsonapi_attribute_present?(attrib_key) ⇒ Boolean
51
52
53
|
# File 'lib/json_apiable/json_apiable.rb', line 51
def jsonapi_attribute_present?(attrib_key)
jsonapi_attribute(attrib_key).present?
end
|
#jsonapi_exclude_attribute(attrib_key) ⇒ Object
88
89
90
91
92
93
|
# File 'lib/json_apiable/json_apiable.rb', line 88
def jsonapi_exclude_attribute(attrib_key)
@jsonapi_exclude_attributes ||= []
@jsonapi_exclude_attributes << attrib_key.to_sym
@invalidate_assign_params = true
jsonapi_build_params.dig(:data, :attributes, attrib_key)
end
|
#jsonapi_exclude_relationship(rel_key) ⇒ Object
95
96
97
98
99
100
|
# File 'lib/json_apiable/json_apiable.rb', line 95
def jsonapi_exclude_relationship(rel_key)
@jsonapi_exclude_relationships ||= []
@jsonapi_exclude_relationships << rel_key.to_sym
@invalidate_assign_params = true
jsonapi_build_params.dig(:data, :relationships, rel_key)
end
|
#jsonapi_relationship(attrib_key) ⇒ Object
55
56
57
|
# File 'lib/json_apiable/json_apiable.rb', line 55
def jsonapi_relationship(attrib_key)
jsonapi_build_params.dig(:data, :relationships, attrib_key)
end
|
#jsonapi_relationship_attribute(relationship, attribute) ⇒ Object
67
68
69
70
71
72
73
|
# File 'lib/json_apiable/json_apiable.rb', line 67
def jsonapi_relationship_attribute(relationship, attribute)
if [:id, :type].include?(attribute.to_sym)
jsonapi_relationship_data(relationship)&.dig(attribute)
else
jsonapi_relationship_data(relationship)&.dig(:attributes, attribute)
end
end
|
#jsonapi_relationship_data(attrib_key) ⇒ Object
59
60
61
|
# File 'lib/json_apiable/json_apiable.rb', line 59
def jsonapi_relationship_data(attrib_key)
jsonapi_build_params.dig(:data, :relationships, attrib_key, :data)
end
|
#jsonapi_relationship_present?(attrib_key) ⇒ Boolean
63
64
65
|
# File 'lib/json_apiable/json_apiable.rb', line 63
def jsonapi_relationship_present?(attrib_key)
jsonapi_relationship(attrib_key).present?
end
|
#parse_jsonapi_include ⇒ Object
153
154
155
|
# File 'lib/json_apiable/json_apiable.rb', line 153
def parse_jsonapi_include
@jsonapi_include_array = query_params[:include].presence&.gsub(/ /, '')&.split(',')&.map(&:to_sym).to_a
end
|
149
150
151
|
# File 'lib/json_apiable/json_apiable.rb', line 149
def
@jsonapi_page_hash = PaginationParser.(query_params, jsonapi_default_page_size)
end
|
#query_params ⇒ Object
157
158
159
|
# File 'lib/json_apiable/json_apiable.rb', line 157
def query_params
request.query_parameters
end
|
#set_jsonapi_content_type ⇒ Object
145
146
147
|
# File 'lib/json_apiable/json_apiable.rb', line 145
def set_jsonapi_content_type
response.['Content-Type'] = JSONAPI_CONTENT_TYPE
end
|
#set_jsonapi_filter(filter_class) ⇒ Object
140
141
142
143
|
# File 'lib/json_apiable/json_apiable.rb', line 140
def set_jsonapi_filter(filter_class)
@jsonapi_filter_class = filter_class
@jsonapi_filter_hash = FilterParser.parse_filters!(jsonapi_build_params, filter_class)
end
|