Class: Flox::RestService

Inherits:
Object
  • Object
show all
Defined in:
lib/flox/rest_service.rb

Overview

A class that makes it easy to communicate with the Flox server via a REST protocol.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game_id, game_key, base_url) ⇒ RestService

Returns a new instance of RestService.



20
21
22
23
24
25
# File 'lib/flox/rest_service.rb', line 20

def initialize(game_id, game_key, base_url)
  @game_id = game_id
  @game_key = game_key
  @base_url = base_url
   :guest
end

Instance Attribute Details

#base_urlString (readonly)

Returns the URL pointing to the Flox REST API.

Returns:

  • (String)

    the URL pointing to the Flox REST API.



18
19
20
# File 'lib/flox/rest_service.rb', line 18

def base_url
  @base_url
end

#game_idString (readonly)

Returns the unique identifier of the game.

Returns:

  • (String)

    the unique identifier of the game.



12
13
14
# File 'lib/flox/rest_service.rb', line 12

def game_id
  @game_id
end

#game_keyString (readonly)

Returns the key that identifies the game.

Returns:

  • (String)

    the key that identifies the game.



15
16
17
# File 'lib/flox/rest_service.rb', line 15

def game_key
  @game_key
end

Instance Method Details

#delete(path) ⇒ Object

Makes a DELETE request at the server.

Returns:

  • the server response.



39
40
41
42
# File 'lib/flox/rest_service.rb', line 39

def delete(path)
  request = Net::HTTP::Delete.new(full_path(path))
  execute(request)
end

#get(path, data = nil) ⇒ Object

Makes a GET request at the server. The given data-Hash is URI-encoded and added to the path.

Returns:

  • the server response.



30
31
32
33
34
35
# File 'lib/flox/rest_service.rb', line 30

def get(path, data=nil)
  path = full_path(path)
  path += "?" + URI.encode_www_form(data) if data
  request = Net::HTTP::Get.new(path)
  execute(request)
end

#inspectString

Returns provides a simple string representation of the service.

Returns:

  • (String)

    provides a simple string representation of the service.



82
83
84
# File 'lib/flox/rest_service.rb', line 82

def inspect
  "[RestService game_id: #{game_id}, base_url: #{base_url}]"
end

#login(auth_type, auth_id = nil, auth_token = nil) ⇒ Object

Makes a login on the server with the given authentication data.

Returns:

  • the server response.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/flox/rest_service.rb', line 62

def (auth_type, auth_id=nil, auth_token=nil)
  auth_data = {
    "authType"  => auth_type,
    "authId"    => auth_id,
    "authToken" => auth_token
  }

  if (auth_type.to_sym == :guest)
    response = auth_data
    auth_data["id"] = String.random_uid
  else
    response = post("authenticate", auth_data)
    auth_data["id"] = response["id"]
  end

  @authentication = auth_data
  response
end

#post(path, data = nil) ⇒ Object

Makes a POST request at the server. The given data-Hash is transferred in the body of the request.

Returns:

  • the server response.



47
48
49
50
# File 'lib/flox/rest_service.rb', line 47

def post(path, data=nil)
  request = Net::HTTP::Post.new(full_path(path))
  execute(request, data)
end

#put(path, data = nil) ⇒ Object

Makes a PUT request at the server. The given data-Hash is transferred in the body of the request.

Returns:

  • the server response.



55
56
57
58
# File 'lib/flox/rest_service.rb', line 55

def put(path, data=nil)
  request = Net::HTTP::Put.new(full_path(path))
  execute(request, data)
end