forked from swiftype/swiftype-rb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update accepted params and json encode post body for searches
- Loading branch information
Showing
1 changed file
with
7 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,23 @@ | ||
module Swiftype | ||
module Search | ||
|
||
VALID_SEARCH_OPTIONS = [:fetch_fields, :search_fields, :filters, :per_page, :page, :document_types, :functional_boosts] | ||
VALID_SUGGEST_OPTIONS = [:fetch_fields, :search_fields, :filters, :document_types, :functional_boosts] | ||
VALID_SUGGEST_OPTIONS = [:fetch_fields, :search_fields, :filters, :document_types, :functional_boosts, :page, :per_page] | ||
VALID_SEARCH_OPTIONS = [:fetch_fields, :search_fields, :filters, :document_types, :functional_boosts, :page, :per_page, :sort_field, :sort_direction] | ||
|
||
def parse_search_options(options) | ||
parse_options(options,VALID_SEARCH_OPTIONS) | ||
parse_options(options, VALID_SEARCH_OPTIONS) | ||
end | ||
|
||
def parse_suggest_options(options) | ||
parse_options(options,VALID_SUGGEST_OPTIONS) | ||
parse_options(options, VALID_SUGGEST_OPTIONS) | ||
end | ||
|
||
def parse_options(options,valid_options) | ||
def parse_options(options, valid_options) | ||
parsed_options = {} | ||
valid_options.each do |option_name| | ||
next unless options[option_name] | ||
encode_single_option(parsed_options,option_name,options[option_name]) | ||
next unless options[option_name] | ||
parsed_options[option_name] = options[option_name] | ||
end | ||
parsed_options | ||
end | ||
|
||
# recursive method to encode arrays, hashes, and values the 'Rails' way, to make server-side parsing simpler | ||
def encode_single_option(parsed_options,key,value,prefix='') | ||
prefix = key if prefix.empty? | ||
if value.instance_of?(Hash) | ||
value.each { |k,v| encode_single_option(parsed_options,k,v,"#{prefix}[#{k}]") } | ||
elsif value.instance_of?(Array) | ||
parsed_options["#{prefix}[]"] = value | ||
else | ||
parsed_options["#{prefix}"] = value | ||
end | ||
end | ||
|
||
end | ||
end |