jdoc 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +5 -0
- data/jdoc.gemspec +0 -1
- data/lib/jdoc.rb +0 -1
- data/lib/jdoc/link.rb +30 -4
- data/lib/jdoc/schema.rb +16 -9
- data/lib/jdoc/version.rb +1 -1
- metadata +2 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 815b05f4d5c15a188bf82d80a5e7e30ec74a3bc3
|
4
|
+
data.tar.gz: a844559ab3bbe3106419b951c2f5bf79b7355b49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b44aca2e36b35a338c0ea8c246bff83d1032c3537a7980e178c0c6e60ee958f1a7a79316356b102a8a8d72de4c2ddb56747565cd5609cce1a9f8f80586823573
|
7
|
+
data.tar.gz: d4aa4980c6ea9796e2a9487ef8df3689f0127c1d8513d1c745958129038e5f8eaf7f4431f831a534a8a5e68aa9089e30e9fdba5e1646707e40e045ec97898de4
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
data/jdoc.gemspec
CHANGED
@@ -17,7 +17,6 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.require_paths = ["lib"]
|
18
18
|
|
19
19
|
spec.add_dependency "multi_json"
|
20
|
-
spec.add_dependency "rack-spec", ">= 0.1.7"
|
21
20
|
spec.add_development_dependency "activesupport"
|
22
21
|
spec.add_development_dependency "bundler", "~> 1.6"
|
23
22
|
spec.add_development_dependency "erubis"
|
data/lib/jdoc.rb
CHANGED
data/lib/jdoc/link.rb
CHANGED
@@ -64,7 +64,7 @@ module Jdoc
|
|
64
64
|
|
65
65
|
# @return [String, nil] Example request body in JSON format
|
66
66
|
def request_body
|
67
|
-
MultiJson.encode(
|
67
|
+
MultiJson.encode(RequestGenerator.call(schema), pretty: true) + "\n"
|
68
68
|
end
|
69
69
|
|
70
70
|
# @return [true, false] True if this endpoint must have request body
|
@@ -98,7 +98,7 @@ module Jdoc
|
|
98
98
|
# @return [Hash]
|
99
99
|
# @raise [Rack::Spec::Mock::ExampleNotFound]
|
100
100
|
def response_hash
|
101
|
-
|
101
|
+
ResponseGenerator.call(schema)
|
102
102
|
end
|
103
103
|
|
104
104
|
# @return [Fixnum] Order score, used to sort links by preferred method order
|
@@ -119,12 +119,12 @@ module Jdoc
|
|
119
119
|
end
|
120
120
|
end
|
121
121
|
|
122
|
-
class
|
122
|
+
class RequestGenerator
|
123
123
|
# Generates example request body from given schema
|
124
124
|
# @note Not includes properties that have readOnly property
|
125
125
|
# @return [Hash]
|
126
126
|
# @example
|
127
|
-
# Jdoc::Link::
|
127
|
+
# Jdoc::Link::RequestGenerator(schema) #=> { "name" => "example", "description" => "foo bar." }
|
128
128
|
def self.call(schema)
|
129
129
|
schema.properties.inject({}) do |result, (key, value)|
|
130
130
|
if value.data["readOnly"]
|
@@ -146,5 +146,31 @@ module Jdoc
|
|
146
146
|
end
|
147
147
|
end
|
148
148
|
end
|
149
|
+
|
150
|
+
class ResponseGenerator
|
151
|
+
# Generates example response Hash from given schema
|
152
|
+
# @return [Hash]
|
153
|
+
# @example
|
154
|
+
# Jdoc::Link::ResponseGenerator(schema) #=> { "id" => 1, "name" => "example" }
|
155
|
+
def self.call(schema)
|
156
|
+
schema.properties.inject({}) do |result, (key, value)|
|
157
|
+
result.merge(
|
158
|
+
key => case
|
159
|
+
when !value.properties.empty?
|
160
|
+
call(value)
|
161
|
+
when !value.data["example"].nil?
|
162
|
+
value.data["example"]
|
163
|
+
when value.type.include?("null")
|
164
|
+
nil
|
165
|
+
else
|
166
|
+
raise ExampleNotFound, "No example found for #{schema.pointer}/#{key}"
|
167
|
+
end
|
168
|
+
)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
class ExampleNotFound < StandardError
|
174
|
+
end
|
149
175
|
end
|
150
176
|
end
|
data/lib/jdoc/schema.rb
CHANGED
@@ -1,8 +1,16 @@
|
|
1
1
|
module Jdoc
|
2
2
|
class Schema
|
3
|
+
# Recursively extracts all links in given JSON schema
|
4
|
+
# @param json_schema [JsonSchema::Schema]
|
5
|
+
# @return [Array] An array of JsonSchema::Schema::Link
|
6
|
+
def self.extract_links(json_schema)
|
7
|
+
links = json_schema.links.select {|link| link.method && link.href }
|
8
|
+
links + json_schema.properties.map {|key, schema| extract_links(schema) }.flatten
|
9
|
+
end
|
10
|
+
|
3
11
|
# @param schema [Hash] JSON Schema
|
4
12
|
def initialize(schema)
|
5
|
-
@
|
13
|
+
@json_schema = JsonSchema.parse!(schema).tap(&:expand_references!)
|
6
14
|
end
|
7
15
|
|
8
16
|
# @return [Hash{Jdoc::Schema => Array}] Linkes table indexed by their schemata
|
@@ -17,19 +25,18 @@ module Jdoc
|
|
17
25
|
# @example
|
18
26
|
# schema.title #=> "app"
|
19
27
|
def title
|
20
|
-
@
|
28
|
+
@json_schema.title
|
21
29
|
end
|
22
30
|
|
23
31
|
private
|
24
32
|
|
25
|
-
# @return [Array
|
33
|
+
# @return [Array] All links defined in given JSON schema
|
34
|
+
# @example
|
35
|
+
# schema.links #=> [#<JsonSchema::Schema::Link>]
|
26
36
|
def links
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
# @return [Rack::Spec::Schema]
|
31
|
-
def rack_schema
|
32
|
-
@rack_schema ||= Rack::Spec::Schema.new(@raw_schema)
|
37
|
+
@links ||= self.class.extract_links(@json_schema).map do |link|
|
38
|
+
Link.new(link: link)
|
39
|
+
end.sort
|
33
40
|
end
|
34
41
|
end
|
35
42
|
end
|
data/lib/jdoc/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jdoc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
@@ -24,20 +24,6 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rack-spec
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: 0.1.7
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: 0.1.7
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: activesupport
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -145,6 +131,7 @@ extensions: []
|
|
145
131
|
extra_rdoc_files: []
|
146
132
|
files:
|
147
133
|
- ".gitignore"
|
134
|
+
- CHANGELOG.md
|
148
135
|
- Gemfile
|
149
136
|
- LICENSE.txt
|
150
137
|
- README.md
|