module Minecraft::Data

Module handling all data-gathering operations

Constants

HEADS_API
STEVE_UUID
USERS_API
VERSION

Attributes

logger[RW]
timeout[RW]

Public Class Methods

api_get(url, &block) click to toggle source

Just gets a URL and wraps errors in Data::Error

# File lib/minecraft/data.rb, line 26
def api_get(url, &block)
  Timeout.timeout(timeout || 0) do
    open(url, &block)
  end
rescue OpenURI::HTTPError => e
  log "Failed to get url #{url}: #{e}"
  raise Error
rescue Timeout::Error
  log "Timed out (#{timeout}) getting url #{url}"
  raise Error
end
head_url_of_username(username) click to toggle source
# File lib/minecraft/data.rb, line 79
def head_url_of_username(username)
  "#{HEADS_API}/#{username}"
end
head_url_of_uuid(uuid) click to toggle source
# File lib/minecraft/data.rb, line 75
def head_url_of_uuid(uuid)
  "#{HEADS_API}/#{uuid}"
end
log(msg) click to toggle source
# File lib/minecraft/data.rb, line 21
def log(msg)
  logger&.debug(msg)
end
name_history_of_username(username) click to toggle source
# File lib/minecraft/data.rb, line 63
def name_history_of_username(username)
  api_get("#{USERS_API}/#{username}") do |io|
    JSON.parse(io.read)['username_history']
  end
end
name_history_of_uuid(uuid) click to toggle source
# File lib/minecraft/data.rb, line 69
def name_history_of_uuid(uuid)
  api_get("#{USERS_API}/#{uuid}") do |io|
    JSON.parse(io.read)['username_history']
  end
end
normalize_uuid(uuid) click to toggle source
# File lib/minecraft/data.rb, line 38
def normalize_uuid(uuid)
  if uuid.is_a? UUIDTools::UUID
    uuid.to_s
  elsif uuid =~ /\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/
    uuid.downcase
  elsif uuid =~ /\A(\h{8})(\h{4})(\h{4})(\h{4})(\h{12})\z/
    "#{$1}-#{$2}-#{$3}-#{$4}-#{$5}".downcase
  else
    # Validation will catch it
    uuid
  end
end
username_to_uuid(username) click to toggle source
# File lib/minecraft/data.rb, line 51
def username_to_uuid(username)
  api_get("#{USERS_API}/#{username}") do |io|
    JSON.parse(io.read)['uuid']
  end
end
uuid_to_username(uuid) click to toggle source
# File lib/minecraft/data.rb, line 57
def uuid_to_username(uuid)
  api_get("#{USERS_API}/#{uuid}") do |io|
    JSON.parse(io.read)['username']
  end
end