diff --git a/CHANGELOG.md b/CHANGELOG.md index 11bc0456d6..a65afb9e33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ #### Features * Your contribution here. +* [#2005](https://github.com/ruby-grape/grape/pull/2005): Content types registrable - [@ericproulx](https://github.com/ericproulx). * [#2003](https://github.com/ruby-grape/grape/pull/2003): Upgraded Rubocop to 0.80.1 - [@ericproulx](https://github.com/ericproulx). * [#2002](https://github.com/ruby-grape/grape/pull/2002): Objects allocation optimization (lazy_lookup) - [@ericproulx](https://github.com/ericproulx). diff --git a/lib/grape.rb b/lib/grape.rb index e5fcc98023..125a2b4c63 100644 --- a/lib/grape.rb +++ b/lib/grape.rb @@ -218,7 +218,8 @@ module ServeFile end require 'grape/config' -require 'grape/util/content_types' +require 'grape/content_types' + require 'grape/util/lazy_value' require 'grape/util/lazy_block' require 'grape/util/endpoint_configuration' diff --git a/lib/grape/content_types.rb b/lib/grape/content_types.rb new file mode 100644 index 0000000000..2c19f97317 --- /dev/null +++ b/lib/grape/content_types.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require 'grape/util/registrable' + +module Grape + module ContentTypes + extend Util::Registrable + + # Content types are listed in order of preference. + CONTENT_TYPES = { + xml: 'application/xml', + serializable_hash: 'application/json', + json: 'application/json', + binary: 'application/octet-stream', + txt: 'text/plain' + }.freeze + + class << self + def content_types_for_settings(settings) + return if settings.blank? + + settings.each_with_object({}) { |value, result| result.merge!(value) } + end + + def content_types_for(from_settings) + if from_settings.present? + from_settings + else + Grape::ContentTypes::CONTENT_TYPES.merge(default_elements) + end + end + end + end +end diff --git a/lib/grape/util/content_types.rb b/lib/grape/util/content_types.rb deleted file mode 100644 index df5a7b008c..0000000000 --- a/lib/grape/util/content_types.rb +++ /dev/null @@ -1,28 +0,0 @@ -# frozen_string_literal: true - -module Grape - module ContentTypes - # Content types are listed in order of preference. - CONTENT_TYPES = { # rubocop:disable Style/MutableConstant - xml: 'application/xml', - serializable_hash: 'application/json', - json: 'application/json', - binary: 'application/octet-stream', - txt: 'text/plain' - } - - def self.content_types_for_settings(settings) - return if settings.blank? - - settings.each_with_object({}) { |value, result| result.merge!(value) } - end - - def self.content_types_for(from_settings) - if from_settings.present? - from_settings - else - Grape::ContentTypes::CONTENT_TYPES - end - end - end -end diff --git a/spec/grape/middleware/formatter_spec.rb b/spec/grape/middleware/formatter_spec.rb index 102bca8f15..7c73663b54 100644 --- a/spec/grape/middleware/formatter_spec.rb +++ b/spec/grape/middleware/formatter_spec.rb @@ -402,10 +402,10 @@ def self.call(_, _) let(:app) { ->(_env) { [200, {}, ['']] } } before do Grape::Formatter.register :invalid, InvalidFormatter - Grape::ContentTypes::CONTENT_TYPES[:invalid] = 'application/x-invalid' + Grape::ContentTypes.register :invalid, 'application/x-invalid' end after do - Grape::ContentTypes::CONTENT_TYPES.delete(:invalid) + Grape::ContentTypes.default_elements.delete(:invalid) Grape::Formatter.default_elements.delete(:invalid) end