Class: Minescope::Rack::WorldData

Inherits:
Object
  • Object
show all
Defined in:
lib/minescope/rack/worlddata.rb

Instance Method Summary collapse

Constructor Details

#initialize(world_dir) ⇒ WorldData

Returns a new instance of WorldData.



7
8
9
# File 'lib/minescope/rack/worlddata.rb', line 7

def initialize(world_dir)
  @world_dir = world_dir
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/minescope/rack/worlddata.rb', line 11

def call(env)
  path = ::Rack::Utils.unescape(env['PATH_INFO'])
  if path.include? ".."
    return [403, {'Content-Type' => 'text/plain'}, ["403 Access Denied"]]
  end

  dat_path = path.dup.sub!(/\.json$/, '.dat')
  
  begin
    request = ::Rack::Request.new(env)
    prettyprint = (request['prettyprint'] || 0).to_i != 0
    raise Errno::ENOENT unless dat_path
    dat_filename = File.join(@world_dir, dat_path)
    json = File.open(dat_filename, "rb") { |io|
      Minescope::Transcoder.transcode(io, :prettyprint => prettyprint)
    }
    [200, {'Content-Type' => 'application/json; charset=utf-8'}, [json]]
  rescue Errno::ENOENT
    [404, {'Content-Type' => 'text/plain'}, ["404 Not Found"]]
  end
end