waitress-core 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d788390d6fd47ac3ea0120241146d3b940217b97
4
- data.tar.gz: 162ee7fc128c28cf58dbaf959f8b2a7ea084b3c4
3
+ metadata.gz: a5fa1729728798da3d7a4ec0949db1a3fe2deae3
4
+ data.tar.gz: 667594fb143c6d05c2ce4b158f3b35eb388aadf2
5
5
  SHA512:
6
- metadata.gz: 2af638e5e7084801949f532ea4702c1b58a94660c6ca39fe1030dd400a5430c056ddeb67d43cb190e5c7d9121cc42a722890a304b910e39f3717d2d206f830c3
7
- data.tar.gz: 236b011d0e408bc37eef94173fc921b0d687ec053c02b9d835145aedecab5628c28b112dad7888e8318d63a6ae94e41c0432dc5eea8795e1493055a9f71afc40
6
+ metadata.gz: 83b1ecfba256a594a6e99b7610656257dfdb83445f889ab907e952c3757c7745e447da388e8be16335f62d483f1c88c013d2c306b23be2f1335b0b762654ed3a
7
+ data.tar.gz: 2fe5debcbbc9770416491d0a876d5190e7d6ab802858ed10654dac67b9f033d327d24fe32be8612e2954433a88704dc9cfee05e09e03acbb3c3259d136a9f52c
data/lib/waitress.rb CHANGED
@@ -16,6 +16,7 @@ require 'waitress/handlers/handler404'
16
16
  require 'waitress/handlers/libhandler'
17
17
  require 'waitress/chef'
18
18
  require 'waitress/evalbind'
19
+ require 'waitress/restful/restbuilder'
19
20
 
20
21
  require 'waitress_http11'
21
22
 
@@ -0,0 +1,79 @@
1
+ module Waitress
2
+ class REST < Handler
3
+
4
+ require 'json'
5
+ require 'scon'
6
+ require 'yaml'
7
+
8
+ attr_accessor :priority
9
+
10
+ SUPPORTED_FORMATS = [:json, :scon, :yaml, :yml]
11
+
12
+ def self.build! schema, &call
13
+ pattern = /:([^\/:\?\[]+)(\?)?(\[[a-zA-Z0-9\-_\s,]+\])?\//
14
+
15
+ a = schema.gsub(pattern) do |match|
16
+ capture_name = $1
17
+ optional = ($2 == "?")
18
+ enumerations = nil
19
+ enumerations = $3.gsub(/\[|\]/, "").split(/,\s?/) unless $3.nil?
20
+ buildRe = "(?<#{capture_name}>"
21
+ if enumerations.nil?
22
+ buildRe << "[^\\/]+"
23
+ else
24
+ buildRe << enumerations.join("|")
25
+ end
26
+ buildRe << ")"
27
+ buildRe << "?" if optional
28
+ buildRe << "/"
29
+ buildRe << "?" if optional
30
+
31
+ buildRe
32
+ end
33
+
34
+ Waitress::REST.new(Regexp.new(a), &call)
35
+ end
36
+
37
+ def initialize regex, &call
38
+ @regex = regex
39
+ @call = call
40
+ @priority = 200
41
+ end
42
+
43
+ def respond? request, vhost
44
+ (request.uri =~ @regex) != nil
45
+ end
46
+
47
+ def encode_format content_hash, request, response, type
48
+ ret = ""
49
+ if type == :json
50
+ if request.get_query.include? "pretty"
51
+ ret = JSON.pretty_generate(content_hash)
52
+ else
53
+ ret = JSON.generate(content_hash)
54
+ end
55
+ elsif type == :scon
56
+ ret = SCON.generate!(content_hash)
57
+ elsif type == :yaml || type == :yml
58
+ ret = content_hash.to_yaml
59
+ end
60
+ ret
61
+ end
62
+
63
+ def serve request, response, client, vhost
64
+ match = request.uri.match @regex
65
+
66
+ form = :json
67
+ if (request.get_query.include? "format")
68
+ val = request.get_query["format"].downcase.to_sym
69
+ form = val if SUPPORTED_FORMATS.include?(val)
70
+ end
71
+
72
+ response.mime ".#{form.to_s}"
73
+
74
+ content_hash = @call.call(match, request, response, vhost)
75
+ response.body encode_format(content_hash, request, response, form)
76
+ end
77
+
78
+ end
79
+ end
@@ -1,3 +1,3 @@
1
1
  module Waitress
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: waitress-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaci Brunning
@@ -226,6 +226,7 @@ files:
226
226
  - lib/waitress/resources/http/img/404.png
227
227
  - lib/waitress/resources/http/index.html
228
228
  - lib/waitress/response.rb
229
+ - lib/waitress/restful/restbuilder.rb
229
230
  - lib/waitress/server.rb
230
231
  - lib/waitress/util/less_watcher.rb
231
232
  - lib/waitress/util/util.rb