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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6199475826180211b7537e2b602a2fc83defb0e
4
- data.tar.gz: 60b7fb0b6bd29b358c7d8962156cabb0bd528cd3
3
+ metadata.gz: 815b05f4d5c15a188bf82d80a5e7e30ec74a3bc3
4
+ data.tar.gz: a844559ab3bbe3106419b951c2f5bf79b7355b49
5
5
  SHA512:
6
- metadata.gz: 4d3fbe63ce3402525682b739a24bab0ff0b35fbfd8ae8339502a23a22311ca9d15d819da570d5813e1bb237923060d5f73b231529e28fc46b2048f2397e19c85
7
- data.tar.gz: 89abc59441bdc661a499a3e87f4484b50e6268db4162fcbe86898d4d6bba0b34b3935932f937ca3ea3475114f23e85f84c4ab9712a6d064b9101607b1a013a85
6
+ metadata.gz: b44aca2e36b35a338c0ea8c246bff83d1032c3537a7980e178c0c6e60ee958f1a7a79316356b102a8a8d72de4c2ddb56747565cd5609cce1a9f8f80586823573
7
+ data.tar.gz: d4aa4980c6ea9796e2a9487ef8df3689f0127c1d8513d1c745958129038e5f8eaf7f4431f831a534a8a5e68aa9089e30e9fdba5e1646707e40e045ec97898de4
@@ -0,0 +1,5 @@
1
+ ## 0.0.2
2
+ * Remove dependency on rack-spec
3
+
4
+ ## 0.0.1
5
+ * 1st Release
data/README.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Jdoc
2
2
  Generate API documentation from JSON Schema.
3
3
 
4
+ ## Install
5
+ ```sh
6
+ $ gem install jdoc
7
+ ```
8
+
4
9
  ## Usage
5
10
  See [example-api-documentation.md](example-api-documentation.md) generated by the following command.
6
11
 
@@ -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"
@@ -2,7 +2,6 @@ require "cgi"
2
2
  require "erubis"
3
3
  require "json_schema"
4
4
  require "multi_json"
5
- require "rack/spec"
6
5
 
7
6
  require "jdoc/generator"
8
7
  require "jdoc/link"
@@ -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(RequestBodyGenerator.call(schema), pretty: true) + "\n"
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
- Rack::Spec::Mock::ResponseGenerator.call(schema)
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 RequestBodyGenerator
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::RequestBodyGenerator(schema) #=> { "name" => "example", "description" => "foo bar." }
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
@@ -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
- @raw_schema = schema
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
- @raw_schema["title"]
28
+ @json_schema.title
21
29
  end
22
30
 
23
31
  private
24
32
 
25
- # @return [Array<Jdoc::Schema::Link>] Sorted links
33
+ # @return [Array] All links defined in given JSON schema
34
+ # @example
35
+ # schema.links #=> [#<JsonSchema::Schema::Link>]
26
36
  def links
27
- rack_schema.links.map {|link| Link.new(link: link) }.sort
28
- end
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
@@ -1,3 +1,3 @@
1
1
  module Jdoc
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
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.1
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