Skip to content

Commit ea7bbb7

Browse files
ericproulxdblock
andauthored
Refactor: Simplify mime_types_for implementation (#2639)
Replace each_with_object with invert.transform_keys! for a more idiomatic Ruby implementation. This also adds a performance optimization by only splitting strings that contain ';' instead of always splitting. Co-authored-by: Daniel (dB.) Doubrovkine <[email protected]>
1 parent a1e62ed commit ea7bbb7

File tree

2 files changed

+2
-4
lines changed

2 files changed

+2
-4
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+
* [#2639](https://github.com/ruby-grape/grape/pull/2639): Refactor mime_types_for - [@ericproulx](https://github.com/ericproulx).
89
* [#2638](https://github.com/ruby-grape/grape/pull/2638): Remove unnecessary path string duplication - [@ericproulx](https://github.com/ericproulx).
910
* Your contribution here.
1011

lib/grape/content_types.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ def content_types_for(from_settings)
2222
def mime_types_for(from_settings)
2323
return MIME_TYPES if from_settings == Grape::ContentTypes::DEFAULTS
2424

25-
from_settings.each_with_object({}) do |(k, v), types_without_params|
26-
# remove optional parameter
27-
types_without_params[v.split(';', 2).first] = k
28-
end
25+
from_settings.invert.transform_keys! { |k| k.include?(';') ? k.split(';', 2).first : k }
2926
end
3027
end
3128
end

0 commit comments

Comments
 (0)