Sha256: 0cd5313bbdd5debbe997f75fc7dee2ffe8844790daf76cf414633f9f84aefa06

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

module Rasti
  module Web
    class Headers

      CONTENT_TYPES = {
        text: 'text/plain',
        html: 'text/html',
        json: 'application/json',
        js:   'application/javascript',
        css:  'text/css'
      }

      class << self

        CONTENT_TYPES.each do |key, value|
          define_method "for_#{key}" do |charset:'utf-8'|
            content_type value, charset: charset
          end
        end

        def for_file(filename, attachment:true, charset:'utf-8')
          merge content_type(MIME::Types.of(filename).first.content_type, charset: charset),
                content_disposition(filename, attachment: attachment)
        end

        private

        def content_type(type, charset:'utf-8')
          {'Content-Type' => "#{type}; charset=#{charset}"}
        end

        def content_disposition(filename, attachment:true)
          args = []
          args << 'attachment' if attachment
          args << "filename=\"#{File.basename(filename)}\""
          {'Content-Disposition' => args.join("; ")}
        end

        def merge(*poperties)
          poperties.inject({}) do |result, prop|
            result.merge prop
          end
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rasti-web-2.1.1 lib/rasti/web/headers.rb
rasti-web-2.1.0 lib/rasti/web/headers.rb