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
- .esc(val) ⇒ Object
- .flatten_filter_hash(hash, parent_key = nil, accumulator = {}) ⇒ Object
Class Method Details
.build_query_parts_for_param(key, value) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/json_api/support/param_helpers.rb', line 41 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
33 34 35 36 37 38 39 |
# File 'lib/json_api/support/param_helpers.rb', line 33 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 |
.esc(val) ⇒ Object
50 51 52 |
# File 'lib/json_api/support/param_helpers.rb', line 50 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 |