class Phaedra::Base

Public Class Methods

get_instance(server, *options) click to toggle source

Used by WEBrick

# File lib/phaedra/base.rb, line 15
def self.get_instance(server, *options)
  self.new(server, *options)
end
new(context = nil) click to toggle source

Context might be a WEBrick server, nil if coming from Rack

# File lib/phaedra/base.rb, line 20
def initialize(context = nil)
  @context = context
end

Public Instance Methods

delete(params) click to toggle source
# File lib/phaedra/base.rb, line 42
def delete(params)
  raise WEBrick::HTTPStatus::NotFound, "`#{request.path}' not found."
end
do_DELETE(req, res)
Alias for: do_GET
do_GET(req, res) click to toggle source
# File lib/phaedra/base.rb, line 60
def do_GET(req, res)
  @req = req
  @res = res

  set_initial_status

  result = run_callbacks :action do
    # WEBrick's query string handler with DELETE is funky
    params = if @req.request_method == "DELETE"
               WEBrick::HTTPUtils::parse_query(@req.query_string)
             else
               @req.query
             end

    @res.body = call_method_action(params)
  end

  return error_condition unless result
  
  complete_response
end
Also aliased as: do_DELETE
do_PATCH(req, res)
Alias for: do_POST
do_POST(req, res) click to toggle source
# File lib/phaedra/base.rb, line 82
def do_POST(req, res)
  @req = req
  @res = res

  set_initial_status

  result = run_callbacks :action do
    params = if (@req.header["content-type"] || @req.header["content_type"]).to_s.include?("multipart/form-data")
      if @req.respond_to?(:params) # Rack
        @req.params
      else
        @req.query # WEBrick
      end
    else
      begin
        JSON.parse(@req.body)
      rescue JSON::ParserError, TypeError
        @req.body
      end
    end

    @res.body = call_method_action(params)
  end

  return error_condition unless result

  complete_response
end
Also aliased as: do_PUT, do_PATCH
do_PUT(req, res)
Alias for: do_POST
get(params) click to toggle source

Override in subclass

# File lib/phaedra/base.rb, line 26
def get(params)
  raise WEBrick::HTTPStatus::NotFound, "`#{request.path}' not found."
end
patch(params) click to toggle source
# File lib/phaedra/base.rb, line 38
def patch(params)
  put(params)
end
post(params) click to toggle source
# File lib/phaedra/base.rb, line 30
def post(params)
  raise WEBrick::HTTPStatus::NotFound, "`#{request.path}' not found."
end
put(params) click to toggle source
# File lib/phaedra/base.rb, line 34
def put(params)
  raise WEBrick::HTTPStatus::NotFound, "`#{request.path}' not found."
end
request() click to toggle source
# File lib/phaedra/base.rb, line 47
def request; @req; end
response() click to toggle source
# File lib/phaedra/base.rb, line 48
def response; @res; end
service(req, res) click to toggle source
# File lib/phaedra/base.rb, line 50
def service(req, res)
  method_name = "do_" + req.request_method.gsub(/-/, "_")
  if respond_to?(method_name)
    __send__(method_name, req, res)
  else
    raise WEBrick::HTTPStatus::MethodNotAllowed,
          "unsupported method `#{req.request_method}'."
  end
end

Protected Instance Methods

call_method_action(params) click to toggle source
# File lib/phaedra/base.rb, line 122
def call_method_action(params)
  params = params.is_a?(Hash) ? params.with_indifferent_access : params
  send(@req.request_method.downcase, params)
end
complete_response() click to toggle source
# File lib/phaedra/base.rb, line 127
def complete_response
  if @res.body.is_a?(String) && !@res["Content-Type"].start_with?("text/")
    @res["Content-Type"] = "text/plain; charset=utf-8"
  elsif @res["Content-Type"].start_with? "application/json"
    @res.body = @res.body.to_json
  end
end
error_condition() click to toggle source
# File lib/phaedra/base.rb, line 135
def error_condition
  @res.status = 500
  @res["Content-Type"] = "text/plain"
  @res.body = "Internal Server Error (callback chain halted)"
end
set_initial_status() click to toggle source
# File lib/phaedra/base.rb, line 117
def set_initial_status
  @res.status = 200
  @res["Content-Type"] = "application/json; charset=utf-8"
end