Class: DBLRuby::Search
- Inherits:
-
Object
- Object
- DBLRuby::Search
- Defined in:
- lib/dblruby/search.rb
Overview
A search for the bots
Instance Attribute Summary collapse
-
#data ⇒ Object
(also: #to_s)
readonly
Data in raw json form.
-
#fields ⇒ String
readonly
The fields query.
-
#limit ⇒ Integer
readonly
The limit query.
-
#offset ⇒ Integer
readonly
The offset query.
-
#search ⇒ String
readonly
The search query.
-
#sort ⇒ String
readonly
The sort query.
Instance Method Summary collapse
-
#count ⇒ Integer
Get the bot count in this result.
-
#first ⇒ Bot
Return only the first bot.
-
#initialize(search: nil, limit: 50, offset: 0, sort: nil, fields: nil) ⇒ Search
constructor
Start a new search.
-
#next ⇒ Search
Get the next page.
-
#results ⇒ Array<Bot>
Gets all the bots.
-
#size ⇒ String
The amount of results returned.
-
#total ⇒ Integer
Get the total possible amount of bots there are for this query.
Constructor Details
#initialize(search: nil, limit: 50, offset: 0, sort: nil, fields: nil) ⇒ Search
Start a new search
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
#data ⇒ Object (readonly) Also known as: to_s
Returns data in raw json form.
24 25 26 |
# File 'lib/dblruby/search.rb', line 24 def data @data end |
#fields ⇒ String (readonly)
Returns the fields query.
41 42 43 |
# File 'lib/dblruby/search.rb', line 41 def fields @fields end |
#limit ⇒ Integer (readonly)
Returns the limit query.
32 33 34 |
# File 'lib/dblruby/search.rb', line 32 def limit @limit end |
#offset ⇒ Integer (readonly)
Returns the offset query.
35 36 37 |
# File 'lib/dblruby/search.rb', line 35 def offset @offset end |
#search ⇒ String (readonly)
Returns the search query.
29 30 31 |
# File 'lib/dblruby/search.rb', line 29 def search @search end |
#sort ⇒ String (readonly)
Returns the sort query.
38 39 40 |
# File 'lib/dblruby/search.rb', line 38 def sort @sort end |
Instance Method Details
#count ⇒ Integer
Get the bot count in this result
83 84 85 |
# File 'lib/dblruby/search.rb', line 83 def count @data['count'] end |
#first ⇒ Bot
Return only the first bot
64 65 66 |
# File 'lib/dblruby/search.rb', line 64 def first DBLRuby::Bot.new(data: @data['results'][0]) end |
#next ⇒ Search
Get the next page
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 |
#results ⇒ Array<Bot>
Gets all 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 |
#size ⇒ String
The amount of results returned
46 47 48 |
# File 'lib/dblruby/search.rb', line 46 def size @data['results'].length end |
#total ⇒ Integer
Get the total possible amount of bots there are for this query
89 90 91 |
# File 'lib/dblruby/search.rb', line 89 def total @data['total'] end |