Class: ApiValve::RouteSet

Inherits:
Object
  • Object
show all
Defined in:
lib/api_valve/route_set.rb

Defined Under Namespace

Classes: Route

Constant Summary collapse

METHODS =
%i(get post put patch delete head).freeze

Instance Method Summary collapse

Constructor Details

#initialize ⇒ RouteSet

Returns a new instance of RouteSet.



15
16
17
# File 'lib/api_valve/route_set.rb', line 15

def initialize
  reset_routes
end

Instance Method Details

#any(path = nil, prok = nil) ⇒ Object



59
60
61
# File 'lib/api_valve/route_set.rb', line 59

def any(path = nil, prok = nil)
  append METHODS, path, prok || Proc.new
end

#append(methods, regexp, prok = nil) ⇒ Object



63
64
65
66
67
68
# File 'lib/api_valve/route_set.rb', line 63

def append(methods, regexp, prok = nil)
  prok ||= Proc.new
  Array.wrap(methods).each do |method|
    @routes[method] << Route.new(regexp, prok)
  end
end

#delete(path = nil, prok = nil) ⇒ Object



35
36
37
# File 'lib/api_valve/route_set.rb', line 35

def delete(path = nil, prok = nil)
  append :delete, path, prok || Proc.new
end

#get(path = nil, prok = nil) ⇒ Object



39
40
41
# File 'lib/api_valve/route_set.rb', line 39

def get(path = nil, prok = nil)
  append :get, path, prok || Proc.new
end

#head(path = nil, prok = nil) ⇒ Object



43
44
45
# File 'lib/api_valve/route_set.rb', line 43

def head(path = nil, prok = nil)
  append :head, path, prok || Proc.new
end

#match(env) ⇒ Object

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/api_valve/route_set.rb', line 19

def match(env)
  request = Rack::Request.new(env)

  # For security reasons do not allow URLs that could break out of the proxy namespace on the
  # server. Preferably an nxing/apache rewrite will kill these URLs before they hit us
  raise 'URL not supported' if request.path_info.include?('/../')

  match_data = nil
  route = @routes && @routes[request.request_method.downcase.to_sym].find do |r|
    (match_data = r.match(request.path_info))
  end
  raise Error::NotRouted, 'Endpoint not found' unless route

  [route, match_data]
end

#patch(path = nil, prok = nil) ⇒ Object



47
48
49
# File 'lib/api_valve/route_set.rb', line 47

def patch(path = nil, prok = nil)
  append :patch, path, prok || Proc.new
end

#post(path = nil, prok = nil) ⇒ Object



51
52
53
# File 'lib/api_valve/route_set.rb', line 51

def post(path = nil, prok = nil)
  append :post, path, prok || Proc.new
end

#put(path = nil, prok = nil) ⇒ Object



55
56
57
# File 'lib/api_valve/route_set.rb', line 55

def put(path = nil, prok = nil)
  append :put, path, prok || Proc.new
end

#reset_routes ⇒ Object



77
78
79
# File 'lib/api_valve/route_set.rb', line 77

def reset_routes
  @routes = Hash[METHODS.map { |v| [v, []] }].freeze
end

#unshift(methods, regexp = nil, prok = nil) ⇒ Object



70
71
72
73
74
75
# File 'lib/api_valve/route_set.rb', line 70

def unshift(methods, regexp = nil, prok = nil)
  prok ||= Proc.new
  Array.wrap(methods).each do |method|
    @routes[method].unshift Route.new(regexp, prok)
  end
end