Class: Flox::RestService
- Inherits:
-
Object
- Object
- Flox::RestService
- 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
-
#base_url ⇒ String
readonly
The URL pointing to the Flox REST API.
-
#game_id ⇒ String
readonly
The unique identifier of the game.
-
#game_key ⇒ String
readonly
The key that identifies the game.
Instance Method Summary collapse
-
#delete(path) ⇒ Object
Makes a
DELETErequest at the server. -
#get(path, data = nil) ⇒ Object
Makes a
GETrequest at the server. -
#initialize(game_id, game_key, base_url) ⇒ RestService
constructor
A new instance of RestService.
-
#inspect ⇒ String
Provides a simple string representation of the service.
-
#login(auth_type, auth_id = nil, auth_token = nil) ⇒ Object
Makes a login on the server with the given authentication data.
-
#post(path, data = nil) ⇒ Object
Makes a
POSTrequest at the server. -
#put(path, data = nil) ⇒ Object
Makes a
PUTrequest at the server.
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 login :guest end |
Instance Attribute Details
#base_url ⇒ String (readonly)
Returns 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_id ⇒ String (readonly)
Returns the unique identifier of the game.
12 13 14 |
# File 'lib/flox/rest_service.rb', line 12 def game_id @game_id end |
#game_key ⇒ String (readonly)
Returns 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.
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.
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 |
#inspect ⇒ String
Returns 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.
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 login(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.
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.
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 |