Class: Waitress::REST
- Defined in:
- lib/waitress/restful/restbuilder.rb
Constant Summary collapse
- SUPPORTED_FORMATS =
[:json, :scon, :yaml, :yml]
Instance Attribute Summary collapse
-
#priority ⇒ Object
Returns the value of attribute priority.
Class Method Summary collapse
Instance Method Summary collapse
- #encode_format(content_hash, request, response, type) ⇒ Object
-
#initialize(regex, &call) ⇒ REST
constructor
A new instance of REST.
- #respond?(request, vhost) ⇒ Boolean
- #serve(request, response, client, vhost) ⇒ Object
Methods inherited from Handler
Constructor Details
#initialize(regex, &call) ⇒ REST
Returns a new instance of REST.
37 38 39 40 41 |
# File 'lib/waitress/restful/restbuilder.rb', line 37 def initialize regex, &call @regex = regex @call = call @priority = 200 end |
Instance Attribute Details
#priority ⇒ Object
Returns the value of attribute priority.
8 9 10 |
# File 'lib/waitress/restful/restbuilder.rb', line 8 def priority @priority end |
Class Method Details
.build!(schema, &call) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/waitress/restful/restbuilder.rb', line 12 def self.build! schema, &call pattern = /:([^\/:\?\[]+)(\?)?(\[[a-zA-Z0-9\-_\s,]+\])?\// a = schema.gsub(pattern) do |match| capture_name = $1 optional = ($2 == "?") enumerations = nil enumerations = $3.gsub(/\[|\]/, "").split(/,\s?/) unless $3.nil? buildRe = "(?<#{capture_name}>" if enumerations.nil? buildRe << "[^\\/]+" else buildRe << enumerations.join("|") end buildRe << ")" buildRe << "?" if optional buildRe << "/" buildRe << "?" if optional buildRe end Waitress::REST.new(Regexp.new(a), &call) end |
Instance Method Details
#encode_format(content_hash, request, response, type) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/waitress/restful/restbuilder.rb', line 47 def encode_format content_hash, request, response, type ret = "" if type == :json if request.get_query.include? "pretty" ret = JSON.pretty_generate(content_hash) else ret = JSON.generate(content_hash) end elsif type == :scon ret = SCON.generate!(content_hash) elsif type == :yaml || type == :yml ret = content_hash.to_yaml end ret end |
#respond?(request, vhost) ⇒ Boolean
43 44 45 |
# File 'lib/waitress/restful/restbuilder.rb', line 43 def respond? request, vhost (request.uri =~ @regex) != nil end |
#serve(request, response, client, vhost) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/waitress/restful/restbuilder.rb', line 63 def serve request, response, client, vhost match = request.uri.match @regex form = :json if (request.get_query.include? "format") val = request.get_query["format"].downcase.to_sym form = val if SUPPORTED_FORMATS.include?(val) end response.mime ".#{form.to_s}" content_hash = @call.call(match, request, response, vhost) response.body encode_format(content_hash, request, response, form) end |