Class: Phaedra::Base
- Inherits:
-
Object
- Object
- Phaedra::Base
- Includes:
- CallbacksActionable
- Defined in:
- lib/phaedra/base.rb
Class Method Summary collapse
-
.get_instance(server, *options) ⇒ Object
Used by WEBrick.
Instance Method Summary collapse
- #delete(params) ⇒ Object
- #do_GET(req, res) ⇒ Object (also: #do_DELETE)
- #do_POST(req, res) ⇒ Object (also: #do_PUT, #do_PATCH)
-
#get(params) ⇒ Object
Override in subclass.
-
#initialize(context = nil) ⇒ Base
constructor
Context might be a WEBrick server, nil if coming from Rack.
- #patch(params) ⇒ Object
- #post(params) ⇒ Object
- #put(params) ⇒ Object
- #request ⇒ Object
- #response ⇒ Object
- #service(req, res) ⇒ Object
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, *) self.new(server, *) end |
Instance Method Details
#delete(params) ⇒ Object
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
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
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
34 35 36 |
# File 'lib/phaedra/base.rb', line 34 def put(params) raise WEBrick::HTTPStatus::NotFound, "`#{request.path}' not found." end |
#request ⇒ Object
47 |
# File 'lib/phaedra/base.rb', line 47 def request; @req; end |
#response ⇒ Object
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 |