committee 1.2.2 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19ffd2ac55e6c3099fa3847fdf10b34028ef3cca
4
- data.tar.gz: 88b45587d79f604c951e0a9a827454d7471a5e8b
3
+ metadata.gz: 80631a912a39fd3bfe5334456acb5c6dcaf8a30a
4
+ data.tar.gz: 61ded3be6affcb5f842a168a27823bc646f00776
5
5
  SHA512:
6
- metadata.gz: 5bd74c2990fd147c4d2d0556b0902123177b8072ee1911e0d410c2218a182187a4f2a8851c9a8826589a885327763a566f996884b6de8cf94facf800388462c2
7
- data.tar.gz: c2b15f8dd3c81660ccbb6836395ccae85eb36b337c4f123546ab2b4a7d1ae7f99671fee0b2f796819819f5d1838a6f203a0722a15ab34e37e3712471ec5d9acd
6
+ metadata.gz: 51cd02c1fedb4fa660e88e3f1d56580a3d3c425e8cb0736903ed6b50a7564ff0e46a4a5aa447af7077132893f1b174e6e4becff245edd80d73da6f961adf04bb
7
+ data.tar.gz: 735ce3684a203194307b638c35201fbd59aaef38813ffa7e076fc17e6ee3ddc7fc3c954cba93930d5ef7e263149fa90c776c70beeba821e81e077e9f1052a47e
@@ -11,7 +11,17 @@ module Committee::Middleware
11
11
  end
12
12
  @schema = JsonSchema.parse!(data)
13
13
  @schema.expand_references!
14
- @router = Committee::Router.new(@schema)
14
+ @router = Committee::Router.new(@schema, options)
15
+ end
16
+
17
+ def call(env)
18
+ request = Rack::Request.new(env)
19
+
20
+ if @router.includes_request?(request)
21
+ handle(request)
22
+ else
23
+ @app.call(request.env)
24
+ end
15
25
  end
16
26
 
17
27
  private
@@ -2,7 +2,6 @@ module Committee::Middleware
2
2
  class RequestValidation < Base
3
3
  def initialize(app, options={})
4
4
  super
5
- @prefix = options[:prefix]
6
5
  @raise = options[:raise]
7
6
  @strict = options[:strict]
8
7
 
@@ -10,19 +9,17 @@ module Committee::Middleware
10
9
  @allow_extra = options[:allow_extra]
11
10
  end
12
11
 
13
- def call(env)
14
- request = Rack::Request.new(env)
15
- env[@params_key] = Committee::RequestUnpacker.new(request).call
16
- if link = @router.routes_request?(request, prefix: @prefix)
12
+ def handle(request)
13
+ request.env[@params_key] = Committee::RequestUnpacker.new(request).call
14
+
15
+ if link = @router.find_request_link(request)
17
16
  validator = Committee::RequestValidator.new(link)
18
- validator.call(request, env[@params_key])
19
- @app.call(env)
17
+ validator.call(request, request.env[@params_key])
18
+ @app.call(request.env)
19
+ elsif @strict
20
+ raise Committee::NotFound
20
21
  else
21
- if @strict
22
- raise Committee::NotFound
23
- else
24
- @app.call(env)
25
- end
22
+ @app.call(request.env)
26
23
  end
27
24
  rescue Committee::BadRequest, Committee::InvalidRequest
28
25
  raise if @raise
@@ -2,14 +2,13 @@ module Committee::Middleware
2
2
  class ResponseValidation < Base
3
3
  def initialize(app, options={})
4
4
  super
5
- @prefix = options[:prefix]
6
5
  @raise = options[:raise]
7
6
  end
8
7
 
9
- def call(env)
10
- status, headers, response = @app.call(env)
11
- request = Rack::Request.new(env)
12
- if link = @router.routes_request?(request, prefix: @prefix)
8
+ def handle(request)
9
+ status, headers, response = @app.call(request.env)
10
+
11
+ if link = @router.find_request_link(request)
13
12
  full_body = ""
14
13
  response.each do |chunk|
15
14
  full_body << chunk
@@ -3,23 +3,21 @@ module Committee::Middleware
3
3
  def initialize(app, options={})
4
4
  super
5
5
  @cache = {}
6
- @call = options[:call]
7
- @prefix = options[:prefix]
6
+ @call = options[:call]
8
7
  end
9
8
 
10
- def call(env)
11
- request = Rack::Request.new(env)
12
- if link = @router.routes_request?(request, prefix: @prefix)
9
+ def handle(request)
10
+ if link = @router.find_request_link(request)
13
11
  headers = { "Content-Type" => "application/json" }
14
12
  data = cache(link.method, link.href) do
15
13
  Committee::ResponseGenerator.new.call(link)
16
14
  end
17
15
  if @call
18
- env["committee.response"] = data
19
- call_status, call_headers, call_body = @app.call(env)
16
+ request.env["committee.response"] = data
17
+ call_status, call_headers, call_body = @app.call(request.env)
20
18
 
21
19
  # a committee.suppress signal initiates a direct pass through
22
- if env["committee.suppress"] == true
20
+ if request.env["committee.suppress"] == true
23
21
  return call_status, call_headers, call_body
24
22
  end
25
23
 
@@ -30,7 +28,7 @@ module Committee::Middleware
30
28
  status = link.rel == "create" ? 201 : 200
31
29
  [status, headers, [MultiJson.encode(data, pretty: true)]]
32
30
  else
33
- @app.call(env)
31
+ @app.call(request.env)
34
32
  end
35
33
  end
36
34
 
@@ -1,11 +1,21 @@
1
1
  module Committee
2
2
  class Router
3
- def initialize(schema)
3
+ def initialize(schema, options = {})
4
4
  @routes = build_routes(schema)
5
+ @prefix = options[:prefix]
6
+ @prefix_regexp = /\A#{Regexp.escape(@prefix)}/.freeze if @prefix
5
7
  end
6
8
 
7
- def routes?(method, path, options = {})
8
- path = path.gsub(/^#{options[:prefix]}/, "") if options[:prefix]
9
+ def includes?(path)
10
+ !@prefix || path =~ @prefix_regexp
11
+ end
12
+
13
+ def includes_request?(request)
14
+ includes?(request.path)
15
+ end
16
+
17
+ def find_link(method, path)
18
+ path = path.gsub(@prefix_regexp, "") if @prefix
9
19
  if method_routes = @routes[method]
10
20
  method_routes.each do |pattern, link|
11
21
  if path =~ pattern
@@ -16,8 +26,8 @@ module Committee
16
26
  nil
17
27
  end
18
28
 
19
- def routes_request?(request, options = {})
20
- routes?(request.request_method, request.path_info, options)
29
+ def find_request_link(request)
30
+ find_link(request.request_method, request.path_info)
21
31
  end
22
32
 
23
33
  private
@@ -11,11 +11,9 @@ module Committee::Test
11
11
  schema.expand_references!
12
12
  schema
13
13
  end
14
- @router ||= Committee::Router.new(@schema)
14
+ @router ||= Committee::Router.new(@schema, prefix: schema_url_prefix)
15
15
 
16
- link =
17
- @router.routes_request?(last_request, prefix: schema_url_prefix)
18
- unless link
16
+ unless link = @router.find_request_link(last_request)
19
17
  response = "`#{last_request.request_method} #{last_request.path_info}` undefined in schema."
20
18
  raise Committee::InvalidResponse.new(response)
21
19
  end
@@ -46,6 +46,13 @@ describe Committee::Middleware::RequestValidation do
46
46
  assert_equal 200, last_response.status
47
47
  end
48
48
 
49
+ it "ignores paths outside the prefix" do
50
+ @app = new_rack_app(prefix: "/v1")
51
+ header "Content-Type", "text/html"
52
+ get "/hello"
53
+ assert_equal 200, last_response.status
54
+ end
55
+
49
56
  it "warns when sending a deprecated string" do
50
57
  mock(Committee).warn_deprecated.with_any_args
51
58
  @app = new_rack_app(schema: File.read("./test/data/schema.json"))
data/test/router_test.rb CHANGED
@@ -1,27 +1,38 @@
1
1
  require_relative "test_helper"
2
2
 
3
3
  describe Committee::Router do
4
- before do
5
- data = MultiJson.decode(File.read("./test/data/schema.json"))
6
- schema = JsonSchema.parse!(data)
7
- schema.expand_references!
8
- @router = Committee::Router.new(schema)
9
- end
10
-
11
4
  it "builds routes without parameters" do
12
- refute_nil @router.routes?("GET", "/apps")
5
+ refute_nil router.find_link("GET", "/apps")
13
6
  end
14
7
 
15
8
  it "builds routes with parameters" do
16
- refute_nil @router.routes?("GET", "/apps/123")
9
+ refute_nil router.find_link("GET", "/apps/123")
17
10
  end
18
11
 
19
12
  it "doesn't match anything on a /" do
20
- assert_nil @router.routes?("GET", "/")
13
+ assert_nil router.find_link("GET", "/")
21
14
  end
22
15
 
23
16
  it "takes a prefix" do
24
17
  # this is a sociopathic example
25
- refute_nil @router.routes?("GET", "/kpi/apps/123", prefix: "/kpi")
18
+ assert router(prefix: "/kpi").find_link("GET", "/kpi/apps/123")
19
+ end
20
+
21
+ it "includes all paths without a prefix" do
22
+ assert router.includes?("/")
23
+ assert router.includes?("/apps")
24
+ end
25
+
26
+ it "only includes the prefix path with a prefix" do
27
+ refute router(prefix: "/kpi").includes?("/")
28
+ assert router(prefix: "/kpi").includes?("/kpi")
29
+ assert router(prefix: "/kpi").includes?("/kpi/apps")
30
+ end
31
+
32
+ def router(options = {})
33
+ data = MultiJson.decode(File.read("./test/data/schema.json"))
34
+ schema = JsonSchema.parse!(data)
35
+ schema.expand_references!
36
+ Committee::Router.new(schema, options)
26
37
  end
27
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: committee
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandur