Class: Phaedra::Base

Inherits:
Object
  • Object
show all
Includes:
CallbacksActionable
Defined in:
lib/phaedra/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = nil) ⇒ Base

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



16
17
18
# File 'lib/phaedra/base.rb', line 16

def initialize(context = nil)
  @context = context
end

Class Method Details

.get_instance(server, *options) ⇒ Object

Used by WEBrick



11
12
13
# File 'lib/phaedra/base.rb', line 11

def self.get_instance(server, *options)
  self.new(server, *options)
end

Instance Method Details

#delete(params) ⇒ Object

Raises:

  • (WEBrick::HTTPStatus::NotFound)


38
39
40
# File 'lib/phaedra/base.rb', line 38

def delete(params)
  raise WEBrick::HTTPStatus::NotFound, "`#{request.path}' not found."
end

#do_GET(req, res) ⇒ Object Also known as: do_DELETE



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/phaedra/base.rb', line 56

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

#do_POST(req, res) ⇒ Object Also known as: do_PUT, do_PATCH



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/phaedra/base.rb', line 78

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

#get(params) ⇒ Object

Override in subclass

Raises:

  • (WEBrick::HTTPStatus::NotFound)


22
23
24
# File 'lib/phaedra/base.rb', line 22

def get(params)
  raise WEBrick::HTTPStatus::NotFound, "`#{request.path}' not found."
end

#patch(params) ⇒ Object



34
35
36
# File 'lib/phaedra/base.rb', line 34

def patch(params)
  put(params)
end

#post(params) ⇒ Object

Raises:

  • (WEBrick::HTTPStatus::NotFound)


26
27
28
# File 'lib/phaedra/base.rb', line 26

def post(params)
  raise WEBrick::HTTPStatus::NotFound, "`#{request.path}' not found."
end

#put(params) ⇒ Object

Raises:

  • (WEBrick::HTTPStatus::NotFound)


30
31
32
# File 'lib/phaedra/base.rb', line 30

def put(params)
  raise WEBrick::HTTPStatus::NotFound, "`#{request.path}' not found."
end

#requestObject



43
# File 'lib/phaedra/base.rb', line 43

def request; @req; end

#responseObject



44
# File 'lib/phaedra/base.rb', line 44

def response; @res; end

#service(req, res) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/phaedra/base.rb', line 46

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