diff options
author | Scott Hain <shain@getchef.com> | 2014-07-14 14:51:53 -0700 |
---|---|---|
committer | Claire McQuin <claire@getchef.com> | 2014-09-04 15:52:23 -0700 |
commit | 47f3b3d93cfd21831debcb4db22ddb7005f28763 (patch) | |
tree | 686982e8d50ddbbb67623cc555f3b84ebfde5ee9 /lib/chef/search | |
parent | 38e7ef5946ba911f8be407cb2d44f6d607ec131f (diff) | |
download | chef-47f3b3d93cfd21831debcb4db22ddb7005f28763.tar.gz |
Updated based on mcq and others feedback
Diffstat (limited to 'lib/chef/search')
-rw-r--r-- | lib/chef/search/query.rb | 91 |
1 files changed, 53 insertions, 38 deletions
diff --git a/lib/chef/search/query.rb b/lib/chef/search/query.rb index c055934709..c93c12082c 100644 --- a/lib/chef/search/query.rb +++ b/lib/chef/search/query.rb @@ -34,9 +34,18 @@ class Chef @rest = Chef::REST.new(url ||Chef::Config[:chef_server_url]) end + + # This search is only kept for backwards compatibility, since the results of the + # new filtered search method will be in a slightly different format + def partial_search(type, query='*:*', *args, &block) + results = search(type,query,args,&block) + + end + + # # New search input, designed to be backwards compatible with the old method signature - # 'type' and 'query' are the same as before, args now will accept either a Hash of + # 'type' and 'query' are the same as before, args now will accept either a Hash of # search arguments with symbols as the keys (ie :sort, :start, :rows) and a :filter_result # option. # @@ -81,50 +90,56 @@ class Chef end private - def escape(s) - s && URI.escape(s.to_s) + def escape(s) + s && URI.escape(s.to_s) + end + + # new search api that allows for a cleaner implementation of things like return filters + # (formerly known as 'partial search'). + # Also args should never be nil, but that is required for Ruby 1.8 compatibility + def do_search(type, query="*:*", args=nil, &block) + raise ArgumentError, "Type must be a string or a symbol!" unless (type.kind_of?(String) || type.kind_of?(Symbol)) + + query_string = create_query_string(type, query, args) + response = call_rest_service(query_string, args) + if !args.nil? && args.key?(:filter_result) + response_rows = response['rows'].map { |row| row['data'] } + else + response_rows = response['rows'] end - # new search api that allows for a cleaner implementation of things like return filters - # (formerly known as 'partial search'). - # Also args should never be nil, but that is required for Ruby 1.8 compatibility - def do_search(type, query="*:*", args=nil, &block) - raise ArgumentError, "Type must be a string or a symbol!" unless (type.kind_of?(String) || type.kind_of?(Symbol)) - - query_string = create_query_string(type, query, args) - response = call_rest_service(query_string, args) - unless block.nil? - response["rows"].each { |rowset| block.call(rowset) unless rowset.nil?} - unless (response["start"] + response["rows"].length) >= response["total"] - args[:start] = response["start"] + args[:rows] - do_search(type, query, args, &block) - end - true - else - [ response["rows"], response["start"], response["total"] ] + unless block.nil? + response_rows.each { |rowset| block.call(rowset) unless rowset.nil?} + unless (response["start"] + response_rows.length) >= response["total"] + args[:start] = response["start"] + args[:rows] + do_search(type, query, args, &block) end - end + true + else + [ response_rows, response["start"], response["total"] ] + end + end - # create the full rest url string - def create_query_string(type, query, args) - # create some default variables just so we don't break backwards compatibility - sort = args[:sort] - start = args[:start] - rows = args[:rows] + # create the full rest url string + def create_query_string(type, query, args) + # create some default variables just so we don't break backwards compatibility + sort = args[:sort] + start = args[:start] + rows = args[:rows] - return "search/#{type}?q=#{escape(query)}&sort=#{escape(sort)}&start=#{escape(start)}&rows=#{escape(rows)}" - end + return "search/#{type}?q=#{escape(query)}&sort=#{escape(sort)}&start=#{escape(start)}&rows=#{escape(rows)}" + end - def call_rest_service(query_string, args) - if args.key?(:filter_result) - response = @rest.post_rest(query_string, args[:filter_result]) - response_rows = response['rows'].map { |row| row['data'] } - else - response = @rest.get_rest(query_string) - response_rows = response['rows'] - end - return response + def call_rest_service(query_string, args) + if args.key?(:filter_result) + response = @rest.post_rest(query_string, args[:filter_result]) + response_rows = response['rows'].map { |row| row['data'] } + else + response = @rest.get_rest(query_string) + response_rows = response['rows'] end + return response + end end end end |