Class: OldGameSpyQuery::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/old_gamespy_query/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(string, mode = 'status') ⇒ Parser

Returns a new instance of Parser.



3
4
5
6
7
8
9
10
# File 'lib/old_gamespy_query/parser.rb', line 3

def initialize(string, mode = 'status')
  @string = string
  @mode   = mode
  @data   = {}
  @array  = @string[0].force_encoding('utf-8').strip.split("\\")
  @array.delete(@array.first) # Remove blank first item
  parse
end

Instance Method Details

#dataObject



68
69
70
# File 'lib/old_gamespy_query/parser.rb', line 68

def data
  @data
end

#parseObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/old_gamespy_query/parser.rb', line 12

def parse
  case @mode
  when 'status'
    status_parse
  when 'players'
    players_parse
  when 'rules'
    status_parse # Status and Rules appear compatible
  when 'info'
    status_parse # Status and Info appear compatible
  end

  @data["_gamespy"] = {
    "hostname" => "#{@string[1][3]}", "gamespy_port" => @string[1][1]
    }
end

#players_parseObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/old_gamespy_query/parser.rb', line 44

def players_parse
  sub_hash = {}
  keys   = []
  values = []
  num = 0

  @array.each_with_index do |object, index|
    keys   << object if index.even?
    values << object if index.odd?
  end

  keys.each_with_index do |key, index|
    value = values[index]
    if key.end_with?("#{num}")
      sub_hash["#{key}"] = value
    elsif key.end_with?("#{num+1}")
      @data["player_#{num}"] = sub_hash
      sub_hash = {}
      num+=1
      sub_hash["#{key}"] = value
    end
  end
end

#status_parseObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/old_gamespy_query/parser.rb', line 29

def status_parse
  keys   = []
  values = []

  @array.each_with_index do |object, index|
    keys   << object if index.even?
    values << object if index.odd?
  end

  keys.each_with_index do |key, index|
    value = values[index]
    @data["#{key}"] = value
  end
end