Sha256: 6abdbc916fa57b4dbe92902178615331fbca32b447b84a6e50248d8ea717e91a

Contents?: true

Size: 928 Bytes

Versions: 15

Compression:

Stored size: 928 Bytes

Contents

require "rubygems"
require "json"
module Butterfly
  class Response
    attr_reader :request, :body, :status, :headers
    
    def initialize req, b=nil
      @request = req
      @headers = {}
      @body = b
    end
    def return!(b)
      @body ||= b
      [status, prepare_headers, prepare_body]
    end
    def prepare_headers
      add_header("ContentType", choose_response_type)
      headers
    end
    def prepare_body
      case request.request_type
      when "json"
        body.to_json
      else
        body.to_s
      end
    end
    def add_header(k,v)
      @headers[k] = v unless @headers.include?(k)
    end
    def status
      @status ||= 200
    end
    def success!
      @status = 200
    end
    def fail!
      @status = 404
    end
    def choose_response_type
      case request.request_type
      when "json"
        "application/json"
      else
        "text/plain"
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 4 rubygems

Version Path
auser-butterfly-0.0.2 lib/butterfly/response.rb
auser-butterfly-0.0.3 lib/butterfly/response.rb
auser-butterfly-0.0.4 lib/butterfly/response.rb
auser-butterfly-0.0.5 lib/butterfly/response.rb
auser-poolparty-1.1.1 vendor/gems/butterfly/lib/response.rb
auser-poolparty-1.1.3 vendor/gems/butterfly/lib/response.rb
auser-poolparty-1.1.4 vendor/gems/butterfly/lib/response.rb
auser-poolparty-1.1.5 vendor/gems/butterfly/lib/response.rb
auser-poolparty-1.1.7 vendor/gems/butterfly/lib/response.rb
auser-poolparty-1.2.0 vendor/gems/butterfly/lib/response.rb
auser-poolparty-1.2.1 vendor/gems/butterfly/lib/response.rb
auser-poolparty-1.2.2 vendor/gems/butterfly/lib/response.rb
fairchild-poolparty-1.1.3 vendor/gems/butterfly/lib/response.rb
fairchild-poolparty-1.1.4 vendor/gems/butterfly/lib/response.rb
poolparty-1.2.2 vendor/gems/butterfly/lib/response.rb