Class: Committee::SchemaValidator::HyperSchema::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/committee/schema_validator/hyper_schema/router.rb

Instance Method Summary collapse

Constructor Details

#initialize(schema, validator_option) ⇒ Router

Returns a new instance of Router.



7
8
9
10
11
12
13
# File 'lib/committee/schema_validator/hyper_schema/router.rb', line 7

def initialize(schema, validator_option)
  @prefix = validator_option.prefix
  @prefix_regexp = /\A#{Regexp.escape(@prefix)}/.freeze if @prefix
  @schema = schema

  @validator_option = validator_option
end

Instance Method Details

#build_schema_validator(request) ⇒ Object



40
41
42
# File 'lib/committee/schema_validator/hyper_schema/router.rb', line 40

def build_schema_validator(request)
  Committee::SchemaValidator::HyperSchema.new(self, request, @validator_option)
end


23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/committee/schema_validator/hyper_schema/router.rb', line 23

def find_link(method, path)
  path = path.gsub(@prefix_regexp, "") if @prefix
  link_with_matches = (@schema.routes[method] || []).map do |pattern, link|
    if matches = pattern.match(path)
      # prefer path which has fewer matches (eg. `/pets/dog` than `/pets/{uuid}` for path `/pets/dog` )
      [matches.captures.size, link, Hash[matches.names.zip(matches.captures)]]
    else
      nil
    end
  end.compact.sort_by(&:first).first
  link_with_matches.nil? ? nil : link_with_matches.slice(1, 2)
end


36
37
38
# File 'lib/committee/schema_validator/hyper_schema/router.rb', line 36

def find_request_link(request)
  find_link(request.request_method, request.path_info)
end

#includes?(path) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/committee/schema_validator/hyper_schema/router.rb', line 15

def includes?(path)
  !@prefix || path =~ @prefix_regexp
end

#includes_request?(request) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/committee/schema_validator/hyper_schema/router.rb', line 19

def includes_request?(request)
  includes?(request.path)
end