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

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

Overview

rubocop:todo Style/Documentation

Constant Summary collapse

ACCEPTABLE_TYPES =
if Rails.env.development?
  ['application/json', '*/*'].freeze
else
  ['application/json'].freeze
end

Instance Method Summary collapse

Instance Method Details

#acceptable_typesObject


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

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

#check_acceptable_content_type_requested!Object


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

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


19
20
21
# File 'app/api/core/service/content_filtering.rb', line 19

def json
  @json
end

#process_request_bodyObject


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/api/core/service/content_filtering.rb', line 23

def process_request_body
  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


37
38
39
# File 'app/api/core/service/content_filtering.rb', line 37

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

#request_acceptedObject


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

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