Module: JSONAPI::ParamHelpers
- Defined in:
- lib/json_api/support/param_helpers.rb
Class Method Summary collapse
- .build_query_parts_for_param(key, value) ⇒ Object
- .build_query_string(query_params) ⇒ Object
- .deep_symbolize_params(params) ⇒ Object
- .empty_array?(data) ⇒ Boolean
-
.ensure_object!(value, member) ⇒ Object
Every JSON:API document member that carries attributes or resource identifiers must be an object.
- .esc(val) ⇒ Object
- .flatten_filter_hash(hash, parent_key = nil, accumulator = {}) ⇒ Object
Class Method Details
.build_query_parts_for_param(key, value) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/json_api/support/param_helpers.rb', line 53 def build_query_parts_for_param(key, value) encoded_key = CGI.escape(key.to_s) case value when Hash then value.map { |k, v| "#{encoded_key}[#{esc(k)}]=#{esc(v)}" } when Array then value.map { |v| "#{encoded_key}=#{esc(v)}" } else ["#{encoded_key}=#{esc(value)}"] end end |
.build_query_string(query_params) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/json_api/support/param_helpers.rb', line 45 def build_query_string(query_params) query_parts = [] query_params.each do |key, value| query_parts.concat(build_query_parts_for_param(key, value)) end query_parts.join("&") end |
.deep_symbolize_params(params) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/json_api/support/param_helpers.rb', line 25 def deep_symbolize_params(params) if params.is_a?(ActionController::Parameters) params.to_unsafe_h.deep_symbolize_keys else params.to_h.deep_symbolize_keys end end |
.empty_array?(data) ⇒ Boolean
66 67 68 |
# File 'lib/json_api/support/param_helpers.rb', line 66 def empty_array?(data) data.is_a?(Array) && data.empty? end |
.ensure_object!(value, member) ⇒ Object
Every JSON:API document member that carries attributes or resource identifiers must be an
object. A malformed document can send a scalar/array in that slot; without this guard a
later .to_h/.slice/.dig raises NoMethodError/TypeError -> an unhandled 500. Coerce
a Parameters to a Hash, pass a Hash through, and reject anything else with an ArgumentError
(which the create/update/relationship rescues render as a clean 400).
38 39 40 41 42 43 |
# File 'lib/json_api/support/param_helpers.rb', line 38 def ensure_object!(value, member) return value if value.is_a?(Hash) return value.to_unsafe_h if value.respond_to?(:to_unsafe_h) raise ArgumentError, "'#{member}' must be an object" end |
.esc(val) ⇒ Object
62 63 64 |
# File 'lib/json_api/support/param_helpers.rb', line 62 def esc(val) CGI.escape(val.to_s) end |
.flatten_filter_hash(hash, parent_key = nil, accumulator = {}) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/json_api/support/param_helpers.rb', line 9 def flatten_filter_hash(hash, parent_key = nil, accumulator = {}) hash.each do |key, value| next unless key full_key = parent_key ? "#{parent_key}.#{key}" : key.to_s if value.is_a?(Hash) flatten_filter_hash(value, full_key, accumulator) else accumulator[full_key] = value end end accumulator end |