Class: Ridley::SearchResource

Inherits:
Resource
  • Object
show all
Defined in:
lib/ridley/resources/search_resource.rb

Class Method Summary collapse

Instance Method Summary collapse

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

Examples:

build_param_string("*:*", rows: 5) #=> "?q=*:*&rows=5"

Parameters:

  • query_string (String)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :sort (String)

    a sort string such as ‘name DESC’

  • :rows (Integer)

    how many rows to return

  • :start (Integer)

    the result number to start from

Returns:

  • (String)


38
39
40
41
42
43
44
45
# File 'lib/ridley/resources/search_resource.rb', line 38

def build_param_string(query_string, options = {})
  query = build_query(query_string, options)
  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

Parameters:

  • query_string (String)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :sort (String)

    a sort string such as ‘name DESC’

  • :rows (Integer)

    how many rows to return

  • :start (Integer)

    the result number to start from

Returns:

  • (Hash)


14
15
16
17
18
19
20
21
# File 'lib/ridley/resources/search_resource.rb', line 14

def build_query(query_string, options = {})
  {}.tap do |query_opts|
    query_opts[:q]     = query_string unless query_string.nil?
    query_opts[:sort]  = options[:sort] unless options[:sort].nil?
    query_opts[:rows]  = options[:rows] unless options[:rows].nil?
    query_opts[:start] = options[:start] unless options[:start].nil?
  end
end

.query_uri(index) ⇒ String

Parameters:

  • index (#to_s)

Returns:

  • (String)


50
51
52
# File 'lib/ridley/resources/search_resource.rb', line 50

def query_uri(index)
  "#{resource_path}/#{index}"
end

Instance Method Details

#indexesArray<String, Symbol>

Returns an array of possible search indexes to be search on

Examples:


Search.indexes(client) => [ :client, :environment, :node, :role ]

Parameters:

Returns:

  • (Array<String, Symbol>)


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

Parameters:

  • index (#to_sym, #to_s)
  • query_string (#to_s)
  • attributes (Array)

    an array of strings in dotted hash notation representing the attributes to return

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :sort (String)

    a sort string such as ‘name DESC’

  • :rows (Integer)

    how many rows to return

  • :start (Integer)

    the result number to start from

Returns:



131
132
133
134
135
136
137
# File 'lib/ridley/resources/search_resource.rb', line 131

def partial(index, query_string, attributes, resources_registry, options = {})
  query_uri    = self.class.query_uri(index)
  param_string = self.class.build_param_string(query_string, options)
  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

Examples:

Search.new(client, :role)
search.run =>
  {
    total: 1,
    start: 0,
    rows: [
      {
        name: "ridley-test-role",
        default_attributes: {},
        json_class: "Chef::Role",
        env_run_lists: {},
        run_list: [],
        description: "a test role for Ridley!",
        chef_type: "role",
        override_attributes: {}
      }
    ]
  }

Parameters:

  • index (#to_sym, #to_s)
  • query_string (#to_s)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :sort (String)

    a sort string such as ‘name DESC’

  • :rows (Integer)

    how many rows to return

  • :start (Integer)

    the result number to start from

Returns:



109
110
111
112
113
114
# File 'lib/ridley/resources/search_resource.rb', line 109

def run(index, query_string, resources_registry, options = {})
  query_uri = self.class.query_uri(index)
  query     = self.class.build_query(query_string, options)

  handle_response(index, resources_registry, request(:get, query_uri, query))
end