Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content types registrable #2005

Merged
merged 3 commits into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
3 changes: 2 additions & 1 deletion lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
34 changes: 34 additions & 0 deletions lib/grape/content_types.rb
Original file line number Diff line number Diff line change
@@ -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
28 changes: 0 additions & 28 deletions lib/grape/util/content_types.rb

This file was deleted.

4 changes: 2 additions & 2 deletions spec/grape/middleware/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down