rack-json_schema 1.0.7 → 1.1.0

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: 6a1a86a4622d0d9f2b35d84e2f0bcf398e483eb6
4
- data.tar.gz: 93d3efdfddb2b39fb584476b33a12aa0146cc793
3
+ metadata.gz: b28c973a0a7e88414ff495f23a4d4d74da9c3024
4
+ data.tar.gz: 17c6b63a58ad58f66db57f43266890ac633f67c2
5
5
  SHA512:
6
- metadata.gz: 1317f593a583c3a5a325eb4c3eadec87c26ea383ac287487f584e20b654fbdeb48430b0f9c3195c8e6f349a224ea04bcf902beb7c6b47b25e2a9cec95eb00cd6
7
- data.tar.gz: bfe7fe3c48dfa2a3cd31e0468055dab86581b23820277d36b0caa87f911ea59e8001b1df8c7b843fc768e7a76cbbcbb23207f5ce264dfde90d3b7662f3d8176c
6
+ metadata.gz: afcd17fe0f1bef5a94cf54a43deb42d6ddbb4b4d5db371e68571153ed12a76b11a75c9f3701eff176f789a60604dce4a397effba8d7beeb1ba77c0e20833ba28
7
+ data.tar.gz: 8bb15acd2a2f91be390215cf9a1c495e3f0982583ed62bda3bda7f4927c0682caca02aea73aa16d3161d044f0fe54c1815e0dc48664af281e3a07ca84372e526
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 1.1.0
2
+ * GET /docs returns HTML
3
+
1
4
  ## 1.0.7
2
5
  * Accepts GET /docs.md
3
6
 
data/README.md CHANGED
@@ -149,7 +149,7 @@ Rack::JsonSchema::Error
149
149
  ```
150
150
 
151
151
  ### Rack::JsonSchema::Docs
152
- Returns API documentation as a text/plain content, rendered in GitHub flavored Markdown.
152
+ Returns API documentation as text/html (GET /docs) or text/plain (GET /docs.md).
153
153
 
154
154
  * You can give `path` option to change default path: `GET /docs`
155
155
  * API documentation is powered by [jdoc](https://github.com/r7kamura/jdoc) gem
data/bin/specup CHANGED
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
  $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
3
- require "rack"
4
3
  require "rack/json_schema"
5
4
  require "yaml"
6
5
 
data/config.ru ADDED
@@ -0,0 +1,13 @@
1
+ require "rack/json_schema"
2
+
3
+ path = File.expand_path("../spec/fixtures/schema.json", __FILE__)
4
+ str = File.read(path)
5
+ schema = JSON.parse(str)
6
+
7
+ use Rack::JsonSchema::Docs, schema: schema
8
+ use Rack::JsonSchema::SchemaProvider, schema: schema
9
+ use Rack::JsonSchema::ErrorHandler
10
+ use Rack::JsonSchema::RequestValidation, schema: schema
11
+ use Rack::JsonSchema::ResponseValidation, schema: schema
12
+ use Rack::JsonSchema::Mock, schema: schema
13
+ run ->(env) { [404, {}, ["Not found"]] }
@@ -1,3 +1,4 @@
1
+ require "erubis"
1
2
  require "jdoc"
2
3
  require "json"
3
4
  require "json_schema"
@@ -10,21 +10,20 @@ module Rack
10
10
  def initialize(app, path: nil, schema: nil)
11
11
  @app = app
12
12
  @path = path
13
- @document = Jdoc::Generator.call(schema)
13
+ @markdown = Jdoc::Generator.call(schema)
14
+ @html = Jdoc::Generator.call(schema, html: true)
14
15
  end
15
16
 
16
17
  # Returns rendered document for document request
17
18
  # @param env [Hash] Rack env
18
19
  def call(env)
19
- if env["REQUEST_METHOD"] == "GET" && env["PATH_INFO"].gsub(/\.md\z/, "") == path
20
- [
21
- 200,
22
- { "Content-Type" => "text/plain; charset=utf-8" },
23
- [@document],
24
- ]
25
- else
26
- @app.call(env)
27
- end
20
+ DocumentGenerator.call(
21
+ app: @app,
22
+ env: env,
23
+ html: @html,
24
+ markdown: @markdown,
25
+ path: path,
26
+ )
28
27
  end
29
28
 
30
29
  private
@@ -33,6 +32,96 @@ module Rack
33
32
  def path
34
33
  @path || DEFAULT_PATH
35
34
  end
35
+
36
+ class DocumentGenerator
37
+ def self.call(*args)
38
+ new(*args).call
39
+ end
40
+
41
+ # @param app [Object] Rack application
42
+ # @param env [Hash] Rack env
43
+ # @param html [String] HTML rendered docs
44
+ # @param markdown [String] Markdown rendered docs
45
+ # @param path [String] Route for docs
46
+ def initialize(app: nil, env: nil, html: nil, markdown: nil, path: nil)
47
+ @app = app
48
+ @env = env
49
+ @html = html
50
+ @markdown = markdown
51
+ @path = path
52
+ end
53
+
54
+ # Generates suited response body from given env & document to docs request
55
+ # @return [Array] Rack response
56
+ def call
57
+ if has_docs_request?
58
+ if has_markdown_request?
59
+ markdown_response
60
+ else
61
+ html_response
62
+ end
63
+ else
64
+ delegate
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ # Delegates request to given rack app
71
+ def delegate
72
+ @app.call(@env)
73
+ end
74
+
75
+ # @return [true, false] True if docs are requested
76
+ def has_docs_request?
77
+ request_method == "GET" && path_without_extname == @path
78
+ end
79
+
80
+ # @return [true, false] True if raw markdown content are requested
81
+ def has_markdown_request?
82
+ extname == ".md"
83
+ end
84
+
85
+ # @return [String] Extension name of request path
86
+ # @example
87
+ # extname #=> ".md"
88
+ def extname
89
+ ::File.extname(path)
90
+ end
91
+
92
+ # @return [String] Request path
93
+ def path
94
+ @env["PATH_INFO"]
95
+ end
96
+
97
+ # @return [String]
98
+ def request_method
99
+ @env["REQUEST_METHOD"]
100
+ end
101
+
102
+ # @return [String]
103
+ def path_without_extname
104
+ path.gsub(/\..+\z/, "")
105
+ end
106
+
107
+ # @return [Array] Rack response of raw markdown text
108
+ def markdown_response
109
+ [
110
+ 200,
111
+ { "Content-Type" => "text/plain; charset=utf-8" },
112
+ [@markdown],
113
+ ]
114
+ end
115
+
116
+ # @return [Array] Rack response of human readable HTML document, rendered from given document
117
+ def html_response
118
+ [
119
+ 200,
120
+ { "Content-Type" => "text/html; charset=utf-8" },
121
+ [@html],
122
+ ]
123
+ end
124
+ end
36
125
  end
37
126
  end
38
127
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  module JsonSchema
3
- VERSION = "1.0.7"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
@@ -17,7 +17,8 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_dependency "jdoc", ">= 0.0.3"
20
+ spec.add_dependency "erubis"
21
+ spec.add_dependency "jdoc", ">= 0.1.4"
21
22
  spec.add_dependency "json_schema"
22
23
  spec.add_dependency "rack"
23
24
  spec.add_development_dependency "bundler", "~> 1.5"
@@ -27,4 +28,5 @@ Gem::Specification.new do |spec|
27
28
  spec.add_development_dependency "rspec", "2.14.1"
28
29
  spec.add_development_dependency "rspec-console"
29
30
  spec.add_development_dependency "rspec-json_matcher"
31
+ spec.add_development_dependency "shotgun"
30
32
  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.7
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -10,20 +10,34 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2014-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: erubis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: jdoc
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - ">="
18
32
  - !ruby/object:Gem::Version
19
- version: 0.0.3
33
+ version: 0.1.4
20
34
  type: :runtime
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
38
  - - ">="
25
39
  - !ruby/object:Gem::Version
26
- version: 0.0.3
40
+ version: 0.1.4
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: json_schema
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -150,6 +164,20 @@ dependencies:
150
164
  - - ">="
151
165
  - !ruby/object:Gem::Version
152
166
  version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: shotgun
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
153
181
  description:
154
182
  email:
155
183
  - r7kamura@gmail.com
@@ -165,6 +193,7 @@ files:
165
193
  - README.md
166
194
  - Rakefile
167
195
  - bin/specup
196
+ - config.ru
168
197
  - lib/rack-json_schema.rb
169
198
  - lib/rack/json_schema.rb
170
199
  - lib/rack/json_schema/base_request_handler.rb