Class: Grape::Router::Route

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/grape/router/route.rb

Constant Summary collapse

ROUTE_ATTRIBUTE_REGEXP =
/route_([_a-zA-Z]\w*)/.freeze
SOURCE_LOCATION_REGEXP =
/^(.*?):(\d+?)(?::in `.+?')?$/.freeze
TRANSLATION_ATTRIBUTES =
[
  :prefix,
  :version,
  :namespace,
  :settings,
  :format,
  :description,
  :http_codes,
  :headers,
  :entity,
  :details,
  :requirements,
  :request_method
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, pattern, options = {}) ⇒ Route

Returns a new instance of Route.



61
62
63
64
65
66
# File 'lib/grape/router/route.rb', line 61

def initialize(method, pattern, options = {})
  @suffix     = options[:suffix]
  @options    = options.merge(method: method.to_s.upcase)
  @pattern    = Pattern.new(pattern, options)
  @translator = AttributeTranslator.new(options.merge(request_method: method.to_s.upcase))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *arguments) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/grape/router/route.rb', line 40

def method_missing(method_id, *arguments)
  match = ROUTE_ATTRIBUTE_REGEXP.match(method_id.to_s)
  if match
    method_name = match.captures.last.to_sym
    warn_route_methods(method_name, caller(1).shift)
    @options[method_name]
  else
    super
  end
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



26
27
28
# File 'lib/grape/router/route.rb', line 26

def app
  @app
end

#indexObject

Returns the value of attribute index.



26
27
28
# File 'lib/grape/router/route.rb', line 26

def index
  @index
end

#patternObject

Returns the value of attribute pattern.



26
27
28
# File 'lib/grape/router/route.rb', line 26

def pattern
  @pattern
end

#regexpObject

Returns the value of attribute regexp.



26
27
28
# File 'lib/grape/router/route.rb', line 26

def regexp
  @regexp
end

#translatorObject Also known as: attributes

Returns the value of attribute translator.



26
27
28
# File 'lib/grape/router/route.rb', line 26

def translator
  @translator
end

Class Method Details

.translate(*attributes) ⇒ Object



33
34
35
36
# File 'lib/grape/router/route.rb', line 33

def self.translate(*attributes)
  AttributeTranslator.register(*attributes)
  def_delegators :@translator, *attributes
end

Instance Method Details

#apply(app) ⇒ Object



72
73
74
75
# File 'lib/grape/router/route.rb', line 72

def apply(app)
  @app = app
  self
end

#exec(env) ⇒ Object



68
69
70
# File 'lib/grape/router/route.rb', line 68

def exec(env)
  @app.call(env)
end

#match?(input) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/grape/router/route.rb', line 77

def match?(input)
  translator.respond_to?(:forward_match) && translator.forward_match ? input.start_with?(pattern.origin) : pattern.match?(input)
end

#params(input = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/grape/router/route.rb', line 81

def params(input = nil)
  if input.nil?
    default = pattern.named_captures.keys.each_with_object({}) do |key, defaults|
      defaults[key] = ''
    end
    default.delete_if { |key, _| key == 'format' }.merge(translator.params)
  else
    parsed = pattern.params(input)
    parsed ? parsed.delete_if { |_, value| value.nil? }.symbolize_keys : {}
  end
end

#route_methodObject



51
52
53
54
# File 'lib/grape/router/route.rb', line 51

def route_method
  warn_route_methods(:method, caller(1).shift, :request_method)
  request_method
end

#route_pathObject



56
57
58
59
# File 'lib/grape/router/route.rb', line 56

def route_path
  warn_route_methods(:path, caller(1).shift)
  pattern.path
end