Class: Muster::Strategies::Rack
- Inherits:
-
Object
- Object
- Muster::Strategies::Rack
- Defined in:
- lib/muster/strategies/rack.rb
Overview
Query string parsing strategy based on Rack::Utils#parse_query
Direct Known Subclasses
Instance Attribute Summary collapse
-
#fields ⇒ Hash
readonly
List of fields to parse, ignoring all others.
-
#options ⇒ Hash
readonly
Options specified during initialization.
Instance Method Summary collapse
-
#fields_to_parse(query_string) ⇒ Hash
protected
Returns the list of fields to be parsed.
-
#initialize(options = {}) ⇒ Rack
constructor
Create a new Rack parsing strategy.
-
#parse(query_string) ⇒ Muster::Results
Processes a query string and returns a hash of its fields/values.
-
#parse_query_string(query_string) ⇒ Hash
protected
Converts the query string into a hash for processing.
Constructor Details
#initialize(options = {}) ⇒ Rack
Create a new Rack parsing strategy
34 35 36 37 38 39 |
# File 'lib/muster/strategies/rack.rb', line 34 def initialize( = {}) @options = .with_indifferent_access @fields = Array.wrap(@options[:field] || @options[:fields]) @fields.map!(&:to_sym) end |
Instance Attribute Details
#fields ⇒ Hash (readonly)
Returns list of fields to parse, ignoring all others.
22 23 24 |
# File 'lib/muster/strategies/rack.rb', line 22 def fields @fields end |
#options ⇒ Hash (readonly)
Returns options specified during initialization.
18 19 20 |
# File 'lib/muster/strategies/rack.rb', line 18 def @options end |
Instance Method Details
#fields_to_parse(query_string) ⇒ Hash (protected)
Returns the list of fields to be parsed
If the :fields option was specified, only those fields will be returned. Otherwise, all fields will be returned.
76 77 78 79 80 81 82 83 84 |
# File 'lib/muster/strategies/rack.rb', line 76 def fields_to_parse(query_string) fields = parse_query_string(query_string) if self.fields.present? fields = fields.select { |key, _| self.fields.include?(key.to_sym) } end fields.with_indifferent_access end |
#parse(query_string) ⇒ Muster::Results
Processes a query string and returns a hash of its fields/values
50 51 52 |
# File 'lib/muster/strategies/rack.rb', line 50 def parse(query_string) Muster::Results.new(fields_to_parse(query_string)) end |
#parse_query_string(query_string) ⇒ Hash (protected)
Converts the query string into a hash for processing
65 66 67 |
# File 'lib/muster/strategies/rack.rb', line 65 def parse_query_string(query_string) ::Rack::Utils.parse_query(query_string).with_indifferent_access end |