Class: DBLRuby

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

Overview

All DBLRuby functionality, whether extended or just here.

Defined Under Namespace

Modules: Errors Classes: Bot, Search, Stats, User, Weekend, Widget

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apikey, id) ⇒ DBLRuby

Initialize a new DBL API, via a key.

Parameters:

  • apikey (String)

    API Key of the bot, taken from the DBL.

  • id (Integer, String)

    Integer/String of the bot's id.


10
11
12
13
# File 'lib/dblruby.rb', line 10

def initialize(apikey, id)
  @api = apikey
  @id = id
end

Instance Attribute Details

#apiObject Also known as: apikey

Get the API Key from instantiation


106
107
108
# File 'lib/dblruby.rb', line 106

def api
  @api
end

#idObject

Get the ID from instantiation


103
104
105
# File 'lib/dblruby.rb', line 103

def id
  @id
end

Instance Method Details

#bot(id) ⇒ Bot Also known as: loadbot

Load a bot.

Parameters:

  • id (Integer, String)

    Integer/String ID of the bot you're requesting.

Returns:

  • (Bot)

    the new Bot object

Raises:


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dblruby.rb', line 25

def bot(id)
  url = "https://top.gg/api/bots/#{id}"
  data = JSON.parse(RestClient.get(url, Authorization: api))
  Bot.new(data)
rescue RestClient::Unauthorized
  raise DBLRuby::Errors::InvalidAPIKey,
        'The API returned a 401 error, I believe your token is invalid.'
rescue RestClient::NotFound
  raise DBLRuby::Errors::InvalidBot,
        'The API returned a 404 error! Is that bot listed?'
end

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

Start a search

Returns:

  • (Search)

    the new search object

See Also:


93
94
95
# File 'lib/dblruby.rb', line 93

def search(search: nil, limit: 50, offset: 0, sort: nil, fields: nil)
  Search.new(search: search, limit: limit, offset: offset, sort: sort, fields: fields)
end

#selfBot

Get all of the bot's stats as if you just called DBL.bot(id)

Returns:

  • (Bot)

    the bot as a bot.


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

def self
  bot(@id)
end

#statsStats

Initialize stats

Returns:

  • (Stats)

    the new stats object


17
18
19
# File 'lib/dblruby.rb', line 17

def stats
  Stats.new(@api, @id)
end

#user(id) ⇒ User Also known as: loaduser

Load a user

Parameters:

  • id (Integer, String)

    Integer/String ID of the user you're requesting.

Returns:

  • (User)

    the new user object

Raises:


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dblruby.rb', line 49

def user(id)
  url = "https://top.gg/api/users/#{id}"
  data = JSON.parse(RestClient.get(url, Authorization: api))
  User.new(data)
rescue RestClient::Unauthorized
  raise DBLRuby::Errors::InvalidAPIKey,
        'The API returned a 401 error, I believe your token is invalid.'
rescue RestClient::NotFound
  raise DBLRuby::Errors::InvalidBot,
        'The API returned a 404 error! Is that bot listed?'
end

#weekendWeekend

The Weekend endpoint only really tells us if it's a weekend

Returns:

  • (Weekend)

    the new Weekend object

See Also:

  • for a simplier version.

78
79
80
81
# File 'lib/dblruby.rb', line 78

def weekend
  url = 'https://top.gg/api/weekend'
  Weekend.new JSON.parse(RestClient.get(url))
end

#weekend?true, false

Returns true or false depending on if it's the “weekend” Weekend counts as 2 votes instead of one.

Returns:

  • (true, false)

    whether it's the weekend or not


86
87
88
# File 'lib/dblruby.rb', line 86

def weekend?
  weekend.weekend?
end

#widget(size: 'large', file_type: 'png', small_type: nil, topcolor: nil, middlecolor: nil, usernamecolor: nil, certifiedcolor: nil, datacolor: nil, labelcolor: nil, highlightcolor: nil, avatarbg: nil, leftcolor: nil, rightcolor: nil, lefttextcolor: nil, righttextcolor: nil, id: @id) ⇒ Object


98
99
100
# File 'lib/dblruby.rb', line 98

def widget(size: 'large', file_type: 'png', small_type: nil, topcolor: nil, middlecolor: nil, usernamecolor: nil, certifiedcolor: nil, datacolor: nil, labelcolor: nil, highlightcolor: nil, avatarbg: nil, leftcolor: nil, rightcolor: nil, lefttextcolor: nil, righttextcolor: nil, id: @id)
  Widget.new(size: size, file_type: file_type, small_type: small_type, topcolor: topcolor, middlecolor: middlecolor, usernamecolor: usernamecolor, certifiedcolor: certifiedcolor, datacolor: datacolor, labelcolor: labelcolor, highlightcolor: highlightcolor, avatarbg: avatarbg, leftcolor: leftcolor, rightcolor: rightcolor, lefttextcolor: lefttextcolor, righttextcolor: righttextcolor, id: id)
end