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

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

Constant Summary collapse

ACCEPTABLE_TYPES =
['application/json']

Instance Method Summary collapse

Instance Method Details

#acceptable_typesObject


44
45
46
# File 'app/api/core/service/content_filtering.rb', line 44

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

#check_acceptable_content_type_requested!Object


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

def check_acceptable_content_type_requested!
  accepts_json_or_star = !request.acceptable_media_types.prioritize(*acceptable_types).blank?
  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_json_response_bodyObject


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

def process_json_response_body
  headers('Content-Type' => 'application/json')
end

#process_request_bodyObject


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

def process_request_body
  content = request.body.read
  raise Core::Service::ContentFiltering::InvalidBodyContentType if not content.blank? and !acceptable_types.include?(request.content_type)

  @json = content.blank? ? {} : MultiJson.load(content) if request.content_type == 'application/json' || content.blank?
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


33
34
35
# File 'app/api/core/service/content_filtering.rb', line 33

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

#request_acceptedObject


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

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