Class: Gamewisp::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/gamewisp/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port, state) ⇒ Server

Returns a new instance of Server.



18
19
20
21
# File 'lib/gamewisp/server.rb', line 18

def initialize port, state
  @port = port
  @state = state
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



15
16
17
# File 'lib/gamewisp/server.rb', line 15

def port
  @port
end

#stateObject (readonly)

Returns the value of attribute state.



16
17
18
# File 'lib/gamewisp/server.rb', line 16

def state
  @state
end

Instance Method Details

#get_authentication_tokenObject

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
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
# File 'lib/gamewisp/server.rb', line 23

def get_authentication_token
  raise ArgumentError, "No port specified" if port.nil?
  raise ArgumentError, "No state specified" if state.nil?

  server = TCPServer.open(port) # Socket to listen on specific port

  client = server.accept
  method, path = client.gets.split
  headers = {}
  while line = client.gets.split(" ", 2)
    break if line[0] == ""
    headers[line[0].chop] = line[1].strip
  end
  data = client.read(headers["Content-Length"].to_i)

  dbg "Server:get_authentication_token [method]", method
  dbg "Server:get_authentication_token [path]", path
  dbg "Server:get_authentication_token [headers]", headers
  dbg "Server:get_authentication_token [data]", data

  results = split_path_components path

  failed = false

  if results["state"].nil? || results["state"] != @state
    client.puts "Unrecognized request. Please try again"
    failed = true
  end

  if results["code"].nil?
    client.puts "Unrecognized request. Please try again"
    failed = true
  end

  unless failed == true
    client.puts "Authorization received. You can close this window now."
  end

  client.close

  results
end

#split_path_components(path) ⇒ Object

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gamewisp/server.rb', line 66

def split_path_components path
  raise ArgumentError, "nil path returned from gamewisp" if path.nil?

  vals = path.split('&', 2)
  vals[0].gsub!("/?", "")

  results = {}
  parts = vals[0].split("=", 2)
  results[parts[0]] = parts[1]

  parts = vals[1].split("=", 2)
  results[parts[0]] = parts[1]

  results
end