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
36 37 38 39 40 41 |
# File 'lib/muster/strategies/rack.rb', line 36 def initialize( ={} ) @options = .with_indifferent_access @fields = Array.wrap(@options[:field] || @options[:fields]) @fields.map!{ |field| field.to_sym } end |
Instance Attribute Details
#fields ⇒ Hash (readonly)
Returns list of fields to parse, ignoring all others.
24 25 26 |
# File 'lib/muster/strategies/rack.rb', line 24 def fields @fields end |
#options ⇒ Hash (readonly)
Returns options specified during initialization.
20 21 22 |
# File 'lib/muster/strategies/rack.rb', line 20 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.
78 79 80 81 82 83 84 85 86 |
# File 'lib/muster/strategies/rack.rb', line 78 def fields_to_parse( query_string ) fields = self.parse_query_string(query_string) if self.fields.present? fields = fields.select{ |key, value| self.fields.include?(key.to_sym) } end return fields.with_indifferent_access end |
#parse(query_string) ⇒ Muster::Results
Processes a query string and returns a hash of its fields/values
52 53 54 |
# File 'lib/muster/strategies/rack.rb', line 52 def parse( query_string ) Muster::Results.new( self.fields_to_parse(query_string) ) end |
#parse_query_string(query_string) ⇒ Hash (protected)
Converts the query string into a hash for processing
67 68 69 |
# File 'lib/muster/strategies/rack.rb', line 67 def parse_query_string( query_string ) ::Rack::Utils.parse_query(query_string).with_indifferent_access end |