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
DELETE
request at the server. -
#get(path, data = nil) ⇒ Object
Makes a
GET
request 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
POST
request at the server. -
#put(path, data = nil) ⇒ Object
Makes a
PUT
request at the server. -
#request(method, path, data = nil) {|body, response| ... } ⇒ Object
Makes a request on the Flox server.
Constructor Details
#initialize(game_id, game_key, base_url) ⇒ RestService
Returns a new instance of RestService.
22 23 24 25 26 27 |
# File 'lib/flox/rest_service.rb', line 22 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.
20 21 22 |
# File 'lib/flox/rest_service.rb', line 20 def base_url @base_url end |
#game_id ⇒ String (readonly)
Returns the unique identifier of the game.
14 15 16 |
# File 'lib/flox/rest_service.rb', line 14 def game_id @game_id end |
#game_key ⇒ String (readonly)
Returns the key that identifies the game.
17 18 19 |
# File 'lib/flox/rest_service.rb', line 17 def game_key @game_key end |
Instance Method Details
#delete(path) ⇒ Object
Makes a DELETE
request at the server.
38 39 40 |
# File 'lib/flox/rest_service.rb', line 38 def delete(path) request(:delete, path) 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.
32 33 34 |
# File 'lib/flox/rest_service.rb', line 32 def get(path, data=nil) request(:get, path, data) end |
#inspect ⇒ String
Returns provides a simple string representation of the service.
144 145 146 |
# File 'lib/flox/rest_service.rb', line 144 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.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/flox/rest_service.rb', line 121 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 if @authentication[:authType] == :guest auth_data[:id] = @authentication[:id] end 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.
45 46 47 |
# File 'lib/flox/rest_service.rb', line 45 def post(path, data=nil) request(:post, path, 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.
52 53 54 |
# File 'lib/flox/rest_service.rb', line 52 def put(path, data=nil) request(:put, path, data) end |
#request(method, path, data = nil) {|body, response| ... } ⇒ Object
Makes a request on the Flox server. When called without a block, the method will raise a ServiceError if the server returns an HTTP error code; when called with a block, it will always succeed.
65 66 67 68 69 70 71 72 73 74 75 76 77 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 112 113 114 115 116 117 |
# File 'lib/flox/rest_service.rb', line 65 def request(method, path, data=nil) request_class = case method when :get if data path += "?" + URI.encode_www_form(data) data = nil end Net::HTTP::Get when :delete then Net::HTTP::Delete when :post then Net::HTTP::Post when :put then Net::HTTP::Put end flox_header = { :sdk => { :type => "ruby", :version => Flox::VERSION }, :gameKey => @game_key, :dispatchTime => Time.now.utc.to_xs_datetime, :bodyCompression => "zlib", :player => @authentication } request = request_class.new(full_path(path)) request["Content-Type"] = "application/json" request["X-Flox"] = flox_header.to_json request.body = encode(data.to_json) if data uri = URI.parse(@base_url) http = Net::HTTP::new(uri.host, uri.port) if uri.scheme == "https" # enable SSL/TLS http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.use_ssl = true end http.start do |session| response = session.request(request) body = body_from_response response if block_given? # if a block was passed to the method, no error is thrown yield body, response return body else # without a block, we raise an error if the request was not a success if (response.is_a? Net::HTTPSuccess) return body else raise Flox::ServiceError.new(response), (body[:message] rescue body) end end end end |