Module: Fishbans

Extended by:
Fishbans
Includes:
BlockEngine, PlayerSkins
Included in:
Fishbans
Defined in:
lib/fishbans.rb,
lib/block_engine.rb,
lib/player_skins.rb

Defined Under Namespace

Modules: BlockEngine, PlayerSkins

Constant Summary collapse

SERVICES =
[
  'mcbouncer',
  'minebans',
  'glizer',
  'mcblockit',
  'mcbans'
]

Instance Method Summary collapse

Methods included from PlayerSkins

#get_player_front, #get_player_head, #get_player_image, #get_player_skin

Methods included from BlockEngine

#get_block, #get_monster

Instance Method Details

#get(url, do_json = true) ⇒ Hash, HTTP::Message (private)

Performs a basic GET request.

Parameters:

  • url (String)

    The URL to get.

  • do_json (Boolean) (defaults to: true)

    Whether to parse the JSON before returning.

Returns:

  • (Hash)

    The parsed JSON of the response, if do_json is true.

  • (HTTP::Message)

    Unparsed response, if do_json is false.

Raises:

  • (RuntimeError)

    An error if the request is not successful.



101
102
103
104
105
106
107
108
109
# File 'lib/fishbans.rb', line 101

def get(url, do_json = true)
  url = URI.encode(url)
  uri = URI.parse(url)
  response = @client.get(uri)
  return response unless do_json
  json = JSON.parse(response.body)
  return json if json['success']
  fail json['error']
end

#get_all_bans(username) ⇒ Object

Gets all bans on a user.

Parameters:

  • username (String)

    The username to check.

Returns:

  • see #parse_generic_ban_result

Raises:

  • see #get



24
25
26
27
# File 'lib/fishbans.rb', line 24

def get_all_bans(username)
  response = get("http://api.fishbans.com/bans/#{username}")
  parse_generic_ban_result(response)
end

#get_ban_service(username, service) ⇒ Boolean, Hash<String, String>

Gets all bans for a given service for a user.

Parameters:

  • username (String)

    The username to check.

  • service (String)

    The service to check. Can be any of the values in the SERVICES array.

Returns:

  • (Boolean)

    False if the service is not an accepted value.

  • (Hash<String, String>)

    Key: Minecraft server; value: reason

Raises:

  • see #get



35
36
37
38
39
40
41
42
43
44
# File 'lib/fishbans.rb', line 35

def get_ban_service(username, service)
  service.downcase!

  if SERVICES.include? service
    response = get("http://api.fishbans.com/bans/#{username}/#{service}")
    parse_generic_ban_result(response)[service]
  else
    false
  end
end

#get_total_bans(username) ⇒ Integer

Gets the total number of bans that the user has.

Parameters:

  • username (String)

    The username to check.

Returns:

  • (Integer)

    The number of bans the user has.

Raises:

  • see #get



50
51
52
53
# File 'lib/fishbans.rb', line 50

def get_total_bans(username)
  response = get("http://api.fishbans.com/stats/#{username}")
  response['stats']['totalbans']
end

#get_total_bans_service(username, service) ⇒ Boolean, Integer

Gets the total number of bans by service that the user has.

Parameters:

  • username (String)

    The username to check.

  • service (String)

    The service to check. Can be any of the values in the SERVICES array.

Returns:

  • (Boolean)

    False if the service is not an accepted value.

  • (Integer)

    The number of bans the user has in the given service. See SERVICES for valid services.

Raises:

  • see #get



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fishbans.rb', line 62

def get_total_bans_service(username, service)
  service.downcase!

  if SERVICES.include?(service)
    # Note that the /service part is not necessary, but it slightly improves
    #   performance of the API.
    response = get("http://api.fishbans.com/stats/#{username}/#{service}")
    response['stats']['service'][service]
  else
    false
  end
end

#get_userid(username) ⇒ String

Gets the Minecraft UUID for the user.

Parameters:

  • username (String)

    The username to get the ID for.

Returns:

  • (String)

    The user's UUID.

Raises:

  • see #get



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

def get_userid(username)
  response = get("http://api.fishbans.com/uuid/#{username}")
  response['uuid']
end

#get_username_history(username) ⇒ Array<String>

Gets username history for the user.

Parameters:

  • username (String)

    The username to get history for.

Returns:

  • (Array<String>)

    Array of strings containing old usernames.

Raises:

  • see #get



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

def get_username_history(username)
  response = get("http://api.fishbans.com/history/#{username}")
  response['data']['history']
end

#parse_generic_ban_result(response) ⇒ Hash<String, Hash<String, String>> (private)

Parses a basic ban result into a formatted hash. This assumes it is not an errored response.

Parameters:

  • response (JSON)

    The response to parse.

Returns:

  • (Hash<String, Hash<String, String>>)

    Service (see SERVICES) is the key; value is a hash with server as the key, and the reason as the value. Empty hash if there are no bans.



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fishbans.rb', line 115

def parse_generic_ban_result(response)
  ret = {}
  response['bans']['service'].each do |service, info|
    next if info['bans'] == 0
    ret[service] = {}
    info['ban_info'].each do |server, reason|
      ret[service][server] = reason
    end
  end

  ret
end