Module: Kagerator

Includes:
RestClient
Defined in:
lib/kagerator.rb,
lib/kagerator/request.rb,
lib/kagerator/version.rb,
lib/kagerator/response.rb

Overview

The main Kagerator class for consumption

Defined Under Namespace

Classes: Request, Response

Constant Summary collapse

API_URL =
'https://api.kag2d.com/'
VERSION =
'1.0.4'

Class Method Summary collapse

Class Method Details

.authenticate(username, password) ⇒ Boolean

Attempt to authenticate to the server using your KAG account username/password

Parameters:

  • username (String)
  • password (String)

Returns:

  • (Boolean)

    True if authenticated, false if failed



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/kagerator.rb', line 78

def self.authenticate(username,password)
  begin
    resp = Kagerator::Request.execute({
      :url => "player/#{username}/token/new",
      :method => :get,
      :headers => {
          :Authorization => "Basic "+Base64.encode64("#{username}:#{password}")
      }
    })
    if resp
      token = resp.from_json
      token = token[:playerToken]
      resp = Kagerator::Request.execute({
        :url => "player/#{username}/token/#{token}",
        :method => :get,
        :headers => {
            :Authorization => "Basic "+Base64.encode64("#{username}:#{password}")
        }
      })
      if resp
        resp = resp.from_json
        if resp[:playerTokenStatus]
          true
        end
      else
        false
      end
    else
      false
    end
  rescue
    false
  end
end

.player(name, load_avatar = true) ⇒ SymbolTable

Get a player information

Parameters:

  • name (String)

    The username of the player

  • load_avatar (Boolean) (defaults to: true)

    Whether or not to add the avatar to the data loaded. Makes another HTTP request to the API

Returns:

  • (SymbolTable)

    A hash of the player data



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kagerator.rb', line 14

def self.player(name,load_avatar = true)
  player = Kagerator::Request.execute({
    :url => "player/#{name}/status",
    :method => :get
  })
  if player
    player = player.from_json
    if load_avatar
      avatar = Kagerator::Request.execute({
        :url => "player/#{name}/avatar",
        :method => :get
      })
      if avatar
        player[:avatar] = avatar.from_json
      end
    end
    player
  else
    false
  end
end

.server(ip, port = 50301) ⇒ SymbolTable

Get information about a specific server

Parameters:

  • ip (String)

    The IP address of the server, in ipv4 or ipv6 format

  • port (String|Integer) (defaults to: 50301)

    The port of the server

Returns:

  • (SymbolTable)

    A hash of the server variables



63
64
65
66
67
68
69
# File 'lib/kagerator.rb', line 63

def self.server(ip,port = 50301)
  server = Kagerator::Request.execute({
    :url => "server/ip/#{ip}/port/#{port}/status",
    :method => :get
  })
  server.from_json
end

.servers(args = {}) ⇒ SymbolTable

Get a list of servers, filtered by any arguments

Parameters:

  • args (Hash) (defaults to: {})

    A hash of arguments to filter with

Returns:

  • (SymbolTable)

    An array of hashes of results



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

def self.servers(args = {})
  qs = ''
  if args.class == Hash or args.class == SymbolTable
    args.each do |k,v|
      qs += '/'+k.to_s+'/'+v.to_s
    end
  end
  servers = Kagerator::Request.execute({
    :url => "servers/connectable/1"+qs,
    :method => :get
  })
  servers.from_json
end

.versionObject



3
# File 'lib/kagerator/version.rb', line 3

def self.version; VERSION; end