Class: Pixiurge::Middleware::TmxJson
- Inherits:
-
Object
- Object
- Pixiurge::Middleware::TmxJson
- Defined in:
- lib/pixiurge/config_ru.rb
Overview
The TmxJson middleware reads TMX files in the normal XML mode that Tiled loads and saves easily, but serves it in the AJAX-friendly JSON format that Tiled exports. This allows JavaScript-friendly usage directly from a standard XML TMX file without the manual export step every time.
Instance Method Summary collapse
-
#call(env) ⇒ Object
The Rack .call method for middleware.
-
#initialize(app, options = {}) ⇒ TmxJson
constructor
Constructor.
Constructor Details
#initialize(app, options = {}) ⇒ TmxJson
Constructor. Rack has a standard format for these, though the options are specific to the TmxJson middleware.
204 205 206 207 208 209 |
# File 'lib/pixiurge/config_ru.rb', line 204 def initialize(app, = {}) @app = app @urls = [([:urls] || "/tmx")].flatten @root = [:root] || Dir.pwd @cache = [:cache] || ::Demiurge::Tmx::TmxCache.new(:root_dir => @root) end |
Instance Method Details
#call(env) ⇒ Object
The Rack .call method for middleware.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/pixiurge/config_ru.rb', line 215 def call(env) # If no TMX path is matched, forward the call to the next middleware call_root = matches_url(env["PATH_INFO"]) return @app.call(env) unless call_root # Okay, a TMX directory was matched... local_path = File.join(@root, env["PATH_INFO"]) # Check for a .tmx.json, .manasource.json or .conv.json json_local_path = local_path.sub(/\.([^.]+)\.json$/, ".tmx") subformat = $1 existing = [local_path, json_local_path].detect { |f| File.exists?(f) } unless existing return [404, {}, [""]] else if existing == local_path # Whatever it is, it's a path that literally exists right there. return [ 200, {}, Rack::File.new(existing) ] elsif subformat == "conv" tmx_map = Tmx.load(existing) json_contents = tmx_map.export_to_string :filename => existing, :format => :json return [200, { "type" => "application/json" }, [ json_contents ] ] elsif subformat == "tmx" return [200, { "type" => "application/json" }, [ MultiJson.dump(@cache.tmx_entry("tmx", existing)) ] ] elsif subformat == "tmxpretty" return [200, { "type" => "application/json" }, [ MultiJson.dump(@cache.tmx_entry("tmx", existing), :pretty => true) ] ] elsif subformat == "manasource" return [200, { "type" => "application/json" }, [ MultiJson.dump(@cache.tmx_entry("manasource", existing)) ] ] elsif subformat == "manasourcepretty" return [200, { "type" => "application/json" }, [ MultiJson.dump(@cache.tmx_entry("manasource", existing), :pretty => true) ] ] else # Okay, no clue what they're asking for even though we have a matching .tmx file. return [404, {}, [""]] end end end |