class MinecraftServerStatus::Result
Attributes
alive[R]
description[R]
errors[R]
favicon[R]
num_max_players[R]
num_online_players[R]
protocol[R]
raw[R]
version[R]
Public Class Methods
new(json_response, alive, errors)
click to toggle source
# 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
Public Instance Methods
alive?()
click to toggle source
# File lib/minecraft_server_status/result.rb, line 29 def alive? alive end
export_favicon(dir_path=nil, file_name=nil)
click to toggle source
# 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
Private Instance Methods
check_image(file_path)
click to toggle source
# File lib/minecraft_server_status/result.rb, line 70 def check_image(file_path) raise ArgumentError.new('file_path is required') if file_path.nil? result = false begin File.open(file_path, 'rb') do |f| head = f.read(8) result = head.unpack('CA3C4') == [0x89, 'PNG', 0xd, 0xa, 0x1a, 0xa] end rescue Exception => e result = false end return result end