Skip to content

Commit fbeedb6

Browse files
authored
Content types registrable (#2005)
1 parent 83f9ca7 commit fbeedb6

File tree

5 files changed

+39
-31
lines changed

5 files changed

+39
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#### Features
44

55
* Your contribution here.
6+
* [#2005](https://github.com/ruby-grape/grape/pull/2005): Content types registrable - [@ericproulx](https://github.com/ericproulx).
67
* [#2003](https://github.com/ruby-grape/grape/pull/2003): Upgraded Rubocop to 0.80.1 - [@ericproulx](https://github.com/ericproulx).
78
* [#2002](https://github.com/ruby-grape/grape/pull/2002): Objects allocation optimization (lazy_lookup) - [@ericproulx](https://github.com/ericproulx).
89

lib/grape.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ module ServeFile
218218
end
219219

220220
require 'grape/config'
221-
require 'grape/util/content_types'
221+
require 'grape/content_types'
222+
222223
require 'grape/util/lazy_value'
223224
require 'grape/util/lazy_block'
224225
require 'grape/util/endpoint_configuration'

lib/grape/content_types.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require 'grape/util/registrable'
4+
5+
module Grape
6+
module ContentTypes
7+
extend Util::Registrable
8+
9+
# Content types are listed in order of preference.
10+
CONTENT_TYPES = {
11+
xml: 'application/xml',
12+
serializable_hash: 'application/json',
13+
json: 'application/json',
14+
binary: 'application/octet-stream',
15+
txt: 'text/plain'
16+
}.freeze
17+
18+
class << self
19+
def content_types_for_settings(settings)
20+
return if settings.blank?
21+
22+
settings.each_with_object({}) { |value, result| result.merge!(value) }
23+
end
24+
25+
def content_types_for(from_settings)
26+
if from_settings.present?
27+
from_settings
28+
else
29+
Grape::ContentTypes::CONTENT_TYPES.merge(default_elements)
30+
end
31+
end
32+
end
33+
end
34+
end

lib/grape/util/content_types.rb

Lines changed: 0 additions & 28 deletions
This file was deleted.

spec/grape/middleware/formatter_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,10 +402,10 @@ def self.call(_, _)
402402
let(:app) { ->(_env) { [200, {}, ['']] } }
403403
before do
404404
Grape::Formatter.register :invalid, InvalidFormatter
405-
Grape::ContentTypes::CONTENT_TYPES[:invalid] = 'application/x-invalid'
405+
Grape::ContentTypes.register :invalid, 'application/x-invalid'
406406
end
407407
after do
408-
Grape::ContentTypes::CONTENT_TYPES.delete(:invalid)
408+
Grape::ContentTypes.default_elements.delete(:invalid)
409409
Grape::Formatter.default_elements.delete(:invalid)
410410
end
411411

0 commit comments

Comments
 (0)