rack-json_schema 1.0.0 → 1.0.1
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 +4 -4
- data/CHANGELOG.md +12 -9
- data/README.md +34 -0
- data/bin/specup +1 -0
- data/lib/rack/json_schema/schema_provider.rb +45 -0
- data/lib/rack/json_schema/version.rb +1 -1
- data/lib/rack/json_schema.rb +1 -0
- data/spec/rack/spec/schema_provider_spec.rb +93 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef655a50e6b11b211d6c48f8b638c6f12ed80b2c
|
4
|
+
data.tar.gz: 614a49ef7e5fcecac371e7d3b2746e6d5c940a79
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f1374e7ac063d401e43cdf6f9430f1f5c551b01b793ee77f7c344f5c34195c79b4f6b820bbb915c7645b66deeb0d1450ad25c1f196698337dcc0f78ed37fe44
|
7
|
+
data.tar.gz: 80c3ed15950aa3d4db33a62576772f17cf23071bfb92bff6029356734bccdd3e3d789deaa61190a3ef9dd775edfd15bb45db8625a09d6c6e4f92d3fc8b325406
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
## 1.0.1
|
2
|
+
* Add Rack::JsonSchema::SchemaProvider
|
3
|
+
|
1
4
|
## 1.0.0
|
2
5
|
* Rename: rack-spec -> rack-json_schema
|
3
6
|
|
@@ -17,36 +20,36 @@
|
|
17
20
|
* Add Rack::JsonSchema::Mock
|
18
21
|
* Prettify response JSON
|
19
22
|
|
20
|
-
##
|
23
|
+
## 0.1.3
|
21
24
|
* Array response support of Rack::JsonSchema::ResponseValidation
|
22
25
|
|
23
|
-
##
|
26
|
+
## 0.1.2
|
24
27
|
* Change Content-Type validation policy
|
25
28
|
* Add Rack::JsonSchema::ResponseValidation
|
26
29
|
|
27
|
-
##
|
30
|
+
## 0.1.1
|
28
31
|
* Add ErrorHandler rack middleware for building error response
|
29
32
|
|
30
|
-
##
|
33
|
+
## 0.1.0
|
31
34
|
* Rebuilt entire code based on JSON schema
|
32
35
|
|
33
|
-
##
|
36
|
+
## 0.0.5
|
34
37
|
* Change RESTful resource API (#get, #post, #put, and #delete)
|
35
38
|
|
36
|
-
##
|
39
|
+
## 0.0.4
|
37
40
|
* Add Rack::JsonSchema::Restful, strongly conventional RESTful API Provider
|
38
41
|
|
39
|
-
##
|
42
|
+
## 0.0.3
|
40
43
|
* Add a new constraint: required
|
41
44
|
* More DRY way for validator definition
|
42
45
|
|
43
|
-
##
|
46
|
+
## 0.0.2
|
44
47
|
* Change key name: queryParameters -> parameters
|
45
48
|
* Add a new constraint: only
|
46
49
|
* Add a new constraint: minimumLength
|
47
50
|
* Add a new constraint: maxinumLength
|
48
51
|
|
49
|
-
##
|
52
|
+
## 0.0.1
|
50
53
|
* Add a new constraint: type
|
51
54
|
* Add a new constraint: minimum
|
52
55
|
* Add a new constraint: maxinum
|
data/README.md
CHANGED
@@ -7,6 +7,7 @@ str = File.read("schema.json")
|
|
7
7
|
schema = JSON.parse(str)
|
8
8
|
|
9
9
|
use Rack::JsonSchema::Docs, schema: schema
|
10
|
+
use Rack::JsonSchema::SchemaProvider, schema: schema
|
10
11
|
use Rack::JsonSchema::ErrorHandler
|
11
12
|
use Rack::JsonSchema::RequestValidation, schema: schema
|
12
13
|
use Rack::JsonSchema::ResponseValidation, schema: schema if ENV["RACK_ENV"] == "test"
|
@@ -198,3 +199,36 @@ List existing apps.
|
|
198
199
|
|
199
200
|
...
|
200
201
|
```
|
202
|
+
|
203
|
+
### Rack::JsonSchema::SchemaProvider
|
204
|
+
Serves JSON Schema at `GET /schema`.
|
205
|
+
|
206
|
+
* You can give `path` option to change default path: `GET /schema`
|
207
|
+
* This middleware is also bundled in the `specup` executable command
|
208
|
+
|
209
|
+
```sh
|
210
|
+
$ specup schema.json
|
211
|
+
[2014-06-06 23:01:35] INFO WEBrick 1.3.1
|
212
|
+
[2014-06-06 23:01:35] INFO ruby 2.0.0 (2013-06-27) [x86_64-darwin12.5.0]
|
213
|
+
[2014-06-06 23:01:35] INFO WEBrick::HTTPServer#start: pid=24303 port=8080
|
214
|
+
|
215
|
+
$ curl :8080/schema -i
|
216
|
+
HTTP/1.1 200 OK
|
217
|
+
Content-Type: text/plain; charset=utf-8
|
218
|
+
Server: WEBrick/1.3.1 (Ruby/2.1.1/2014-02-24)
|
219
|
+
Date: Sat, 07 Jun 2014 19:58:04 GMT
|
220
|
+
Content-Length: 2175
|
221
|
+
Connection: Keep-Alive
|
222
|
+
|
223
|
+
{
|
224
|
+
"$schema": "http://json-schema.org/draft-04/hyper-schema",
|
225
|
+
"definitions": {
|
226
|
+
"app": {
|
227
|
+
"$schema": "http://json-schema.org/draft-04/hyper-schema",
|
228
|
+
"description": "An app is a program to be deployed.",
|
229
|
+
"id": "schemata/app",
|
230
|
+
"title": "App",
|
231
|
+
...
|
232
|
+
}
|
233
|
+
}
|
234
|
+
}
|
data/bin/specup
CHANGED
@@ -21,6 +21,7 @@ end
|
|
21
21
|
|
22
22
|
app = Rack::Builder.new do
|
23
23
|
use Rack::JsonSchema::Docs, schema: schema
|
24
|
+
use Rack::JsonSchema::SchemaProvider, schema: schema
|
24
25
|
use Rack::JsonSchema::ErrorHandler
|
25
26
|
use Rack::JsonSchema::Mock, schema: schema
|
26
27
|
run ->(env) { [404, {}, ["Not found"]] }
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Rack
|
2
|
+
module JsonSchema
|
3
|
+
class SchemaProvider
|
4
|
+
DEFAULT_PATH = "/schema"
|
5
|
+
|
6
|
+
# Behaves as a rack-middleware
|
7
|
+
# @param app [Object] Rack application
|
8
|
+
# @param path [String, nil] URL path to return JSON Schema (default: /schema)
|
9
|
+
# @param schema [Hash] Schema object written in JSON schema format
|
10
|
+
# @raise [JsonSchema::SchemaError]
|
11
|
+
def initialize(app, path: nil, schema: nil)
|
12
|
+
@app = app
|
13
|
+
@path = path
|
14
|
+
@schema = schema
|
15
|
+
end
|
16
|
+
|
17
|
+
# Returns rendered document for document request
|
18
|
+
# @return [Array] Rack response
|
19
|
+
# @param env [Hash] Rack env
|
20
|
+
def call(env)
|
21
|
+
if env["REQUEST_METHOD"] == "GET" && env["PATH_INFO"] == path
|
22
|
+
[
|
23
|
+
200,
|
24
|
+
{ "Content-Type" => "application/json" },
|
25
|
+
[rendered_schema],
|
26
|
+
]
|
27
|
+
else
|
28
|
+
@app.call(env)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
# @return [String] Path to return document
|
35
|
+
def path
|
36
|
+
@path || DEFAULT_PATH
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return [String] Rendered JSON Schema in JSON format
|
40
|
+
def rendered_schema
|
41
|
+
MultiJson.encode(@schema, pretty: true)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/rack/json_schema.rb
CHANGED
@@ -0,0 +1,93 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Rack::JsonSchema::SchemaProvider do
|
4
|
+
include Rack::Test::Methods
|
5
|
+
|
6
|
+
let(:app) do
|
7
|
+
local_schema_url_path = schema_url_path
|
8
|
+
local_schema = schema
|
9
|
+
Rack::Builder.app do
|
10
|
+
use Rack::JsonSchema::SchemaProvider, path: local_schema_url_path, schema: local_schema
|
11
|
+
run ->(env) do
|
12
|
+
[
|
13
|
+
200,
|
14
|
+
{},
|
15
|
+
["dummy"],
|
16
|
+
]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
let(:schema) do
|
22
|
+
str = File.read(schema_path)
|
23
|
+
JSON.parse(str)
|
24
|
+
end
|
25
|
+
|
26
|
+
let(:schema_path) do
|
27
|
+
File.expand_path("../../../fixtures/schema.json", __FILE__)
|
28
|
+
end
|
29
|
+
|
30
|
+
let(:schema_url_path) do
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
|
34
|
+
let(:response) do
|
35
|
+
last_response
|
36
|
+
end
|
37
|
+
|
38
|
+
let(:env) do
|
39
|
+
{}
|
40
|
+
end
|
41
|
+
|
42
|
+
let(:params) do
|
43
|
+
{}
|
44
|
+
end
|
45
|
+
|
46
|
+
subject do
|
47
|
+
send(verb, path, params, env)
|
48
|
+
response.status
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "#call" do
|
52
|
+
let(:verb) do
|
53
|
+
:get
|
54
|
+
end
|
55
|
+
|
56
|
+
context "without :path option" do
|
57
|
+
let(:path) do
|
58
|
+
"/schema"
|
59
|
+
end
|
60
|
+
|
61
|
+
it "returns JSON Schema to request to GET /schema" do
|
62
|
+
should == 200
|
63
|
+
response.body.should include("app")
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context "with :path option" do
|
68
|
+
let(:schema_url_path) do
|
69
|
+
"/json_schema"
|
70
|
+
end
|
71
|
+
|
72
|
+
let(:path) do
|
73
|
+
"/json_schema"
|
74
|
+
end
|
75
|
+
|
76
|
+
it "responds to specified path" do
|
77
|
+
should == 200
|
78
|
+
response.body.should_not == "dummy"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "without targeted request" do
|
83
|
+
let(:path) do
|
84
|
+
"/apps"
|
85
|
+
end
|
86
|
+
|
87
|
+
it "delegates request to inner app" do
|
88
|
+
should == 200
|
89
|
+
response.body.should == "dummy"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-json_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
@@ -189,11 +189,13 @@ files:
|
|
189
189
|
- lib/rack/json_schema/request_validation.rb
|
190
190
|
- lib/rack/json_schema/response_validation.rb
|
191
191
|
- lib/rack/json_schema/schema.rb
|
192
|
+
- lib/rack/json_schema/schema_provider.rb
|
192
193
|
- lib/rack/json_schema/version.rb
|
193
194
|
- rack-json_schema.gemspec
|
194
195
|
- spec/fixtures/schema.json
|
195
196
|
- spec/rack/spec/docs_spec.rb
|
196
197
|
- spec/rack/spec/mock_spec.rb
|
198
|
+
- spec/rack/spec/schema_provider_spec.rb
|
197
199
|
- spec/rack/spec_spec.rb
|
198
200
|
- spec/spec_helper.rb
|
199
201
|
homepage: https://github.com/r7kamura/rack-json_schema
|
@@ -224,6 +226,7 @@ test_files:
|
|
224
226
|
- spec/fixtures/schema.json
|
225
227
|
- spec/rack/spec/docs_spec.rb
|
226
228
|
- spec/rack/spec/mock_spec.rb
|
229
|
+
- spec/rack/spec/schema_provider_spec.rb
|
227
230
|
- spec/rack/spec_spec.rb
|
228
231
|
- spec/spec_helper.rb
|
229
232
|
has_rdoc:
|