Sha256: aa7bf285c4ef24724ccb82fddf14a3e209098f5792337d2ad7bb2b6d35a10597

Contents?: true

Size: 1.24 KB

Versions: 52

Compression:

Stored size: 1.24 KB

Contents

class Rack::App::Serializer

  require "rack/app/serializer/formats_builder"

  def initialize(options={})

    @default_formatter = options[:default_formatter] || lambda { |o| o.to_s }
    @default_content_type = options[:default_content_type]

    formatters = options[:formatters] || {}
    content_types = options[:content_types] || {}

    @content_types = Hash.new(@default_content_type)
    @content_types.merge!(content_types)

    @formatters = Hash.new(@default_formatter)
    @formatters.merge!(formatters)

  end

  def serialize(extname, object)
    String(@formatters[extname].call(object))
  end

  def response_headers_for(extname)
    headers = {}
    add_content_type_for(headers, extname)
    headers
  end

  def to_options
    {
      :formatters => @formatters,
      :content_types => @content_types,
      :default_formatter => @default_formatter,
      :default_content_type => @default_content_type,
    }
  end

  def extnames
    (@formatters.keys + @content_types.keys).uniq
  end

  protected

  CONTENT_TYPE = ::Rack::App::Constants::HTTP::Headers::CONTENT_TYPE

  def add_content_type_for(headers, extname)
    content_type = @content_types[extname]
    if content_type
      headers[CONTENT_TYPE]= content_type.dup
    end
  end


end

Version data entries

52 entries across 52 versions & 2 rubygems

Version Path
rack-app-11.0.2 lib/rack/app/serializer.rb
rack-app-11.0.1 lib/rack/app/serializer.rb
rack-app-11.0.0 lib/rack/app/serializer.rb
rack-app-10.0.0 lib/rack/app/serializer.rb
rack-app-9.0.0 lib/rack/app/serializer.rb
rack-app-8.1.0 lib/rack/app/serializer.rb
rack-app-8.0.1 lib/rack/app/serializer.rb
rack-app-8.0.0 lib/rack/app/serializer.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/rack-app-7.7.0/lib/rack/app/serializer.rb
rack-app-7.7.0 lib/rack/app/serializer.rb
rack-app-7.6.5 lib/rack/app/serializer.rb
rack-app-7.6.4 lib/rack/app/serializer.rb
rack-app-7.6.3 lib/rack/app/serializer.rb
rack-app-7.6.2 lib/rack/app/serializer.rb
rack-app-7.6.1 lib/rack/app/serializer.rb
rack-app-7.6.0 lib/rack/app/serializer.rb
rack-app-7.5.2 lib/rack/app/serializer.rb
rack-app-7.5.1 lib/rack/app/serializer.rb
rack-app-7.5.0 lib/rack/app/serializer.rb
rack-app-7.4.0 lib/rack/app/serializer.rb