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.
-
#regex ⇒ Object
Returns the value of attribute regex.
Class Method Summary collapse
Instance Method Summary collapse
- #encode_format(content_hash, request, response, type) ⇒ Object
- #getpath(request) ⇒ 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.
39 40 41 42 43 |
# File 'lib/waitress/restful/restbuilder.rb', line 39 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 |
#regex ⇒ Object
Returns the value of attribute regex.
9 10 11 |
# File 'lib/waitress/restful/restbuilder.rb', line 9 def regex @regex end |
Class Method Details
.build!(schema, &call) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/waitress/restful/restbuilder.rb', line 13 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 if optional buildRe << "/)?" else buildRe << ")/" end buildRe end Waitress::REST.new(Regexp.new("^#{a}$"), &call) end |
Instance Method Details
#encode_format(content_hash, request, response, type) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/waitress/restful/restbuilder.rb', line 55 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 |
#getpath(request) ⇒ Object
45 46 47 48 49 |
# File 'lib/waitress/restful/restbuilder.rb', line 45 def getpath request path = request.path path += "/" unless path.end_with? "/" path end |
#respond?(request, vhost) ⇒ Boolean
51 52 53 |
# File 'lib/waitress/restful/restbuilder.rb', line 51 def respond? request, vhost (getpath(request) =~ @regex) != nil end |
#serve(request, response, client, vhost) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/waitress/restful/restbuilder.rb', line 71 def serve request, response, client, vhost match = getpath(request).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 |