Skip to content

Commit 2c3592e

Browse files
ericproulxdblock
andauthored
Refactor: Move available_media_types to base class and compute once (#2640)
Move available_media_types method from Header to Base class and compute it once during initialization instead of on each call. This improves performance and code organization by moving shared logic to the base class. Co-authored-by: Daniel (dB.) Doubrovkine <[email protected]>
1 parent 7ce67e5 commit 2c3592e

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* [#2629](https://github.com/ruby-grape/grape/pull/2629): Refactor Router Architecture - [@ericproulx](https://github.com/ericproulx).
66
* [#2633](https://github.com/ruby-grape/grape/pull/2633): Refactor API::Instance and reorganize DSL modules - [@ericproulx](https://github.com/ericproulx).
77
* [#2636](https://github.com/ruby-grape/grape/pull/2636): Refactor router to simplify method signatures and reduce duplication - [@ericproulx](https://github.com/ericproulx).
8+
* [#2640](https://github.com/ruby-grape/grape/pull/2640): Compute available_media_types once - [@ericproulx](https://github.com/ericproulx).
89
* [#2637](https://github.com/ruby-grape/grape/pull/2637): Refactor declared method - [@ericproulx](https://github.com/ericproulx).
910
* [#2639](https://github.com/ruby-grape/grape/pull/2639): Refactor mime_types_for - [@ericproulx](https://github.com/ericproulx).
1011
* [#2638](https://github.com/ruby-grape/grape/pull/2638): Remove unnecessary path string duplication - [@ericproulx](https://github.com/ericproulx).

lib/grape/middleware/versioner/base.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ def potential_version_match?(potential_version)
5050
def version_not_found!
5151
throw :error, status: 404, message: '404 API Version Not Found', headers: CASCADE_PASS_HEADER
5252
end
53+
54+
private
55+
56+
def available_media_types
57+
@available_media_types ||= begin
58+
media_types = []
59+
base_media_type = "application/vnd.#{vendor}"
60+
content_types.each_key do |extension|
61+
versions&.reverse_each do |version|
62+
media_types << "#{base_media_type}-#{version}+#{extension}"
63+
media_types << "#{base_media_type}-#{version}"
64+
end
65+
media_types << "#{base_media_type}+#{extension}"
66+
end
67+
68+
media_types << base_media_type
69+
media_types.concat(content_types.values.flatten)
70+
media_types
71+
end
72+
end
5373
end
5474
end
5575
end

lib/grape/middleware/versioner/header.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,6 @@ def version_not_found!(media_types)
105105

106106
invalid_version_header!('API version not found.')
107107
end
108-
109-
def available_media_types
110-
[].tap do |available_media_types|
111-
base_media_type = "application/vnd.#{vendor}"
112-
content_types.each_key do |extension|
113-
versions&.reverse_each do |version|
114-
available_media_types << "#{base_media_type}-#{version}+#{extension}"
115-
available_media_types << "#{base_media_type}-#{version}"
116-
end
117-
available_media_types << "#{base_media_type}+#{extension}"
118-
end
119-
120-
available_media_types << base_media_type
121-
available_media_types.concat(content_types.values.flatten)
122-
end
123-
end
124108
end
125109
end
126110
end

0 commit comments

Comments
 (0)