Class: MinecraftServerStatus::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/minecraft_server_status/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_response, alive, errors) ⇒ Result



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/minecraft_server_status/result.rb', line 15

def initialize(json_response, alive, errors)
  @raw    = json_response
  @alive  = alive
  @errors = errors
  if alive
    @version            = json_response["version"]["name"]
    @protocol           = json_response["version"]["protocol"]
    @num_max_players    = json_response["players"]["max"]
    @num_online_players = json_response["players"]["online"]
    @description        = json_response["description"]
    @favicon            = json_response["favicon"]
  end
end

Instance Attribute Details

#aliveObject (readonly)

Returns the value of attribute alive.



5
6
7
# File 'lib/minecraft_server_status/result.rb', line 5

def alive
  @alive
end

#descriptionObject (readonly)

Returns the value of attribute description.



5
6
7
# File 'lib/minecraft_server_status/result.rb', line 5

def description
  @description
end

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/minecraft_server_status/result.rb', line 5

def errors
  @errors
end

#faviconObject (readonly)

Returns the value of attribute favicon.



5
6
7
# File 'lib/minecraft_server_status/result.rb', line 5

def favicon
  @favicon
end

#num_max_playersObject (readonly)

Returns the value of attribute num_max_players.



5
6
7
# File 'lib/minecraft_server_status/result.rb', line 5

def num_max_players
  @num_max_players
end

#num_online_playersObject (readonly)

Returns the value of attribute num_online_players.



5
6
7
# File 'lib/minecraft_server_status/result.rb', line 5

def num_online_players
  @num_online_players
end

#protocolObject (readonly)

Returns the value of attribute protocol.



5
6
7
# File 'lib/minecraft_server_status/result.rb', line 5

def protocol
  @protocol
end

#rawObject (readonly)

Returns the value of attribute raw.



5
6
7
# File 'lib/minecraft_server_status/result.rb', line 5

def raw
  @raw
end

#versionObject (readonly)

Returns the value of attribute version.



5
6
7
# File 'lib/minecraft_server_status/result.rb', line 5

def version
  @version
end

Instance Method Details

#alive?Boolean



29
30
31
# File 'lib/minecraft_server_status/result.rb', line 29

def alive?
  alive
end

#export_favicon(dir_path = nil, file_name = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/minecraft_server_status/result.rb', line 33

def export_favicon(dir_path=nil, file_name=nil)
  if !alive?
    warn "server must be alive"
    return nil
  end

  if favicon.nil?
    warn "favicon is not found"
    return nil
  end

  dir_path  ||= File.dirname(__FILE__) + '/'
  file_name ||= Time.now.strftime("%Y%m%d_%H%M%S") + '.png'
  file_path = dir_path + file_name

  # truncate newline and header
  base64_encoded_image = favicon.gsub(/[\r\n]/, '').gsub(/^data:image\/png;base64,/, '')
  
  begin
    FileUtils.mkdir_p(dir_path) unless FileTest.exist?(dir_path)
    File.open(file_path, 'wb') do |f|
      f.write(Base64.decode64(base64_encoded_image))
    end
  rescue Exception => e
    warn "failed to create image file: #{e}"
    return nil
  end

  if !check_image(file_path)
    warn 'invalid image format'
    return nil
  end

  return file_name
end