Sha256: bb072ad92d908d6055b1860871cc6a90ea545a95d326a9e1854bf4677e9e589a

Contents?: true

Size: 1.06 KB

Versions: 19

Compression:

Stored size: 1.06 KB

Contents

require 'forwardable'
require 'json'

module Jeanine
  class Response
    attr_accessor :action_variables
    attr_reader :headers, :body
    attr_accessor :status

    def initialize(body = [], status = 200, headers = { 'Content-Type' => 'text/html; charset=utf-8' })
      @body = body
      @status = status
      @headers = headers
      @length = 0

      return if body == []
      if body.respond_to? :to_str
        write body.to_str
      elsif body.respond_to? :each
        body.each { |i| write i.to_s }
      else
        raise TypeError, 'body must #respond_to? #to_str or #each'
      end
    end

    def complete!
      unless (100..199).include?(status) || status == 204
        headers['Content-Length'] = @length.to_s
      end
      [status, headers, body]
    end

    def content_type=(type)
      headers['Content-Type'] = type
    end

    def redirect_to(target, status = 302)
      self.status = status
      headers['Location'] = target
    end

    def write(string)
      s = string.to_s
      @length += s.bytesize

      body << s
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
jeanine-0.8.1 lib/jeanine/response.rb
jeanine-0.8 lib/jeanine/response.rb
jeanine-0.7.8 lib/jeanine/response.rb
jeanine-0.7.7 lib/jeanine/response.rb
jeanine-0.7.6 lib/jeanine/response.rb
jeanine-0.7.5 lib/jeanine/response.rb
jeanine-0.7.4 lib/jeanine/response.rb
jeanine-0.7.3.2 lib/jeanine/response.rb
jeanine-0.7.3.1 lib/jeanine/response.rb
jeanine-0.7.3 lib/jeanine/response.rb
jeanine-0.7.2 lib/jeanine/response.rb
jeanine-0.7.1 lib/jeanine/response.rb
jeanine-0.7.0 lib/jeanine/response.rb
jeanine-0.6.0 lib/jeanine/response.rb
jeanine-0.5.0 lib/jeanine/response.rb
jeanine-0.4.0 lib/jeanine/response.rb
jeanine-0.3.0 lib/jeanine/response.rb
jeanine-0.2.0 lib/jeanine/response.rb
jeanine-0.1.0 lib/jeanine/response.rb