Sha256: 41d6f24e9b36baae150cfae49f9a7cea2ea9412fbffdeb58ecc214dee895b556

Contents?: true

Size: 1.54 KB

Versions: 12

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require 'json'

module Impression
  
  module RequestExtensions

    # Response extensions for `Qeweney::Request`
    module Responses

      TEXT_HEADERS = { 'Content-Type' => Qeweney::MimeTypes[:txt] }.freeze
      HTML_HEADERS = { 'Content-Type' => Qeweney::MimeTypes[:html] }.freeze
      JSON_HEADERS = { 'Content-Type' => Qeweney::MimeTypes[:json] }.freeze

      # Send an HTTP response with plain text content. The content type is set
      # to `text/plain`.
      #
      # @param text [String] response body
      # @param **headers [Hash] additional response headers
      def respond_text(text, **headers)
        headers = headers.empty? ? TEXT_HEADERS : headers.merge(TEXT_HEADERS)
        respond(text, headers)
      end

      # Send an HTTP response with HTML content. The content type is set to
      # `text/html`.
      #
      # @param html [String] response body
      # @param **headers [Hash] additional response headers
      def respond_html(html, **headers)
        headers = headers.empty? ? HTML_HEADERS : headers.merge(HTML_HEADERS)
        respond(html, headers)
      end

      # Send an JSON response. The given object is converted to JSON. The
      # content type is set to `application/json`.
      #
      # @param object [any] object to convert to JSON
      # @param **headers [Hash] additional response headers
      def respond_json(object, **headers)
        headers = headers.empty? ? JSON_HEADERS : headers.merge(JSON_HEADERS)
        respond(object.to_json, headers)
      end
    end

  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
impression-0.13 lib/impression/request_extensions/responses.rb
impression-0.12 lib/impression/request_extensions/responses.rb
impression-0.11 lib/impression/request_extensions/responses.rb
impression-0.10 lib/impression/request_extensions/responses.rb
impression-0.9 lib/impression/request_extensions/responses.rb
impression-0.8 lib/impression/request_extensions/responses.rb
impression-0.7.1 lib/impression/request_extensions/responses.rb
impression-0.7 lib/impression/request_extensions/responses.rb
impression-0.6 lib/impression/request_extensions/responses.rb
impression-0.5 lib/impression/request_extensions/responses.rb
impression-0.4 lib/impression/request_extensions/responses.rb
impression-0.3 lib/impression/request_extensions/responses.rb