Class: Ridley::SearchResource
- Defined in:
- lib/ridley/resources/search_resource.rb
Class Method Summary collapse
-
.build_param_string(query_string, options = {}) ⇒ String
Builds and returns a query parameter string for the search API.
- .build_query(query_string, options = {}) ⇒ Hash
- .query_uri(index) ⇒ String
Instance Method Summary collapse
-
#indexes ⇒ Array<String, Symbol>
Returns an array of possible search indexes to be search on.
-
#partial(index, query_string, attributes, resources_registry, options = {}) ⇒ Array<ChefObject>, Hash
Perform a partial search on the Chef server.
-
#run(index, query_string, resources_registry, options = {}) ⇒ Array<ChefObject>, Hash
Executes the built up query on the search’s client.
Methods inherited from Resource
#all, #connection, #create, #delete, #delete_all, #find, #from_file, #from_json, #initialize, #new, representation, represented_by, resource_path, set_resource_path, #update
Constructor Details
This class inherits a constructor from Ridley::Resource
Class Method Details
.build_param_string(query_string, options = {}) ⇒ String
Builds and returns a query parameter string for the search API
38 39 40 41 42 43 44 45 |
# File 'lib/ridley/resources/search_resource.rb', line 38 def build_param_string(query_string, = {}) query = build_query(query_string, ) param = "?q=#{escape(query[:q])}" param += "&sort=#{escape(query[:sort])}" if query[:sort] param += "&start=#{escape(query[:start])}" if query[:start] param += "&rows=#{escape(query[:rows])}" if query[:rows] param end |
.build_query(query_string, options = {}) ⇒ Hash
14 15 16 17 18 19 20 21 |
# File 'lib/ridley/resources/search_resource.rb', line 14 def build_query(query_string, = {}) {}.tap do |query_opts| query_opts[:q] = query_string unless query_string.nil? query_opts[:sort] = [:sort] unless [:sort].nil? query_opts[:rows] = [:rows] unless [:rows].nil? query_opts[:start] = [:start] unless [:start].nil? end end |
.query_uri(index) ⇒ String
50 51 52 |
# File 'lib/ridley/resources/search_resource.rb', line 50 def query_uri(index) "#{resource_path}/#{index}" end |
Instance Method Details
#indexes ⇒ Array<String, Symbol>
Returns an array of possible search indexes to be search on
72 73 74 |
# File 'lib/ridley/resources/search_resource.rb', line 72 def indexes request(:get, self.class.resource_path).collect { |name, _| name } end |
#partial(index, query_string, attributes, resources_registry, options = {}) ⇒ Array<ChefObject>, Hash
Perform a partial search on the Chef server
131 132 133 134 135 136 137 |
# File 'lib/ridley/resources/search_resource.rb', line 131 def partial(index, query_string, attributes, resources_registry, = {}) query_uri = self.class.query_uri(index) param_string = self.class.build_param_string(query_string, ) body = build_partial_body(index, attributes) handle_partial(index, resources_registry, request(:post, "#{query_uri}#{param_string}", JSON.generate(body))) end |
#run(index, query_string, resources_registry, options = {}) ⇒ Array<ChefObject>, Hash
Executes the built up query on the search’s client
109 110 111 112 113 114 |
# File 'lib/ridley/resources/search_resource.rb', line 109 def run(index, query_string, resources_registry, = {}) query_uri = self.class.query_uri(index) query = self.class.build_query(query_string, ) handle_response(index, resources_registry, request(:get, query_uri, query)) end |