Module: Mojang

Defined in:
lib/mojang.rb,
lib/errors.rb

Overview

Wrapper module around the Mojang and Minecraft web APIs.

Defined Under Namespace

Modules: Errors

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.name_history(uuid) ⇒ Hash<Symbol/Time, String>

Gets a user's name history from their UUID. the name was changed. Value is always the name at that point in time.

Parameters:

  • uuid (String)

    The user's ID (see #userid).

Returns:

  • (Hash<Symbol/Time, String>)

    A hash of all the names. Key is either :original, or the Time object of when

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mojang.rb', line 53

def name_history(uuid)
  response = Curl.get("https://api.mojang.com/user/profiles/#{uuid}/names").body_str
  json = Oj.load(response)
  if json.key?('error')
    fail Mojang::Errors::MojangError.new(json['error'], json['errorMessage'])
  end
  ret = {}
  json.each do |hash|
    if hash.key?('changedToAt')
      ret[Time.at(hash['changedToAt'] / 1000)] = hash['name']
    else
      ret[:original] = hash['name']
    end
  end

  ret
end

.statusHash<String, String>

Gets the status for the various Mojang and Minecraft servers and web services.

Returns:

  • (Hash<String, String>)

    A hash containing keys of the sites, and values of the statuses (green, yellow, red).



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mojang.rb', line 11

def status
  response = Curl.get('https://status.mojang.com/check'.freeze).body_str
  json = Oj.load(response)
  ret = {}
  # Reformatting the returned data because it is super annoying to work with.
  json.each do |hash|
    hash.each do |site, status|
      ret[site] = status
    end
  end

  ret
end

.userid(username, date = nil) ⇒ String

Gets the User ID (UUID) for the given username at the given time.

Parameters:

  • username (String)

    The username.

  • date (Date) (defaults to: nil)

    The date to get the ID at.

Returns:

  • (String)

    The username's user ID.

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mojang.rb', line 31

def userid(username, date = nil)
  profile_str = "https://api.mojang.com/users/profiles/minecraft/#{username}"
  # If the provided date (or 0, if not provided) does not return anything, try without any date provided at all.
  # This is necessary because for users with name history's *have* you have to provide some date (and 0 is valid),
  # while for users who do *not* have name history, giving a date (including 0) will return 204 No Content.
  # If it is *still* empty after both tries, then error.
  response = Curl.get(profile_str, { at: date.to_i }).body_str
  response = Curl.get(profile_str).body_str if response.empty?
  fail Mojang::Errors::NoSuchUserError.new(username) if response.empty?
  json = Oj.load(response)
  if json.key?('error')
    fail Mojang::Errors::MojangError.new(json['error'], json['errorMessage'])
  end

  json['id']
end

Instance Method Details

#name_history(uuid) ⇒ Hash<Symbol/Time, String> (private)

Gets a user's name history from their UUID. the name was changed. Value is always the name at that point in time.

Parameters:

  • uuid (String)

    The user's ID (see #userid).

Returns:

  • (Hash<Symbol/Time, String>)

    A hash of all the names. Key is either :original, or the Time object of when

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mojang.rb', line 53

def name_history(uuid)
  response = Curl.get("https://api.mojang.com/user/profiles/#{uuid}/names").body_str
  json = Oj.load(response)
  if json.key?('error')
    fail Mojang::Errors::MojangError.new(json['error'], json['errorMessage'])
  end
  ret = {}
  json.each do |hash|
    if hash.key?('changedToAt')
      ret[Time.at(hash['changedToAt'] / 1000)] = hash['name']
    else
      ret[:original] = hash['name']
    end
  end

  ret
end

#statusHash<String, String> (private)

Gets the status for the various Mojang and Minecraft servers and web services.

Returns:

  • (Hash<String, String>)

    A hash containing keys of the sites, and values of the statuses (green, yellow, red).



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mojang.rb', line 11

def status
  response = Curl.get('https://status.mojang.com/check'.freeze).body_str
  json = Oj.load(response)
  ret = {}
  # Reformatting the returned data because it is super annoying to work with.
  json.each do |hash|
    hash.each do |site, status|
      ret[site] = status
    end
  end

  ret
end

#userid(username, date = nil) ⇒ String (private)

Gets the User ID (UUID) for the given username at the given time.

Parameters:

  • username (String)

    The username.

  • date (Date) (defaults to: nil)

    The date to get the ID at.

Returns:

  • (String)

    The username's user ID.

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mojang.rb', line 31

def userid(username, date = nil)
  profile_str = "https://api.mojang.com/users/profiles/minecraft/#{username}"
  # If the provided date (or 0, if not provided) does not return anything, try without any date provided at all.
  # This is necessary because for users with name history's *have* you have to provide some date (and 0 is valid),
  # while for users who do *not* have name history, giving a date (including 0) will return 204 No Content.
  # If it is *still* empty after both tries, then error.
  response = Curl.get(profile_str, { at: date.to_i }).body_str
  response = Curl.get(profile_str).body_str if response.empty?
  fail Mojang::Errors::NoSuchUserError.new(username) if response.empty?
  json = Oj.load(response)
  if json.key?('error')
    fail Mojang::Errors::MojangError.new(json['error'], json['errorMessage'])
  end

  json['id']
end