Class: Webmate::Route
- Inherits:
-
Object
- Object
- Webmate::Route
- Defined in:
- lib/webmate/route_helpers/route.rb
Constant Summary collapse
- FIELDS =
[:method, :path, :action, :transport, :responder, :route_regexp, :static_params]
Instance Method Summary collapse
-
#initialize(args) ⇒ Route
constructor
method: GET/POST/PUT/DELETE path : /user/123/posts/123/comments transport: HTTP/WS/ responder: class, responsible to ‘respond’ action action: method in webmate responders, called to fetch data static params: additional params hash, which will be passed to responder for example, { :scope => :user }.
-
#match(request_path) ⇒ Object
method should check coincidence of path pattern and given path ‘/projects/qwerty123/tasks/asdf13/comments/zxcv123’ will be parsed with route /projects/:project_id/tasks/:task_id/comments/:comment_id and return result = { action: ‘read’, responder: CommentsResponder, params: { project_id: ‘qwerty123’, task_id: :asdf13, comment_id: :zxcv123 } }.
Constructor Details
#initialize(args) ⇒ Route
method: GET/POST/PUT/DELETE path : /user/123/posts/123/comments transport: HTTP/WS/ responder: class, responsible to ‘respond’ action action: method in webmate responders, called to fetch data static params: additional params hash, which will be passed to responder
for example, { :scope => :user }
14 15 16 17 18 19 20 21 22 |
# File 'lib/webmate/route_helpers/route.rb', line 14 def initialize(args) values = args.with_indifferent_access FIELDS.each do |field_name| instance_variable_set("@#{field_name.to_s}", values[field_name]) end normalize_data_if_needed @route_regexp ||= construct_match_regexp end |
Instance Method Details
#match(request_path) ⇒ Object
method should check coincidence of path pattern and given path ‘/projects/qwerty123/tasks/asdf13/comments/zxcv123’ will be parsed with route /projects/:project_id/tasks/:task_id/comments/:comment_id and return
result = {
action: 'read',
responder: CommentsResponder,
params: {
project_id: 'qwerty123',
task_id: :asdf13,
comment_id: :zxcv123
}
}
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/webmate/route_helpers/route.rb', line 39 def match(request_path) if match_data = @route_regexp.match(request_path) route_data = { action: @action, responder: @responder, params: HashWithIndifferentAccess.new(static_params || {}) } @substitution_attrs.each_with_index do |key, index| if key == :splat route_data[:params][key] ||= [] route_data[:params][key] += match_data[index.next].split('/') else route_data[:params][key] = match_data[index.next] end end route_data else nil # not matched. end end |