Class: AcceptableApi::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/acceptable_api/route.rb

Instance Method Summary collapse

Constructor Details

#initialize(constraints, controller_action) ⇒ Route

Returns a new instance of Route.



8
9
10
11
# File 'lib/acceptable_api/route.rb', line 8

def initialize constraints, controller_action
  self.constraints = constraints
  self.controller_action = controller_action
end

Instance Method Details

#controller_classObject



36
37
38
39
40
# File 'lib/acceptable_api/route.rb', line 36

def controller_class
  controller_class_name.split(/::/).inject(Object) { |scope, const_name|
    scope.const_get const_name
  }
end

#controller_class_nameObject



42
43
44
# File 'lib/acceptable_api/route.rb', line 42

def controller_class_name
  controller_action.split(/#/, 2)[0]
end

#controller_for_request(request) ⇒ Object



32
33
34
# File 'lib/acceptable_api/route.rb', line 32

def controller_for_request request
  controller_class.new params_from(request), controller_method
end

#controller_methodObject



46
47
48
# File 'lib/acceptable_api/route.rb', line 46

def controller_method
  controller_action.split(/#/, 2)[-1]
end

#match?(request) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
# File 'lib/acceptable_api/route.rb', line 13

def match? request
  return false unless constraints[:via].upcase == request.request_method
  return false unless path_regex.match request.path
  true
end

#params_from(request) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/acceptable_api/route.rb', line 24

def params_from request
  matches = path_regex.match request.path
  matches.names.inject({}) { |a,e|
    a.merge! e.to_sym => matches[e]
    a
  }
end

#path_regexObject



19
20
21
22
# File 'lib/acceptable_api/route.rb', line 19

def path_regex
  named_captures = constraints[:at].gsub /\:([^\/]+)/, '(?<\1>[^\/]+)'
  Regexp.new "^#{named_captures}$"
end