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, #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_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)


26
27
28
# File 'lib/ridley/resources/search_resource.rb', line 26

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>)


42
43
44
# File 'lib/ridley/resources/search_resource.rb', line 42

def indexes
  request(:get, self.class.resource_path).collect { |name, _| name }
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:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ridley/resources/search_resource.rb', line 79

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

  case index.to_sym
  when :node
    response[:rows].collect { |row| Ridley::NodeObject.new(resources_registry[:node_resource], row) }
  when :role
    response[:rows].collect { |row| Ridley::RoleObject.new(resources_registry[:role_resource], row) }
  when :client
    response[:rows].collect { |row| Ridley::ClientObject.new(resources_registry[:client_resource], row) }
  when :environment
    response[:rows].collect { |row| Ridley::EnvironmentObject.new(resources_registry[:environment_resource], row) }
  else
    response[:rows]
  end
end