class Brawlstars::Client

Public Class Methods

new(token: false) click to toggle source
# File lib/brawlstars.rb, line 43
def initialize(token: false)
  raise "No authorization token was given!" if !token
  @token = token
end

Public Instance Methods

about() click to toggle source

Get info about the API

@return [Hash] info about the API

# File lib/brawlstars.rb, line 52
def about
  get("/about")
end
clubSearch(name) click to toggle source

Search for clubs by their name

@param name [String] name to search for. @return [Hash] clubs returned by the search.

# File lib/brawlstars.rb, line 100
def clubSearch(name)
  get("/club/search?name=#{name.gsub(' ', '%20')}")
end
getBattleLog(tag) click to toggle source

Get a player's battle log by their tag

@param tag [String] tag of the player to get. @return [Hash] data of the player's battle log that was searched.

# File lib/brawlstars.rb, line 80
def getBattleLog(tag)
  tag = validateTag(tag)
  get("/player/battlelog?tag=#{tag}")
end
getClub(tag) click to toggle source

Get a club by its tag

@param tag [String] tag of the club to get. @return [Hash] data of the club that was searched.

# File lib/brawlstars.rb, line 90
def getClub(tag)
  tag = validateTag(tag)
  get("/club?tag=#{tag}")
end
getCurrentEvents() click to toggle source

Get current events

@return [Hash] current events

# File lib/brawlstars.rb, line 116
def getCurrentEvents
  get("/events?type=current")
end
getMisc() click to toggle source

Get miscellaneous data, like shop/season reset

@return [Hash] miscellaneous data

# File lib/brawlstars.rb, line 124
def getMisc
  get("/misc")
end
getPlayer(tag) click to toggle source

Get a player by their tag

@param tag [String] tag of the player to get. @return [Brawlstars::Player] data of the player that was searched.

# File lib/brawlstars.rb, line 61
def getPlayer(tag)
  tag = validateTag(tag)
  Player.new(self, get("/player?tag=#{tag}"))
end
getTopClubs(count=200, region="global") click to toggle source

Get top global clubs

@param count [Integer] number of clubs to return. @param region [String] 2 character country code for the region of leaderboard to return. @return [Hash] clubs in order from 1st rank down.

# File lib/brawlstars.rb, line 134
def getTopClubs(count=200, region="global")
  raise 'Count must be a number.' if !count.is_a? Integer
  raise 'Count must be between 1 and 200.' if !count.between?(1,200)
  validateRegion(region)
  get("/leaderboards/clubs?count=#{count}&region=#{region}")
end
getTopPlayers(count=200, brawler="", region="global") click to toggle source

Get top global players

@param count [Integer] number of players to return. @param brawler [String] brawler leaderboard to return. @param region [String] 2 character country code for the region of leaderboard to return. @return [Hash] players in order from 1st rank down.

# File lib/brawlstars.rb, line 148
def getTopPlayers(count=200, brawler="", region="global")
  raise 'Count must be a number.' if !count.is_a? Integer
  raise 'Count must be between 1 and 200.' if !count.between?(1,200)
  validateRegion(region)
  get("/leaderboards/players?count=#{count}&brawler=#{brawler}&region=#{region}")
end
getUpcomingEvents() click to toggle source

Get upcoming events

@return [Hash] upcoming events

# File lib/brawlstars.rb, line 108
def getUpcomingEvents
  get("/events?type=upcoming")
end
playerSearch(name) click to toggle source

Search for players by their name

@param name [String] name to search for. @return [Hash] players returned by the search.

# File lib/brawlstars.rb, line 71
def playerSearch(name)
  get("/player/search?name=#{name.gsub(' ', '%20')}")
end

Protected Instance Methods

get(ep) click to toggle source

The method to send GET requests to the API

@param ep [String] the endpoint to get. @return [Hash] the response returned by the endpoint.

# File lib/brawlstars.rb, line 162
def get(ep)
  url = "https://api.starlist.pro/v1#{ep}"
  begin
    res = HTTParty.get(url, {headers: {"Authorization" => @token}})
  rescue HTTParty::Error => e
    puts e
  end
  case res.code
    when 200
      res.parsed_response
    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