Class: MojangApi::Profile

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/mojang_api/profile.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Profile

Returns a new instance of Profile.



6
7
8
9
10
11
# File 'lib/mojang_api/profile.rb', line 6

def initialize(params={})
  @name = params.fetch(:name, nil)
  @game = params.fetch(:game, 'minecraft')
  self.uuid = params.fetch(:uuid, nil)
  @uuid_time = params.fetch(:uuid_time, Time.now)
end

Class Method Details

.load_from_name_list(usernames = [], game = 'minecraft') ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mojang_api/profile.rb', line 65

def load_from_name_list(usernames = [], game='minecraft')
  all_responses = []
  usernames = usernames.reject(&:empty?)
  usernames.uniq.each_slice(100) do |username_slice|
    opts = {
      body: username_slice.to_json,
      headers: {"Content-Type" => "application/json"}
    }
    response = post("/profiles/#{game}",opts)
    all_responses += response.map {|result| new(name: result['name'], uuid: result['id'], game: game)}
  end
  
  all_responses.sort_by! { |profile| profile.name }
end

Instance Method Details

#dashed_uuidObject



25
26
27
# File 'lib/mojang_api/profile.rb', line 25

def dashed_uuid
  "#{uuid[0..7]}-#{uuid[8..11]}-#{uuid[12..15]}-#{uuid[16..19]}-#{uuid[20..31]}"
end

#load_nameObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mojang_api/profile.rb', line 43

def load_name
  response = self.class.get("/user/profiles/#{@uuid}/names", format: :json)
  if response.ok? 
    if response.length > 1
      raise "Multiple names for that UUID, unknown behavior!" 
    end
    
  else
    raise "Unable to find that UUID."
  end
  @name = response[0]
  @name
end

#load_uuid(at = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/mojang_api/profile.rb', line 33

def load_uuid(at = nil)
  opts = { format: :json }
  opts[:query] = { at: at.to_i } if at
  response = self.class.get("/users/profiles/#{@game}/#{@name}", opts)
  @uuid = response['id']
  @name = response['name']
  @uuid_time = at.nil? ? Time.now : at
  @uuid
end

#nameObject



29
30
31
# File 'lib/mojang_api/profile.rb', line 29

def name
  @name || load_name
end

#to_whitelist_format(version = :v17) ⇒ Object



57
58
59
60
61
62
# File 'lib/mojang_api/profile.rb', line 57

def to_whitelist_format(version = :v17)
  {
    uuid: formatted_uuid,
    name: name
  }
end

#uuidObject



13
14
15
# File 'lib/mojang_api/profile.rb', line 13

def uuid
  @uuid || load_uuid(@uuid_time)
end

#uuid=(new_uuid) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/mojang_api/profile.rb', line 17

def uuid=(new_uuid)
  if new_uuid
    new_uuid = new_uuid.strip.tr('-', '')
    raise "UUID is incorrect length." if new_uuid.length != 32
  end
  @uuid = new_uuid
end