Module: Core::Service::ContentFiltering::Helpers

Defined in:
app/api/core/service/content_filtering.rb

Constant Summary collapse

ACCEPTABLE_TYPES =
Rails.env.development? ? %w[application/json */*].freeze : ['application/json'].freeze

Instance Method Summary collapse

Instance Method Details

#acceptable_typesObject



46
47
48
# File 'app/api/core/service/content_filtering.rb', line 46

def acceptable_types
  ACCEPTABLE_TYPES + ::Api::EndpointHandler.registered_mimetypes
end

#check_acceptable_content_type_requested!Object



50
51
52
53
# File 'app/api/core/service/content_filtering.rb', line 50

def check_acceptable_content_type_requested!
  accepts_json_or_star = request.acceptable_media_types.prioritize(*acceptable_types).present?
  raise Core::Service::ContentFiltering::InvalidRequestedContentType unless accepts_json_or_star
end

#jsonObject



22
23
24
# File 'app/api/core/service/content_filtering.rb', line 22

def json
  @json
end

#process_request_bodyObject

rubocop:todo Metrics/AbcSize



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/api/core/service/content_filtering.rb', line 26

def process_request_body # rubocop:todo Metrics/AbcSize
  content = request.body.read
  if content.present? && acceptable_types.exclude?(request.content_type)
    raise Core::Service::ContentFiltering::InvalidBodyContentType
  end

  if request.content_type == 'application/json' || content.blank?
    @json = content.blank? ? {} : MultiJson.load(content)
  end
ensure
  # It's important to ensure that the body IO object has been rewound to the start for other requests.
  request.body.rewind
end

#process_response_bodyObject



40
41
42
# File 'app/api/core/service/content_filtering.rb', line 40

def process_response_body
  headers('Content-Type' => request_accepted.first)
end

#request_acceptedObject



55
56
57
# File 'app/api/core/service/content_filtering.rb', line 55

def request_accepted
  request.acceptable_media_types.prioritize(*acceptable_types).map(&:to_s)
end