Class: Ezags::Xsd::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/ezags-xsd/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



4
5
6
7
8
9
10
# File 'lib/ezags-xsd/middleware.rb', line 4

def initialize(app, options = {})
  @files = Dir[Ezags::Xsd.protocols_root.join('**/**/*.xsd').to_s, Ezags::Xsd.protocols_root.join('*.xsd').to_s]
  #@files.concat Dir[Ezags::Xsd.protocols_root.join('**/**/*.wsdl').to_s, Ezags::Xsd.protocols_root.join('*.wsdl').to_s]

  @path_cache, @render_cache = {}, {}
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ezags-xsd/middleware.rb', line 12

def call(env)
  if env['PATH_INFO'] =~ /\.xsd$/
    file = find_file(env['PATH_INFO'])
    if file
      [200, {'Content-Type' => 'application/xml; charset=utf-8'}, [rewrite_xsd(file)]]
    else
      [404, {}, []]
    end
  else
    @app.call(env)
  end
end

#find_file(path) ⇒ Object



25
26
27
# File 'lib/ezags-xsd/middleware.rb', line 25

def find_file(path)
  @path_cache[path] ||= @files.find {|f| f =~ /#{path}$/ }
end

#rewrite_xsd(file) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/ezags-xsd/middleware.rb', line 29

def rewrite_xsd(file)
  @render_cache[file] ||= File.read(file).gsub(/schemaLocation="(.*?)"/) do
    location = $1
    new_location = location.gsub(/(\.\.\/)*/,'')
    %Q{schemaLocation="/#{new_location}"}
  end
end