Class: Flox
- Inherits:
-
Object
- Object
- Flox
- Defined in:
- lib/flox.rb,
lib/flox/version.rb
Overview
The main class used to interact with the Flox cloud service. Create an instance of Flox using the game ID and key acquired from the web interface, then login with a "Hero" key. That way, you will be able to access the data of all players.
Defined Under Namespace
Classes: Entity, Error, Player, Query, RestService, Score, ServiceError
Constant Summary collapse
- DEFAULT_URL =
The URL where the Flox servers are found.
"https://www.flox.cc"
- VERSION =
The current version of the Flox SDK.
'0.4.0'
Instance Attribute Summary collapse
-
#current_player ⇒ Flox::Player
readonly
The player that is currently logged in.
Instance Method Summary collapse
-
#base_url ⇒ String
The base URL of the Flox service.
-
#delete_entity(*entity) ⇒ Object
Deletes the given entity from the database.
-
#find_entities(*query) ⇒ Object
Executes a query over Entities, using a simple SQL-like syntax.
-
#find_entity_ids(*query) ⇒ Array<String>
Executes a query over Entities, using a simple SQL-like syntax.
-
#find_log_ids(query = nil, limit = nil) ⇒ Array<String>
Finds just the IDs of the logs, defined by a certain query.
-
#find_logs(query = nil, limit = nil) ⇒ Array<Hash>
Finds logs defined by a certain query.
-
#game_id ⇒ String
The ID of the game you are accessing.
-
#game_key ⇒ String
The key of the game you are accessing.
-
#initialize(game_id, game_key, base_url = Flox::DEFAULT_URL) ⇒ Flox
constructor
Creates a new instance with a certain game ID and key.
-
#inspect ⇒ String
A string representation of the object.
-
#load_entity(type, id) ⇒ Flox::Entity
Loads an entity with a certain type and id from the server.
-
#load_log(log_id) ⇒ Hash
Loads a log with a certain ID.
-
#load_player(id) ⇒ Flox::Player
Loads an entity with type '.player'.
-
#load_resource(path, args = nil) ⇒ Hash
Loads a JSON object from the given path.
-
#load_resources(path, ids) {|id, resource| ... } ⇒ Array
Loads a bulk of resources from a certain path, optionally feeding them through a block.
-
#load_scores(leaderboard_id, scope) ⇒ Array<Flox::Score>
Loads all scores of a leaderboard, sorted by rank.
-
#login_guest ⇒ Flox::Player
(also: #logout)
Creates a new guest player and logs it in.
-
#login_with_key(key) ⇒ Flox::Player
Makes a key-login on the server.
-
#post_score(leaderboard_id, score, player_name) ⇒ Object
Posts a score to a certain leaderboard.
-
#save_entity(entity) ⇒ Flox::Entity
Stores an entity on the server.
-
#status ⇒ Hash
Loads the status of the Flox service.
Constructor Details
#initialize(game_id, game_key, base_url = Flox::DEFAULT_URL) ⇒ Flox
Creates a new instance with a certain game ID and key. Per default, a guest player will be logged in. You probably need to call 'login_with_key' with a Hero-key to access your data.
23 24 25 26 |
# File 'lib/flox.rb', line 23 def initialize(game_id, game_key, base_url=Flox::DEFAULT_URL) @service = RestService.new(game_id, game_key, base_url) self.login_guest end |
Instance Attribute Details
#current_player ⇒ Flox::Player (readonly)
Returns The player that is currently logged in.
18 19 20 |
# File 'lib/flox.rb', line 18 def current_player @current_player end |
Instance Method Details
#base_url ⇒ String
The base URL of the Flox service.
225 226 227 |
# File 'lib/flox.rb', line 225 def base_url service.base_url end |
#delete_entity(entity) ⇒ Object #delete_entity(type, id) ⇒ Object
Deletes the given entity from the database.
82 83 84 85 86 87 88 89 90 |
# File 'lib/flox.rb', line 82 def delete_entity(*entity) if entity.length > 1 type, id = entity[0], entity[1] else type, id = entity[0].type, entity[0].id end service.delete(entity_path(type, id)) nil end |
#find_entities(query) ⇒ Object #find_entities(entity_type, constraints = nil, *args) ⇒ Object
Executes a query over Entities, using a simple SQL-like syntax.
185 186 187 188 189 190 191 |
# File 'lib/flox.rb', line 185 def find_entities(*query) query = create_query(*query) ids = find_entity_ids(query) load_resources "entities/#{query.type}", ids do |id, data| create_entity(query.type, id, data) end end |
#find_entity_ids(*query) ⇒ Array<String>
Executes a query over Entities, using a simple SQL-like syntax.
197 198 199 200 201 202 203 |
# File 'lib/flox.rb', line 197 def find_entity_ids(*query) query = create_query(*query) path = "entities/#{query.type}" data = { where: query.constraints, offset: query.offset, limit: query.limit, orderBy: query.order_by } service.post(path, data).map {|e| e[:id]} end |
#find_log_ids(query = nil, limit = nil) ⇒ Array<String>
Finds just the IDs of the logs, defined by a certain query.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/flox.rb', line 160 def find_log_ids(query=nil, limit=nil) log_ids = [] cursor = nil begin args = {} args[:q] = query if query args[:l] = limit if limit args[:c] = cursor if cursor result = service.get "logs", args cursor = result[:cursor] log_ids += result[:ids] limit -= log_ids.length if limit end while !cursor.nil? and (limit.nil? or limit > 0) log_ids end |
#find_logs(query = nil, limit = nil) ⇒ Array<Hash>
Finds logs defined by a certain query. Here are some sample queries:
day:2014-02-20
→ all logs of a certain dayseverity:warning
→ all logs of type warning & errorseverity:error
→ all logs of type errorday:2014-02-20 severity:error
→ all error logs from February 20th.
152 153 154 155 |
# File 'lib/flox.rb', line 152 def find_logs(query=nil, limit=nil) log_ids = find_log_ids(query, limit) load_resources('logs', log_ids) end |
#game_id ⇒ String
The ID of the game you are accessing.
213 214 215 |
# File 'lib/flox.rb', line 213 def game_id service.game_id end |
#game_key ⇒ String
The key of the game you are accessing.
219 220 221 |
# File 'lib/flox.rb', line 219 def game_key service.game_key end |
#inspect ⇒ String
Returns a string representation of the object.
230 231 232 |
# File 'lib/flox.rb', line 230 def inspect "[Flox game_id: '#{game_id}', base_url: '#{base_url}']" end |
#load_entity(type, id) ⇒ Flox::Entity
Loads an entity with a certain type and id from the server. Normally, the type is the class name you used for the entity in your game.
55 56 57 58 59 |
# File 'lib/flox.rb', line 55 def load_entity(type, id) path = entity_path(type, id) data = service.get(path) create_entity(type, id, data) end |
#load_log(log_id) ⇒ Hash
Loads a log with a certain ID. A log is a Hash instance.
139 140 141 |
# File 'lib/flox.rb', line 139 def load_log(log_id) service.get log_path(log_id) end |
#load_player(id) ⇒ Flox::Player
Loads an entity with type '.player'.
63 64 65 |
# File 'lib/flox.rb', line 63 def load_player(id) load_entity('.player', id) end |
#load_resource(path, args = nil) ⇒ Hash
Loads a JSON object from the given path. This works with any resource e.g. entities, logs, etc.
120 121 122 |
# File 'lib/flox.rb', line 120 def load_resource(path, args=nil) service.get(path, args) end |
#load_resources(path, ids) {|id, resource| ... } ⇒ Array
Loads a bulk of resources from a certain path, optionally feeding them through a block.
129 130 131 132 133 134 135 |
# File 'lib/flox.rb', line 129 def load_resources(path, ids) ids.map do |id| resource = load_resource "#{path}/#{id}" resource = yield id, resource if block_given? resource end end |
#load_scores(leaderboard_id, scope) ⇒ Array<Flox::Score>
Loads all scores of a leaderboard, sorted by rank. 'scope' can either be one of the symbols +:today, :this_week, :all_time+ or an array of player IDs.
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/flox.rb', line 103 def load_scores(leaderboard_id, scope) path = leaderboard_path(leaderboard_id) args = {} if scope.is_a?(Array) args[:p] = scope else args[:t] = scope.to_s.to_camelcase end raw_scores = service.get(path, args) raw_scores.collect { |raw_score| Score.new(raw_score) } end |
#login_guest ⇒ Flox::Player Also known as: logout
Creates a new guest player and logs it in. After the login,
current_player
will point to this player.
39 40 41 |
# File 'lib/flox.rb', line 39 def login_guest() login(:guest) end |
#login_with_key(key) ⇒ Flox::Player
Makes a key-login on the server. It is recommended to create a 'hero'
player in the web interface and use that for the login. After the login,
current_player
will point to this player.
32 33 34 |
# File 'lib/flox.rb', line 32 def login_with_key(key) login(:key, key) end |
#post_score(leaderboard_id, score, player_name) ⇒ Object
Posts a score to a certain leaderboard. Beware that only the top score of a player will appear on the leaderboard.
94 95 96 97 98 |
# File 'lib/flox.rb', line 94 def post_score(leaderboard_id, score, player_name) path = leaderboard_path(leaderboard_id) data = { playerName: player_name, value: score } service.post(path, data) end |
#save_entity(entity) ⇒ Flox::Entity
Stores an entity on the server.
69 70 71 72 73 74 |
# File 'lib/flox.rb', line 69 def save_entity(entity) result = service.put(entity.path, entity) entity[:updatedAt] = result[:updatedAt] entity[:createdAt] = result[:createdAt] entity end |
#status ⇒ Hash
Loads the status of the Flox service.
207 208 209 |
# File 'lib/flox.rb', line 207 def status service.get("") end |