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

#initializeRouteSet

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, options = {}, prok = nil) ⇒ Object



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

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

#delete(path = nil, options = {}, prok = nil) ⇒ Object



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

def delete(path = nil, options = {}, prok = nil)
  push :delete, path, options, prok || Proc.new
end

#get(path = nil, options = {}, prok = nil) ⇒ Object



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

def get(path = nil, options = {}, prok = nil)
  push :get, path, options, prok || Proc.new
end

#head(path = nil, options = {}, prok = nil) ⇒ Object



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

def head(path = nil, options = {}, prok = nil)
  push :head, path, options, 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].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, options = {}, prok = nil) ⇒ Object



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

def patch(path = nil, options = {}, prok = nil)
  push :patch, path, options, prok || Proc.new
end

#post(path = nil, options = {}, prok = nil) ⇒ Object



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

def post(path = nil, options = {}, prok = nil)
  push :post, path, options, prok || Proc.new
end

#push(methods, regexp, options = {}, prok = nil) ⇒ Object Also known as: append



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

def push(methods, regexp, options = {}, prok = nil)
  add_route :push, methods, regexp, options, prok || Proc.new
end

#put(path = nil, options = {}, prok = nil) ⇒ Object



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

def put(path = nil, options = {}, prok = nil)
  push :put, path, options, prok || Proc.new
end

#reset_routesObject



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

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

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



69
70
71
# File 'lib/api_valve/route_set.rb', line 69

def unshift(methods, regexp = nil, options = {}, prok = nil)
  add_route :unshift, methods, regexp, options, prok || Proc.new
end