Class: DBLRuby::Search

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

Overview

A search for the bots

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search: nil, limit: 50, offset: 0, sort: nil, fields: nil) ⇒ Search

Start a new search

Parameters:

  • limit (Integer) (defaults to: 50)

    The amount of bots to return. Max. 500

  • offset (Integer) (defaults to: 0)

    Amount of bots to skip

  • search (String) (defaults to: nil)

    A search string in the format of field: value field2: value2

  • sort (String) (defaults to: nil)

    The field to sort by. Prefix with - to reverse the order

  • fields (String) (defaults to: nil)

    A comma separated list of fields to show.


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dblruby/search.rb', line 9

def initialize(search: nil, limit: 50, offset: 0, sort: nil, fields: nil)
  url = 'https://discordbots.org/api/bots'
  @search = search
  @limit = limit
  @offset = offset
  @sort = sort
  @fields = fields
  @data = JSON.parse(RestClient.get(url, params: { search: search,
                                                   limit: limit,
                                                   offset: offset,
                                                   sort: sort,
                                                   fields: fields }))
end

Instance Attribute Details

#dataObject (readonly) Also known as: to_s

Returns data in raw json form.

Returns:

  • data in raw json form.


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

def data
  @data
end

#fieldsString (readonly)

Returns the fields query.

Returns:

  • (String)

    the fields query


41
42
43
# File 'lib/dblruby/search.rb', line 41

def fields
  @fields
end

#limitInteger (readonly)

Returns the limit query.

Returns:

  • (Integer)

    the limit query


32
33
34
# File 'lib/dblruby/search.rb', line 32

def limit
  @limit
end

#offsetInteger (readonly)

Returns the offset query.

Returns:

  • (Integer)

    the offset query


35
36
37
# File 'lib/dblruby/search.rb', line 35

def offset
  @offset
end

#searchString (readonly)

Returns the search query.

Returns:

  • (String)

    the search query


29
30
31
# File 'lib/dblruby/search.rb', line 29

def search
  @search
end

#sortString (readonly)

Returns the sort query.

Returns:

  • (String)

    the sort query


38
39
40
# File 'lib/dblruby/search.rb', line 38

def sort
  @sort
end

Instance Method Details

#countInteger

Get the bot count in this result

Returns:

  • (Integer)

    total bots returned

See Also:


83
84
85
# File 'lib/dblruby/search.rb', line 83

def count
  @data['count']
end

#firstBot

Return only the first bot

Returns:

  • (Bot)

    the first bot.


64
65
66
# File 'lib/dblruby/search.rb', line 64

def first
  DBLRuby::Bot.new(data: @data['results'][0])
end

#nextSearch

Get the next page

Returns:

  • (Search)

    next page of results


70
71
72
73
74
75
76
77
78
# File 'lib/dblruby/search.rb', line 70

def next
  DBLRuby::Search.new(
    search: search,
    limit: limit,
    offset: limit + offset,
    sort: sort,
    fields: fields
  )
end

#resultsArray<Bot>

Gets all the bots

Returns:

  • (Array<Bot>)

    a list of the bots


52
53
54
55
56
57
58
59
60
# File 'lib/dblruby/search.rb', line 52

def results
  output = Array.new(size)
  cur = 0
  @data['results'].each do |e|
    output[cur] = DBLRuby::Bot.new(data: e)
    cur += 1
  end
  output
end

#sizeString

The amount of results returned

Returns:

  • (String)

    The amount of bots

See Also:


46
47
48
# File 'lib/dblruby/search.rb', line 46

def size
  @data['results'].length
end

#totalInteger

Get the total possible amount of bots there are for this query

Returns:

  • (Integer)

    the total amount of bots


89
90
91
# File 'lib/dblruby/search.rb', line 89

def total
  @data['total']
end