Class: Whoosh::Serialization::Negotiator

Inherits:
Object
  • Object
show all
Defined in:
lib/whoosh/serialization/negotiator.rb

Constant Summary collapse

SERIALIZERS =
{
  "application/json" => -> { Json },
  "application/msgpack" => -> { Msgpack },
  "application/x-msgpack" => -> { Msgpack },
  "application/protobuf" => -> { Protobuf },
  "application/x-protobuf" => -> { Protobuf }
}.freeze

Class Method Summary collapse

Class Method Details

.for_accept(accept_header) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/whoosh/serialization/negotiator.rb', line 14

def self.for_accept(accept_header)
  return Json if accept_header.nil? || accept_header.empty?

  accept_header.split(",").each do |media_type|
    type = media_type.strip.split(";").first.strip
    return Json if type == "*/*"

    serializer = SERIALIZERS[type]
    return serializer.call if serializer
  end

  Json # fallback
end

.for_content_type(content_type) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/whoosh/serialization/negotiator.rb', line 28

def self.for_content_type(content_type)
  return Json if content_type.nil? || content_type.empty?

  type = content_type.split(";").first.strip
  serializer = SERIALIZERS[type]
  serializer ? serializer.call : Json
end