Class: GrapePathHelpers::DecoratedRoute

Inherits:
Object
  • Object
show all
Defined in:
lib/grape-path-helpers/decorated_route.rb

Overview

wrapper around Grape::Route that adds a helper method

Constant Summary collapse

PATH_SEGMENTS_REGEXP =
%r{\(/?\.:?\w+\)|/(?!\))|(?<=\))|\??\*}
PATH_SEGMENTS_WITH_WILDCARDS_REGEXP =
%r{\(/?\.:?\w+\)|/(?!\))|(?<=\))|\?}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route) ⇒ DecoratedRoute

Returns a new instance of DecoratedRoute.



16
17
18
19
20
21
22
23
# File 'lib/grape-path-helpers/decorated_route.rb', line 16

def initialize(route)
  @route = route
  @route_options = route.options
  @helper_names = []
  @helper_arguments = required_helper_segments
  @extension = default_extension
  define_path_helpers
end

Instance Attribute Details

#extensionObject (readonly)

Returns the value of attribute extension.



6
7
8
# File 'lib/grape-path-helpers/decorated_route.rb', line 6

def extension
  @extension
end

#helper_argumentsObject (readonly)

Returns the value of attribute helper_arguments.



6
7
8
# File 'lib/grape-path-helpers/decorated_route.rb', line 6

def helper_arguments
  @helper_arguments
end

#helper_namesObject (readonly)

Returns the value of attribute helper_names.



6
7
8
# File 'lib/grape-path-helpers/decorated_route.rb', line 6

def helper_names
  @helper_names
end

#routeObject (readonly)

Returns the value of attribute route.



6
7
8
# File 'lib/grape-path-helpers/decorated_route.rb', line 6

def route
  @route
end

#route_optionsObject (readonly)

Returns the value of attribute route_options.



6
7
8
# File 'lib/grape-path-helpers/decorated_route.rb', line 6

def route_options
  @route_options
end

Class Method Details

.sanitize_method_name(string) ⇒ Object



12
13
14
# File 'lib/grape-path-helpers/decorated_route.rb', line 12

def self.sanitize_method_name(string)
  string.gsub(/\W|^[0-9]/, '_')
end

Instance Method Details

#default_extensionObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/grape-path-helpers/decorated_route.rb', line 25

def default_extension
  pattern = /\((\.:?\w+)\)$/
  match = route_path.match(pattern)
  return '' unless match

  ext = match.captures.first
  if ext == '.:format'
    ''
  else
    ext
  end
end

#define_path_helper(method_name, route_attributes) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/grape-path-helpers/decorated_route.rb', line 47

def define_path_helper(method_name, route_attributes)
  method_body = <<-RUBY
    def #{method_name}(attributes = {}, include_wildcard_segments = false)
      attrs = #{route_attributes}.merge(attributes)

      query_params = attrs.delete(:params)
      content_type = attrs.delete(:format)
      path = '/' + path_segments_with_values(attrs, include_wildcard_segments).join('/')

      path + content_type + query_string(query_params)
    end
  RUBY
  instance_eval method_body
end

#define_path_helpersObject



38
39
40
41
42
43
44
45
# File 'lib/grape-path-helpers/decorated_route.rb', line 38

def define_path_helpers
  route_versions.each do |version|
    route_attributes = { version: version, format: extension }
    method_name = path_helper_name(route_attributes)
    @helper_names << method_name
    define_path_helper(method_name, route_attributes)
  end
end

#dynamic_path_segmentsObject

rubocop:enable Style/OptionalBooleanParameter



126
127
128
129
130
131
# File 'lib/grape-path-helpers/decorated_route.rb', line 126

def dynamic_path_segments
  segments = path_segments.select do |segment|
    dynamic_segment?(segment)
  end
  segments.map { |s| s.slice(1..-1) }
end

#dynamic_segment?(segment) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/grape-path-helpers/decorated_route.rb', line 133

def dynamic_segment?(segment)
  segment.start_with?(':', '*')
end

#optional_segment?(segment) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/grape-path-helpers/decorated_route.rb', line 137

def optional_segment?(segment)
  segment.start_with?('(')
end

#path_helper_name(opts = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/grape-path-helpers/decorated_route.rb', line 81

def path_helper_name(opts = {})
  if route_options[:as]
    name = route_options[:as].to_s
  else
    segments = path_segments_with_values(opts)

    name = if segments.empty?
             'root'
           else
             segments.join('_')
           end
  end

  sanitized_name = self.class.sanitize_method_name(name)
  "#{sanitized_name}_path"
end

#path_segments(include_wildcard_segments = false) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/grape-path-helpers/decorated_route.rb', line 116

def path_segments(include_wildcard_segments = false)
  pattern = if include_wildcard_segments
              PATH_SEGMENTS_WITH_WILDCARDS_REGEXP
            else
              PATH_SEGMENTS_REGEXP
            end
  route_path.split(pattern).reject(&:blank?)
end

#path_segments_with_values(opts, include_wildcard_segments = false) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter



109
110
111
112
113
114
# File 'lib/grape-path-helpers/decorated_route.rb', line 109

def path_segments_with_values(opts, include_wildcard_segments = false)
  segments = path_segments(include_wildcard_segments).map do |s|
    segment_to_value(s, opts)
  end
  segments.reject(&:blank?)
end

#query_string(params) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/grape-path-helpers/decorated_route.rb', line 62

def query_string(params)
  if params.nil?
    ''
  else
    "?#{params.to_param}"
  end
end

#required_helper_segmentsObject



141
142
143
144
145
146
# File 'lib/grape-path-helpers/decorated_route.rb', line 141

def required_helper_segments
  segments_in_options = dynamic_path_segments.select do |segment|
    route.options[segment.to_sym]
  end
  dynamic_path_segments - segments_in_options
end

#route_methodObject



174
175
176
# File 'lib/grape-path-helpers/decorated_route.rb', line 174

def route_method
  route.request_method
end

#route_namespaceObject



170
171
172
# File 'lib/grape-path-helpers/decorated_route.rb', line 170

def route_namespace
  route.namespace
end

#route_pathObject



162
163
164
# File 'lib/grape-path-helpers/decorated_route.rb', line 162

def route_path
  route.path
end

#route_versionObject



166
167
168
# File 'lib/grape-path-helpers/decorated_route.rb', line 166

def route_version
  route.version
end

#route_versionsObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/grape-path-helpers/decorated_route.rb', line 70

def route_versions
  return [nil] if route_version.nil? || route_version.empty?

  if route_version.is_a?(String)
    version_pattern = /[^\[",\]\s]+/
    route_version.scan(version_pattern)
  else
    route_version
  end
end

#segment_to_value(segment, opts = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/grape-path-helpers/decorated_route.rb', line 98

def segment_to_value(segment, opts = {})
  if dynamic_segment?(segment)
    options = route.options.merge(stringify_keys(opts))
    key = segment.slice(1..-1).to_sym
    options[key]
  else
    segment
  end
end

#special_keysObject



148
149
150
# File 'lib/grape-path-helpers/decorated_route.rb', line 148

def special_keys
  %w[format params]
end

#uses_segments_in_path_helper?(segments) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
155
156
157
158
159
160
# File 'lib/grape-path-helpers/decorated_route.rb', line 152

def uses_segments_in_path_helper?(segments)
  segments = segments.reject { |x| special_keys.include?(x) }

  if helper_arguments.empty? && segments.any?
    false
  else
    helper_arguments.all? { |x| segments.include?(x) }
  end
end