Module: Minecraft::Data

Defined in:
lib/minecraft/data.rb,
lib/minecraft/data/version.rb

Overview

Module handling all data-gathering operations

Defined Under Namespace

Classes: Error

Constant Summary collapse

USERS_API =
'https://api.ashcon.app/mojang/v2/user'.freeze
HEADS_API =
'https://api.ashcon.app/mojang/v2/avatar'.freeze
STEVE_UUID =

Steve <3

'8667ba71-b85a-4004-af54-457a9734eed7'.freeze
VERSION =
'0.2.0'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



18
19
20
# File 'lib/minecraft/data.rb', line 18

def logger
  @logger
end

.timeoutObject

Returns the value of attribute timeout.



19
20
21
# File 'lib/minecraft/data.rb', line 19

def timeout
  @timeout
end

Class Method Details

.api_get(url, &block) ⇒ Object

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



26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



79
80
81
# File 'lib/minecraft/data.rb', line 79

def head_url_of_username(username)
  "#{HEADS_API}/#{username}"
end

.head_url_of_uuid(uuid) ⇒ Object



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

def head_url_of_uuid(uuid)
  "#{HEADS_API}/#{uuid}"
end

.log(msg) ⇒ Object



21
22
23
# File 'lib/minecraft/data.rb', line 21

def log(msg)
  logger&.debug(msg)
end

.name_history_of_username(username) ⇒ Object



63
64
65
66
67
# 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) ⇒ Object



69
70
71
72
73
# 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) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object



51
52
53
54
55
# 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) ⇒ Object



57
58
59
60
61
# 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