Class: Webmate::Routes::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/webmate/routes/collection.rb

Constant Summary collapse

TRANSPORTS =
[:ws, :http]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCollection

Returns a new instance of Collection.



7
8
9
10
11
12
# File 'lib/webmate/routes/collection.rb', line 7

def initialize
  @routes = {}
  @resource_scope = []

  enable_websockets if configatron.websockets.enabled
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



5
6
7
# File 'lib/webmate/routes/collection.rb', line 5

def routes
  @routes
end

Instance Method Details

#define(&block) ⇒ Object

Call this method to define routes in application Examples:

routes = Webmate::Routes::Collection.new
routes.define do
  resources :projects
end


20
21
22
# File 'lib/webmate/routes/collection.rb', line 20

def define(&block)
  instance_eval(&block)
end

#get_routes(method, transport) ⇒ Object

Get routes by method and transport

Parameters:

  • String

    method - GET/POST/PUT/PATCH/DELETE

  • String

    transport - HTTP / WS [ HTTPS / WSS ]

Returns:

  • Array list of routes



44
45
46
47
# File 'lib/webmate/routes/collection.rb', line 44

def get_routes(method, transport)
  @routes[method] ||= {}
  @routes[method][transport] || []
end

#match(method, transport, path) ⇒ Hash?

Get list of matched routes

Parameters:

  • String

    method - GET/POST/PUT/PATCH/DELETE

  • String

    transport - HTTP / WS [ HTTPS / WSS ]

  • String

    path - /projects/123/tasks

Returns:

  • (Hash, nil)


30
31
32
33
34
35
36
37
# File 'lib/webmate/routes/collection.rb', line 30

def match(method, transport, path)
  get_routes(method, transport.upcase).each do |route|
    if info = route.match(path)
      return info
    end
  end
  nil
end