Class: Yogo::Rack::ModelLookup

Inherits:
Object
  • Object
show all
Defined in:
lib/yogo/rack/model_lookup.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ModelLookup.



8
9
10
11
12
13
# File 'lib/yogo/rack/model_lookup.rb', line 8

def initialize(app, options = {})
  paths = options[:paths] || ['data']
  @app = app
  @scope = options[:scope] || lambda { |*args| return Schema }
  @base_regexp = /^\/(#{paths.join('|')})\/((\w|-|\s)+)/
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/yogo/rack/model_lookup.rb', line 15

def call(env)
  if env['PATH_INFO'] =~ @base_regexp
    model_id = $2
    env['yogo.schema'], env['yogo.resource'] = get_model(model_id, env)
    return [ 404, {'Content-Type' => 'text/plain'}, ["#{model_id} not found"] ] if env['yogo.schema'].nil?
  end

  @app.call(env)
end