sinatra-swagger 0.2.0 → 0.2.1

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
- MjY0OWY2MzkyM2NiOWJhNWUxZjZiNjkwODhhMTgxNDkzMjBiNzI1NA==
4
+ ZDNkNzU3NDI5MDI2YWFkZjQwMTU3NTQ4OTJiODgxZGJiZWRkMTQ5Mg==
5
5
  data.tar.gz: !binary |-
6
- NTcwYmUxZGNhZTg5OTkwMzc1Y2YwOWYxYzBmNDQ2ZTQ4OGE2MjY3Ng==
6
+ YjgzOWE5YjkwNjcwMmQyMDk2ZTlhMDVkNTRiM2Y4OGFmYWE1MmI5OA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OTFkMjcyNGFjYjJhZDlkMWIyMTA4OGRmNjNjMzI2NWJmMGNiY2I0NTkxYTlk
10
- MGY5NTI3ZDRiMTgwZDM0ZmUwMmVkMDdiNzZmMDAwZWQ3NzU1YjVhZTFhMGU1
11
- ZjAzZmU2MzRiNTZkOTI2ZTE1OTY1ZTc3NTk5MWQ4YzU0ZTFjMDQ=
9
+ OTRiNWI5NjgxMmY1MGE0ZmFlMTFlYWY2YTE4YmFkY2RjOTU3ZDBkMDkwMGNl
10
+ NjBmODJlNTM2M2VmMThkNjE3YjYwY2M3OTIwZDllMDc0YWViZGFlMjk2ODA1
11
+ ODE1MjFjMzRiYjQwNTFiYzhiYWMyMzYxNTYwYmFmN2U2OGU2MWQ=
12
12
  data.tar.gz: !binary |-
13
- ZWFkNGI4Y2EyYWEzOTU0ZjJkNjExMDEyNWNhNWNmMmE3ZGMyYWMxNDllOWE1
14
- YjdjOGI5NjJjODVlZWJjZDQ2NmYzNjdlM2E3M2M2ZWMyM2ZhZDlkYjQ5ODQ0
15
- ZWIxYjZhZTQ2NjQ2M2EzNzM3MmVlZDNiM2Q4ZmMzZTM5YzI0NWY=
13
+ YWQ4MWU4MjUxZGY5OWJlMDE0MmM3Y2E4MGVlYzE0MDczOGVlNjQ0YmMyNjEx
14
+ ODAxYWFhOTRiYTEzODYxNjhlMTMxNzBiNjgxNWYzYmZjZGQ0Y2IzZGU2MDhk
15
+ MWI5MGY1NDlhYjk1YjVmNDY2OGUyNTExMDE2YjJhMWE4ZmZhNTU=
data/README.md CHANGED
@@ -1,3 +1,105 @@
1
1
  # Sinatra::Swagger
2
2
 
3
- Helper methods for accesing your swagger documentation from within your sinatra routes.
3
+ Extensions & helper methods for accesing your swagger documentation from within your sinatra routes.
4
+
5
+ This library is in the *very* early stages of development, there will be 'obvious' features that are missing and wild inefficiencies, but please feel free to file issues/enhancements if there's anything you think would be useful.
6
+
7
+ ## Param Validator
8
+
9
+ This extension will use the `parameters` component of the route being accessed from your Swagger document to type cast your incoming parameters and raise `400` errors if any are incorrect:
10
+
11
+ ### An example
12
+
13
+ Your swagger 2.0 documentation, in the root of your project at `swagger/search.yaml`, might have some of these components:
14
+
15
+ ```yaml
16
+ swagger: 2.0
17
+ paths:
18
+ /search/{type}:
19
+ get:
20
+ parameters:
21
+ - name: type
22
+ in: path
23
+ description: Type of object to search for
24
+ required: true
25
+ - name: q
26
+ in: query
27
+ type: string
28
+ required: true
29
+ - name: limit
30
+ in: query
31
+ type: integer
32
+ minimum: 1
33
+ required: false
34
+ default: 20
35
+ ```
36
+
37
+ You would load that in your sinatra application like this:
38
+
39
+ ```ruby
40
+ require "sinatra/base"
41
+ require "sinatra/swagger"
42
+
43
+ class Search < Sinatra::Base
44
+ register Sinatra::Swagger::ParamValidator
45
+ swagger "swagger/search.yaml"
46
+
47
+ get "/search/*" do
48
+ # Sinatra::Swagger has pulled out the 'type' from the path
49
+ search_type = params['type']
50
+ # Sinatra::Swagger has ensured limit is integer-like, is one or bigger, and will be set to 20 if not given by the user
51
+ limit = params['limit']
52
+
53
+ swagger_spec # => is the hash from the spec at `paths./search/{type}.get`
54
+ end
55
+ end
56
+ ```
57
+
58
+ For now, if you were to request `/search/books` (ie. no required 'q' param) or `/search/books?q=penumbra&limit=p` then a `before` hook would catch the error and return a `400` with a JSON description of the issue.
59
+
60
+ You can override what happens when an invalidity is encountered by overriding the helper method `invalid_params` which takes a hash of invalidities in the form `param_name => issue_description_as_symbol`.
61
+
62
+ ## Spec Enforcer
63
+
64
+ While you're developing it can be really helpful to know that the output of your endpoints matches your spec. The spec enforcer will raise an exception if the data you're sending to the user doesn't conform to your swagger declared schema (if there is one).
65
+
66
+ Your Swagger 2.0 doc at `swagger/shows.yaml` might have some bits that look like this:
67
+
68
+ ```yaml
69
+ swagger: 2.0
70
+ paths:
71
+ /shows/{showId}:
72
+ responses:
73
+ 200:
74
+ schema:
75
+ title: Details describing a post
76
+ type: object
77
+ required:
78
+ - name
79
+ additionalProperties: false
80
+ properties:
81
+ name:
82
+ type: string
83
+ ```
84
+
85
+ You could reference it like this:
86
+
87
+ ```ruby
88
+ require "sinatra/base"
89
+ require "sinatra/swagger"
90
+
91
+ class Search < Sinatra::Base
92
+ register Sinatra::Swagger::SpecEnforcer unless production?
93
+ swagger "swagger/shows.yaml"
94
+
95
+ get "/shows/*" do
96
+ {
97
+ title: "This isn't a schema-valid response"
98
+ }.to_json
99
+ end
100
+ end
101
+ ```
102
+
103
+ The `after` hook will check the response body and will run the schema validation and raise a `JSON::Schema::ValidationError` exception as the output doesn't match the schema.
104
+
105
+ In order to not be restrictive, if the output doesn't look like JSON or if there's no schema defined, then no action will be taken and the body will be sent to the client.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -6,6 +6,20 @@ module Sinatra
6
6
  def self.registered(app)
7
7
  app.register Swagger::SwaggerLinked
8
8
 
9
+ app.helpers do
10
+ def invalid_params(invalidities)
11
+ content_type :json
12
+ halt(
13
+ 400,
14
+ {
15
+ error: 'invalid_params',
16
+ developerMessage: 'Some of the given parameters were invalid according to the Swagger spec.',
17
+ invalidities: invalidities
18
+ }.to_json
19
+ )
20
+ end
21
+ end
22
+
9
23
  app.before do
10
24
  next if swagger_spec.nil?
11
25
  _, captures, spec = swagger_spec.values
@@ -45,7 +59,7 @@ module Sinatra
45
59
  nil
46
60
  }.compact]
47
61
 
48
- halt 400, invalidities.to_json if invalidities.any?
62
+ invalid_params(invalidities) if invalidities.any?
49
63
  end
50
64
  end
51
65
  end
@@ -1,4 +1,4 @@
1
- require "sinatra/swagger/param_validator"
1
+ require "sinatra/swagger/swagger_linked"
2
2
 
3
3
  module Sinatra
4
4
  module Swagger
@@ -1,2 +1,3 @@
1
1
  require "sinatra/swagger/version"
2
+ require "sinatra/swagger/param_validator"
2
3
  require "sinatra/swagger/spec_enforcer"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-swagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - JP Hastings-Spital
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-13 00:00:00.000000000 Z
11
+ date: 2015-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema