Sha256: f3d335284265c5fcf6f7f3460462c6508110c7b0055cb23c3f5a4bfafe718f36

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

module Peictt
  module Builder
    class HttpHeader
      attr_reader :args
      attr_reader :headers
      ERROR_MSG = "First for render argument must be a view"\
      "name as a Symbol or"\
      "string; Second argument for render must be type Hash".freeze

      MODIFIERS = [:text, :json, :headers, :status].freeze

      def initialize(arg)
        @args = arg
        @headers = { "Content-Type" => "text/html" }
        process_args
      end

      def status
        @status || 200
      end

      private

      def json
        @headers["Content-Type"] = "application/json"
      end

      def text
        @headers["Content-Type"] = "text/plain"
      end

      def add_headers(headers)
        @headers.merge! headers
      end

      def process_options(options)
        @status = options[:status]
        options.keys.each do |key|
          if MODIFIERS.include?(key) && (key != :headers)
            send(key)
          elsif MODIFIERS.include?(key) && (key == :headers)
            add_headers options[:headers]
          end
        end
      end

      def process_args
        if (args.size > 1) && (args[1].is_a? Hash)
          process_options args[1]
        elsif (args.size == 1) && (args[0].is_a? Hash)
          process_options args[0]
        elsif (args.size == 1) &&
              ((args[0].is_a? String) || (args[0].is_a? Symbol))
          status
        elsif (args.size > 1) && (!args[1].is_a? Hash)
          raise ArgumentError.new ERROR_MSG
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
peictt-0.1.1 lib/peictt/builder/http_header.rb