Class: Apia::RouteSet

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouteSet

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

#controllersObject (readonly)

Returns the value of attribute controllers.



10
11
12
# File 'lib/apia/route_set.rb', line 10

def controllers
  @controllers
end

#endpointsObject (readonly)

Returns the value of attribute endpoints.



11
12
13
# File 'lib/apia/route_set.rb', line 11

def endpoints
  @endpoints
end

#groupsObject (readonly)

Returns the value of attribute groups.



12
13
14
# File 'lib/apia/route_set.rb', line 12

def groups
  @groups
end

#mapObject (readonly)

Returns the value of attribute map.



8
9
10
# File 'lib/apia/route_set.rb', line 8

def map
  @map
end

#routesObject (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

Parameters:

  • path (String)

Returns:

  • (Array<String>)


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

Parameters:

  • string (String)

Returns:

  • (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

Parameters:

  • route (Moonstone::Route)

Returns:

  • (Moonstone::Route)


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

#dslObject



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

Parameters:

  • request_method (Symbol)
  • path (String)

Returns:

  • (Array<Moonstone::Route>)


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