diff options
Diffstat (limited to 'lib/chef/search/query.rb')
-rw-r--r-- | lib/chef/search/query.rb | 79 |
1 files changed, 37 insertions, 42 deletions
diff --git a/lib/chef/search/query.rb b/lib/chef/search/query.rb index c055934709..9be4b07062 100644 --- a/lib/chef/search/query.rb +++ b/lib/chef/search/query.rb @@ -36,7 +36,7 @@ class Chef # # 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. # @@ -44,7 +44,7 @@ class Chef # { # :returned_name1 => ["path", "to", "variable"], # :returned_name2 => ["shorter", "path"] - # } + # } # a real world example might be something like: # { # :ip_address => ["ipaddress"], @@ -53,7 +53,7 @@ class Chef # this will bring back 2 variables 'ip_address' and 'ruby_version' with whatever value was found # an example of the returned json may be: # {"ip_address":"127.0.0.1", "ruby_version": "1.9.3"} - # + # def search(type, query='*:*', *args, &block) raise ArgumentError, "Type must be a string or a symbol!" unless (type.kind_of?(String) || type.kind_of?(Symbol)) raise ArgumentError, "Invalid number of arguments!" if (args.size > 3) @@ -81,50 +81,45 @@ class Chef end private - 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) - 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"] ] - end - end + def escape(s) + s && URI.escape(s.to_s) + 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] + # 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)) - return "search/#{type}?q=#{escape(query)}&sort=#{escape(sort)}&start=#{escape(start)}&rows=#{escape(rows)}" + query_string = create_query_string(type, query, args) + if !args.nil? && 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 - - 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'] + 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 - return response + 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] + + return "search/#{type}?q=#{escape(query)}&sort=#{escape(sort)}&start=#{escape(start)}&rows=#{escape(rows)}" + end end end end |