Module: Julewire::Rack::Capture::BodyContentType

Defined in:
lib/julewire/rack/capture/body_content_type.rb

Constant Summary collapse

JSON_ONLY =
["application/json", %r{\Aapplication/.+\+json\z}].freeze
BINARY_MEDIA_TYPES =
%w[
  application/gzip
  application/octet-stream
  application/pdf
  application/x-gzip
  application/zip
].freeze
BINARY_MEDIA_PREFIXES =
%w[
  audio/
  font/
  image/
  video/
].freeze

Class Method Summary collapse

Class Method Details

.allowed?(target, selector:) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
# File 'lib/julewire/rack/capture/body_content_type.rb', line 23

def allowed?(target, selector:)
  media_type = media_type_for(target)
  return false if binary?(media_type)
  return true if selector == true
  return false unless selector

  return false if media_type.empty?

  Array(selector).any? { matches?(media_type, it) }
end

.binary?(media_type) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/julewire/rack/capture/body_content_type.rb', line 34

def binary?(media_type)
  value = media_type.to_s
  return true if BINARY_MEDIA_TYPES.include?(value)

  BINARY_MEDIA_PREFIXES.any? { value.start_with?(it) }
end

.direct_content_type(target) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/julewire/rack/capture/body_content_type.rb', line 51

def direct_content_type(target)
  return target.media_type if target.respond_to?(:media_type)
  return target.content_mime_type.to_s if target.respond_to?(:content_mime_type) && target.content_mime_type
  return target.content_type if target.respond_to?(:content_type)

  target.get_header("CONTENT_TYPE") if target.respond_to?(:get_header)
end

.header_content_type(target) ⇒ Object



59
60
61
# File 'lib/julewire/rack/capture/body_content_type.rb', line 59

def header_content_type(target)
  header_value(target.headers, "content-type") if target.respond_to?(:headers)
end

.header_value(headers, key) ⇒ Object



63
64
65
66
67
# File 'lib/julewire/rack/capture/body_content_type.rb', line 63

def header_value(headers, key)
  return unless headers.respond_to?(:[])

  headers[key] || headers[key.upcase] || headers[key.split("-").map(&:capitalize).join("-")]
end

.matches?(media_type, matcher) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
# File 'lib/julewire/rack/capture/body_content_type.rb', line 69

def matches?(media_type, matcher)
  case matcher
  when Regexp
    matcher.match?(media_type)
  else
    media_type == normalized_media_type(matcher)
  end
end

.media_type_for(target) ⇒ Object



41
42
43
# File 'lib/julewire/rack/capture/body_content_type.rb', line 41

def media_type_for(target)
  normalized_media_type(raw_content_type(target))
end

.normalized_media_type(value) ⇒ Object



78
79
80
# File 'lib/julewire/rack/capture/body_content_type.rb', line 78

def normalized_media_type(value)
  value.to_s.partition(";").first.strip.downcase
end

.raw_content_type(target) ⇒ Object



45
46
47
48
49
# File 'lib/julewire/rack/capture/body_content_type.rb', line 45

def raw_content_type(target)
  direct_content_type(target) || header_content_type(target)
rescue StandardError
  nil
end