sinatra-swagger 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OGRhZDE4ZTc4ZDFlMDAxYTY1MGI5OTU1MzNmMDRiYTQwM2UwNmZjZA==
4
+ MjY0OWY2MzkyM2NiOWJhNWUxZjZiNjkwODhhMTgxNDkzMjBiNzI1NA==
5
5
  data.tar.gz: !binary |-
6
- MTExNmY2MWU5ZTdiNDZiM2YzMmRjMGI0ZmMzY2EzNWYxYjdhMWQzNQ==
6
+ NTcwYmUxZGNhZTg5OTkwMzc1Y2YwOWYxYzBmNDQ2ZTQ4OGE2MjY3Ng==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NzBkMzEzOGFhNzVmN2I1YjUwNzU4OWE5Mjc0NWY0NjE2OTg0YTI0ZTllOTI3
10
- NWFlYTBjMzIzOGI2N2IzZjAyYWYwNjI4MDdkNWI5MDA1NDYxNjhkNTAxYmIx
11
- MDZjOTNmN2Y2ZDVjYTBhZDc3ZWJjMmZjYmFlYWZiMzAyM2QxNTU=
9
+ OTFkMjcyNGFjYjJhZDlkMWIyMTA4OGRmNjNjMzI2NWJmMGNiY2I0NTkxYTlk
10
+ MGY5NTI3ZDRiMTgwZDM0ZmUwMmVkMDdiNzZmMDAwZWQ3NzU1YjVhZTFhMGU1
11
+ ZjAzZmU2MzRiNTZkOTI2ZTE1OTY1ZTc3NTk5MWQ4YzU0ZTFjMDQ=
12
12
  data.tar.gz: !binary |-
13
- NjA2YTZjMzg3MmE2OWY2OGU2OTczYmIxNTI5NDdjZWJmZDE1MDFjNDJmYjI4
14
- ZmEyZGY3MGY1Y2VhZWMwZjk1ZGVkMTg4OWNhMzQ4NjM5YWQ4ZjgyMTFlMzY5
15
- ZjY0Njg2NTY2MDcwMjI0YjY5MDJhMWM2NjNmOTM1MTcwNDA2ZDY=
13
+ ZWFkNGI4Y2EyYWEzOTU0ZjJkNjExMDEyNWNhNWNmMmE3ZGMyYWMxNDllOWE1
14
+ YjdjOGI5NjJjODVlZWJjZDQ2NmYzNjdlM2E3M2M2ZWMyM2ZhZDlkYjQ5ODQ0
15
+ ZWIxYjZhZTQ2NjQ2M2EzNzM3MmVlZDNiM2Q4ZmMzZTM5YzI0NWY=
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -0,0 +1,55 @@
1
+ require "sinatra/swagger/swagger_linked"
2
+
3
+ module Sinatra
4
+ module Swagger
5
+ module ParamValidator
6
+ def self.registered(app)
7
+ app.register Swagger::SwaggerLinked
8
+
9
+ app.before do
10
+ next if swagger_spec.nil?
11
+ _, captures, spec = swagger_spec.values
12
+
13
+ invalidities = Hash[(spec['parameters'] || []).map { |details|
14
+ param_name = details['name']
15
+
16
+ parameter = case details['in']
17
+ when "query"
18
+ params[param_name]
19
+ when "path"
20
+ captures[param_name]
21
+ else
22
+ raise NotImplementedError, "Can't cope with #{details['in']} parameters right now"
23
+ end
24
+
25
+ if !parameter
26
+ next [param_name, :missing] if details['required'] && details['required'] != "false"
27
+ next unless details['default']
28
+ parameter = details['default']
29
+ end
30
+
31
+ begin
32
+ parameter = ::Swagger.cast(parameter.to_s, details['type']) if parameter
33
+ rescue ArgumentError
34
+ next [param_name, :incorrect_type]
35
+ end
36
+
37
+ if %w{integer number}.include? details['type']
38
+ too_large = details['maximum'] && parameter > details['maximum']
39
+ too_small = details['minimum'] && parameter < details['minimum']
40
+ next [param_name, :out_of_range] if too_large || too_small
41
+ end
42
+
43
+ params[param_name] = parameter
44
+
45
+ nil
46
+ }.compact]
47
+
48
+ halt 400, invalidities.to_json if invalidities.any?
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ register Swagger::ParamValidator
55
+ end
@@ -1,68 +1,25 @@
1
- require "yaml"
2
- require "swagger/rack"
1
+ require "sinatra/swagger/param_validator"
3
2
 
4
3
  module Sinatra
5
4
  module Swagger
6
5
  module SpecEnforcer
7
- def swagger(filepath)
8
- set :swagger, ::Swagger::Base.new(filepath)
9
- end
10
-
11
6
  def self.registered(app)
12
- app.helpers Helpers
13
-
14
- app.before do
15
- spec = swagger_spec
16
- unless spec.nil?
17
- _, captures, spec = spec.values
18
-
19
- invalidities = Hash[(spec['parameters'] || []).map { |details|
20
- param_name = details['name']
21
-
22
- parameter = case details['in']
23
- when "query"
24
- params[param_name]
25
- when "path"
26
- captures[param_name]
27
- else
28
- raise NotImplementedError, "Can't cope with #{details['in']} parameters right now"
29
- end
30
-
31
- if !parameter
32
- next [param_name, :missing] if details['required'] && details['required'] != "false"
33
- next unless details['default']
34
- parameter = details['default']
35
- end
36
-
37
- begin
38
- parameter = ::Swagger.cast(parameter.to_s, details['type']) if parameter
39
- rescue ArgumentError
40
- next [param_name, :incorrect_type]
41
- end
42
-
43
- if %w{integer number}.include? details['type']
44
- too_large = details['maximum'] && parameter > details['maximum']
45
- too_small = details['minimum'] && parameter < details['minimum']
46
- next [param_name, :out_of_range] if too_large || too_small
47
- end
48
-
49
- params[param_name] = parameter
50
-
51
- nil
52
- }.compact]
53
-
54
- halt 400, invalidities.to_json if invalidities.any?
7
+ app.register Swagger::SwaggerLinked
8
+
9
+ app.after do
10
+ next unless response.content_type =~ %r{^application/(?:.+\+)?json$}
11
+ next unless body = JSON.parse(response.body.first) rescue nil
12
+ next if swagger_spec.nil?
13
+ schema = schema_from_spec_at("responses/#{response.status}/schema")
14
+ next if schema.nil?
15
+ begin
16
+ JSON::Validator.validate!(schema, body)
17
+ rescue JSON::Schema::ValidationError => e
18
+ e.message = "Response JSON did not match the Swagger schema: #{e.message}"
19
+ raise e
55
20
  end
56
21
  end
57
22
  end
58
-
59
- module Helpers
60
- def swagger_spec
61
- settings.swagger.request_spec(env: env)
62
- end
63
- end
64
23
  end
65
24
  end
66
-
67
- register Swagger::SpecEnforcer
68
25
  end
@@ -0,0 +1,32 @@
1
+ require "swagger/rack"
2
+
3
+ module Sinatra
4
+ module Swagger
5
+ module SwaggerLinked
6
+ def swagger(filepath)
7
+ set :swagger, ::Swagger::Base.from_file(filepath)
8
+ end
9
+
10
+ def self.registered(app)
11
+ app.helpers Helpers
12
+ end
13
+
14
+ module Helpers
15
+ def swagger_spec
16
+ raise "No swagger file loaded" unless settings.swagger
17
+ settings.swagger.request_spec(env: env)
18
+ end
19
+
20
+ def schema_from_spec_at(path)
21
+ schema = swagger_spec[:spec]
22
+ path.split("/").each do |key|
23
+ schema = schema[YAML.load(key)]
24
+ return if schema.nil?
25
+ end
26
+ schema['definitions'] = settings.swagger['definitions'] if settings.swagger['definitions']
27
+ schema
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
data/lib/swagger/base.rb CHANGED
@@ -25,9 +25,17 @@ module Swagger
25
25
  JSON::Validator.add_schema(JSON::Schema.new(data, data['id']))
26
26
  end
27
27
 
28
- def initialize(filepath)
29
- @spec = YAML.load(open(filepath))
28
+ def initialize(data)
29
+ @spec = data
30
30
  JSON::Validator.validate!({ "$ref" => "http://swagger.io/v2/schema.json#" }, @spec)
31
31
  end
32
+
33
+ def self.from_file(filepath)
34
+ new(YAML.load(open(filepath)))
35
+ end
36
+
37
+ def [](key)
38
+ @spec[key]
39
+ end
32
40
  end
33
41
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-swagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - JP Hastings-Spital
@@ -81,7 +81,9 @@ files:
81
81
  - Rakefile
82
82
  - VERSION
83
83
  - lib/sinatra/swagger.rb
84
+ - lib/sinatra/swagger/param_validator.rb
84
85
  - lib/sinatra/swagger/spec_enforcer.rb
86
+ - lib/sinatra/swagger/swagger_linked.rb
85
87
  - lib/sinatra/swagger/version.rb
86
88
  - lib/swagger/base.rb
87
89
  - lib/swagger/rack.rb