Class: Ridley::Search

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, index, query, options = {}) ⇒ Search

Returns a new instance of Search.

Parameters:

  • client (Ridley::Client)
  • index (#to_sym)
  • query (#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



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

def initialize(client, index, query, options = {})
  @client = client
  @index  = index.to_sym
  @query  = query
  @sort   = options[:sort]
  @rows   = options[:rows]
  @start  = options[:start]
end

Instance Attribute Details

#clientRidley::Client (readonly)

Returns:



19
20
21
# File 'lib/ridley/resources/search.rb', line 19

def client
  @client
end

#indexObject (readonly)

Returns the value of attribute index.



20
21
22
# File 'lib/ridley/resources/search.rb', line 20

def index
  @index
end

#queryObject (readonly)

Returns the value of attribute query.



21
22
23
# File 'lib/ridley/resources/search.rb', line 21

def query
  @query
end

#rowsObject

Returns the value of attribute rows.



24
25
26
# File 'lib/ridley/resources/search.rb', line 24

def rows
  @rows
end

#sortObject

Returns the value of attribute sort.



23
24
25
# File 'lib/ridley/resources/search.rb', line 23

def sort
  @sort
end

#startObject

Returns the value of attribute start.



25
26
27
# File 'lib/ridley/resources/search.rb', line 25

def start
  @start
end

Class Method Details

.indexes(client) ⇒ Array<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>)


13
14
15
# File 'lib/ridley/resources/search.rb', line 13

def indexes(client)
  client.connection.get("search").body.collect { |name, _| name }
end

Instance Method Details

#runHash

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: {}
      }
    ]
  }

Returns:

  • (Hash)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ridley/resources/search.rb', line 69

def run
  response = client.connection.get(query_uri, query_options).body

  case index
  when :node
    response[:rows].collect { |row| Ridley::NodeResource.new(client, row) }
  when :role
    response[:rows].collect { |row| Ridley::RoleResource.new(client, row) }
  when :client
    response[:rows].collect { |row| Ridley::ClientResource.new(client, row) }
  when :environment
    response[:rows].collect { |row| Ridley::EnvironmentResource.new(client, row) }
  else
    response[:rows]
  end
end