Class: Brawlstars::Client

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token: false) ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/brawlstars.rb', line 7

def initialize(token: false)
  raise "No authorization token was given!" if !token
  @@token = token
end

Class Method Details

.get(ep) ⇒ Hash

The method to send GET requests to the API

Parameters:

  • ep (String) —

    the endpoint to get.

Returns:

  • (Hash) —

    the response returned by the endpoint.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/brawlstars.rb', line 15

def self.get(ep)
  url = "https://api.brawlapi.cf/v1#{ep}"
  begin
    res = HTTParty.get(url, {headers: {"Authorization" => @@token}})
  rescue HTTParty::Error => e
    puts e
  end
  case res.code
    when 200
      res
    when 400...600
      raise "Error on request; Status code: #{res.code}; Message: #{res["message"]}"
  end
end

Instance Method Details

#about ⇒ Hash

Get info about the API

Returns:

  • (Hash) —

    info about the API



34
35
36
# File 'lib/brawlstars.rb', line 34

def about
  Client.get("/about")
end

#clubSearch(name) ⇒ Hash

Search for clubs by their name

Parameters:

  • name (String) —

    name to search for.

Returns:

  • (Hash) —

    clubs returned by the search.



72
73
74
# File 'lib/brawlstars.rb', line 72

def clubSearch(name)
  Client.get("/club/search?name=#{name.gsub(' ', '%20')}")
end

#getClub(tag) ⇒ Hash

Get a club by its tag

Parameters:

  • tag (String) —

    tag of the club to get.

Returns:

  • (Hash) —

    data of the club that was searched.



62
63
64
65
# File 'lib/brawlstars.rb', line 62

def getClub(tag)
  tag = validateTag(tag)
  Client.get("/club?tag=#{tag}")
end

#getCurrentEvents ⇒ Hash

Get current events

Returns:

  • (Hash) —

    current events



88
89
90
# File 'lib/brawlstars.rb', line 88

def getCurrentEvents
  Client.get("/events?type=current")
end

#getMisc ⇒ Hash

Get miscellaneous data, like shop/season reset

Returns:

  • (Hash) —

    miscellaneous data



96
97
98
# File 'lib/brawlstars.rb', line 96

def getMisc
  Client.get("/misc")
end

#getPlayer(tag) ⇒ Hash

Get a player by their tag

Parameters:

  • tag (String) —

    tag of the player to get.

Returns:

  • (Hash) —

    data of the player that was searched.



43
44
45
46
# File 'lib/brawlstars.rb', line 43

def getPlayer(tag)
  tag = validateTag(tag)
  Client.get("/player?tag=#{tag}")
end

#getTopClubs(count = 200) ⇒ Hash

Get top global clubs

Parameters:

  • count (Integer) (defaults to: 200) —

    number of clubs to return.

Returns:

  • (Hash) —

    clubs in order from 1st rank down.



105
106
107
108
109
# File 'lib/brawlstars.rb', line 105

def getTopClubs(count=200)
  raise 'Count must be a number.' if !count.is_a? Integer
  raise 'Count must be between 1 and 200.' if !count.between?(1,200)
  Client.get("/leaderboards/clubs?count=#{count}")
end

#getTopPlayers(count = 200, brawler = "") ⇒ Hash

Get top global clubs

Parameters:

  • count (Integer) (defaults to: 200) —

    number of clubs to return.

  • brawler (String) (defaults to: "") —

    brawler leaderboard to return.

Returns:

  • (Hash) —

    players in order from 1st rank down.



117
118
119
120
121
# File 'lib/brawlstars.rb', line 117

def getTopPlayers(count=200, brawler="")
  raise 'Count must be a number.' if !count.is_a? Integer
  raise 'Count must be between 1 and 200.' if !count.between?(1,200)
  Client.get("/leaderboards/players?count=#{count}&brawler=#{brawler}")
end

#getUpcomingEvents ⇒ Hash

Get upcoming events

Returns:

  • (Hash) —

    upcoming events



80
81
82
# File 'lib/brawlstars.rb', line 80

def getUpcomingEvents
  Client.get("/events?type=upcoming")
end

#playerSearch(name) ⇒ Hash

Search for players by their name

Parameters:

  • name (String) —

    name to search for.

Returns:

  • (Hash) —

    players returned by the search.



53
54
55
# File 'lib/brawlstars.rb', line 53

def playerSearch(name)
  Client.get("/player/search?name=#{name.gsub(' ', '%20')}")
end