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(connection, index, query, options = {}) ⇒ Search

Returns a new instance of Search.

Parameters:

  • connection (Ridley::Connection)
  • 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
45
# File 'lib/ridley/resources/search.rb', line 37

def initialize(connection, index, query, options = {})
  @connection = connection
  @index      = index.to_sym
  @query      = query

  @sort       = options[:sort]
  @rows       = options[:rows]
  @start      = options[:start]
end

Instance Attribute Details

#connectionRidley::Connection (readonly)

Returns:



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

def connection
  @connection
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(connection) ⇒ Array<String, Symbol>

Returns an array of possible search indexes to be search on

Examples:


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

Parameters:

Returns:

  • (Array<String, Symbol>)


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

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

Instance Method Details

#runHash

Executes the built up query on the search’s connection

Examples:

Search.new(connection, :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)


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

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

  case index
  when :node
    response[:rows].collect { |row| Node.new(connection, row) }
  when :role
    response[:rows].collect { |row| Role.new(connection, row) }
  when :client
    response[:rows].collect { |row| Client.new(connection, row) }
  when :environment
    response[:rows].collect { |row| Environment.new(connection, row) }
  else
    response[:rows]
  end
end