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.



38
39
40
41
# File 'lib/brawlstars.rb', line 38

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.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/brawlstars.rb', line 48

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 401
      raise Error::Unauthorized
    when 404
      raise Error::NotFoundError
    when 429
      raise Error::RateLimitError
    when 503
      raise Error::MaintainanceError
    when 500...600
      raise Error::ServerError
  end
end

Instance Method Details

#aboutHash

Get info about the API

Returns:

  • (Hash)

    info about the API



75
76
77
# File 'lib/brawlstars.rb', line 75

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.



113
114
115
# File 'lib/brawlstars.rb', line 113

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.



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

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

#getCurrentEventsHash

Get current events

Returns:

  • (Hash)

    current events



129
130
131
# File 'lib/brawlstars.rb', line 129

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

#getMiscHash

Get miscellaneous data, like shop/season reset

Returns:

  • (Hash)

    miscellaneous data



137
138
139
# File 'lib/brawlstars.rb', line 137

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.



84
85
86
87
# File 'lib/brawlstars.rb', line 84

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.



146
147
148
149
150
# File 'lib/brawlstars.rb', line 146

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 players

Parameters:

  • count (Integer) (defaults to: 200)

    number of players to return.

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

    brawler leaderboard to return.

Returns:

  • (Hash)

    players in order from 1st rank down.



158
159
160
161
162
# File 'lib/brawlstars.rb', line 158

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

#getUpcomingEventsHash

Get upcoming events

Returns:

  • (Hash)

    upcoming events



121
122
123
# File 'lib/brawlstars.rb', line 121

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.



94
95
96
# File 'lib/brawlstars.rb', line 94

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