Class: Apia::RouteSet
- Inherits:
-
Object
- Object
- Apia::RouteSet
- Defined in:
- lib/apia/route_set.rb
Instance Attribute Summary collapse
-
#controllers ⇒ Object
readonly
Returns the value of attribute controllers.
-
#endpoints ⇒ Object
readonly
Returns the value of attribute endpoints.
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#map ⇒ Object
readonly
Returns the value of attribute map.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Class Method Summary collapse
-
.split_path(path) ⇒ Array<String>
Split a URL part into its appropriate parts.
-
.strip_slashes(string) ⇒ String
Remove slashes from the start and end of a given string.
Instance Method Summary collapse
-
#add(route) ⇒ Moonstone::Route
Add a new route to the set.
- #dsl ⇒ Object
-
#find(request_method, path) ⇒ Array<Moonstone::Route>
Find routes that exactly match a given path.
-
#initialize ⇒ RouteSet
constructor
A new instance of RouteSet.
Constructor Details
#initialize ⇒ RouteSet
Returns a new instance of RouteSet.
14 15 16 17 18 19 20 |
# File 'lib/apia/route_set.rb', line 14 def initialize @map = {} @routes = [] @controllers = [] @endpoints = [] @groups = [] end |
Instance Attribute Details
#controllers ⇒ Object (readonly)
Returns the value of attribute controllers.
10 11 12 |
# File 'lib/apia/route_set.rb', line 10 def controllers @controllers end |
#endpoints ⇒ Object (readonly)
Returns the value of attribute endpoints.
11 12 13 |
# File 'lib/apia/route_set.rb', line 11 def endpoints @endpoints end |
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
12 13 14 |
# File 'lib/apia/route_set.rb', line 12 def groups @groups end |
#map ⇒ Object (readonly)
Returns the value of attribute map.
8 9 10 |
# File 'lib/apia/route_set.rb', line 8 def map @map end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
9 10 11 |
# File 'lib/apia/route_set.rb', line 9 def routes @routes end |
Class Method Details
.split_path(path) ⇒ Array<String>
Split a URL part into its appropriate parts
82 83 84 |
# File 'lib/apia/route_set.rb', line 82 def split_path(path) strip_slashes(path).split('/') end |
.strip_slashes(string) ⇒ String
Remove slashes from the start and end of a given string
74 75 76 |
# File 'lib/apia/route_set.rb', line 74 def strip_slashes(string) string.sub(/\A\/+/, '').sub(/\/\z/, '') end |
Instance Method Details
#add(route) ⇒ Moonstone::Route
Add a new route to the set
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/apia/route_set.rb', line 30 def add(route) @routes << route if route.controller && !@controllers.include?(route.controller) @controllers << route.controller end if route.endpoint && !@controllers.include?(route.endpoint) @endpoints << route.endpoint end parts = self.class.split_path(route.path).map { |p| p =~ /\A:/ ? '?' : p } parts.size.times do |i| if i.zero? source = @map else source = @map.dig(*parts[0, i]) end source[parts[i]] ||= { _routes: [] } source[parts[i]][:_routes] << route if i == parts.size - 1 end route end |
#dsl ⇒ Object
22 23 24 |
# File 'lib/apia/route_set.rb', line 22 def dsl @dsl ||= DSLs::RouteSet.new(self) end |
#find(request_method, path) ⇒ Array<Moonstone::Route>
Find routes that exactly match a given path
58 59 60 61 62 63 64 65 66 |
# File 'lib/apia/route_set.rb', line 58 def find(request_method, path) parts = self.class.split_path(path) last = @map parts.size.times do |i| last = last[parts[i]] || last['?'] return [] if last.nil? end last[:_routes].select { |r| r.request_method == request_method } end |