Class: Grape::Util::AcceptHeaderHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/util/accept_header_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(accept_header:, versions:, **options) ⇒ AcceptHeaderHandler

Returns a new instance of AcceptHeaderHandler.



8
9
10
11
12
13
14
# File 'lib/grape/util/accept_header_handler.rb', line 8

def initialize(accept_header:, versions:, **options)
  @accept_header = accept_header
  @versions = versions
  @vendor = options.fetch(:vendor, nil)
  @strict = options.fetch(:strict, false)
  @cascade = options.fetch(:cascade, true)
end

Instance Attribute Details

#accept_headerObject (readonly)

Returns the value of attribute accept_header.



6
7
8
# File 'lib/grape/util/accept_header_handler.rb', line 6

def accept_header
  @accept_header
end

#cascadeObject (readonly)

Returns the value of attribute cascade.



6
7
8
# File 'lib/grape/util/accept_header_handler.rb', line 6

def cascade
  @cascade
end

#strictObject (readonly)

Returns the value of attribute strict.



6
7
8
# File 'lib/grape/util/accept_header_handler.rb', line 6

def strict
  @strict
end

#vendorObject (readonly)

Returns the value of attribute vendor.



6
7
8
# File 'lib/grape/util/accept_header_handler.rb', line 6

def vendor
  @vendor
end

#versionsObject (readonly)

Returns the value of attribute versions.



6
7
8
# File 'lib/grape/util/accept_header_handler.rb', line 6

def versions
  @versions
end

Instance Method Details

#match_best_quality_media_type!(content_types: Grape::ContentTypes::CONTENT_TYPES, allowed_methods: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/grape/util/accept_header_handler.rb', line 16

def match_best_quality_media_type!(content_types: Grape::ContentTypes::CONTENT_TYPES, allowed_methods: nil)
  return unless vendor

  strict_header_checks!
  media_type = Grape::Util::MediaType.best_quality(accept_header, available_media_types(content_types))
  if media_type
    yield media_type
  else
    fail!(allowed_methods)
  end
end