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



20
21
22
# File 'lib/phaedra/base.rb', line 20

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

Class Method Details

.get_instance(server, *options) ⇒ Object

Used by WEBrick



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

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

Instance Method Details

#delete(params) ⇒ Object

Raises:

  • (WEBrick::HTTPStatus::NotFound)


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

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

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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

#get(params) ⇒ Object

Override in subclass

Raises:

  • (WEBrick::HTTPStatus::NotFound)


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

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

#patch(params) ⇒ Object



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

def patch(params)
  put(params)
end

#post(params) ⇒ Object

Raises:

  • (WEBrick::HTTPStatus::NotFound)


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

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

#put(params) ⇒ Object

Raises:

  • (WEBrick::HTTPStatus::NotFound)


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

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

#requestObject



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

def request; @req; end

#responseObject



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

def response; @res; end

#service(req, res) ⇒ Object



50
51
52
53
54
55
56
57
58
# 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