swagui 0.0.6 → 0.1.1

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: 5ab94a4ed97c6df7d5274224ffcf4da84f6e1367
4
- data.tar.gz: 4821b3413d845dbcb2d94cfd4450686cd0264d47
3
+ metadata.gz: 023033ea0c161ce867c7a46fabc90d0dac05cac2
4
+ data.tar.gz: 46963da87b4e990b4f8ecf485844aa8848ecec97
5
5
  SHA512:
6
- metadata.gz: 667c6bac6f78a29211d5d08827bc4563ad7a6afc7adbbd31065d77feb286c6728ceebaf7212e9f4693dfd20733df5772f6be9b711bb3991009053d7d9b26c943
7
- data.tar.gz: 67bbada1aa02f87a3a7517b63555b1a522e94b23971fb8cbef142f688db358581cd4f983f2802630ba1cd609282346bde1e15794cbf196e4bf33fcd2d5f1f8e2
6
+ metadata.gz: 232abfff9686dcd15518fd398b6954ed12d287d46e8aa67086c40ad698001706dafa2db9649af5ed15bc20d4f5e4c9420f1c15787df2bf49677dbae8045274ba
7
+ data.tar.gz: a81e3d6c0adccc86cd42c9f116526156ec0e15e72daa453f4d3ae670892aacd070747b1888964973c5553a60a5109188ff9ee4b61c96e61f4d4f60ee531e1b24
data/README.md CHANGED
@@ -19,7 +19,7 @@ In your `application.rb` (for Rails app) or `config.ru` (for Rack app), add Swig
19
19
 
20
20
  ```ruby
21
21
  if Rack.env == 'development'
22
- use Swagui::Middleware, url: '/doc', path: 'doc'
22
+ use Swagui::App, url: '/doc', path: 'doc'
23
23
  end
24
24
  ```
25
25
 
data/bin/swagui CHANGED
@@ -6,5 +6,5 @@ require 'swagui'
6
6
  base_server = lambda {|x| [200, {}, ['']]}
7
7
 
8
8
  server = WEBrick::HTTPServer.new(:Port => 9292)
9
- server.mount '/', Rack::Handler::WEBrick, Swagui::Middleware.new(base_server, url: '/', path: (ARGV[0] || '.'))
9
+ server.mount '/', Rack::Handler::WEBrick, Swagui::App.new(base_server, url: '/', path: (ARGV[0] || '.'))
10
10
  server.start
data/lib/swagui/app.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'rack'
2
+ require 'json'
3
+ require 'yaml'
4
+
5
+ module Swagui
6
+
7
+ class App
8
+
9
+ def initialize(app, options={}, &blk)
10
+ @app = app
11
+ @asset_handler = AssetHandler.new(options[:url])
12
+ @swagger_doc_handler = SwaggerDocHandler.new(options[:path], options[:url])
13
+
14
+ if block_given?
15
+ @asset_stack = Rack::Auth::Basic.new(@asset_handler, "Restricted Documentation", &blk)
16
+ @swagger_doc_stack = Rack::Auth::Basic.new(@swagger_doc_handler, "Restricted Documentation", &blk)
17
+ else
18
+ @asset_stack = @asset_handler
19
+ @swagger_doc_stack = @swagger_doc_handler
20
+ end
21
+ end
22
+
23
+ def call(env)
24
+ if @asset_handler.handles?(env) # for assets (css/js) files
25
+ @asset_stack.call(env)
26
+ elsif @swagger_doc_handler.handles?(env) # for swagger json files
27
+ @swagger_doc_stack.call(env)
28
+ else
29
+ @app.call(env)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,20 @@
1
+ module Swagui
2
+ class AssetHandler
3
+
4
+ def initialize(path)
5
+ @url_regex = Regexp.union(Regexp.new("^\/swagger-ui"), Regexp.new("^#{path}\/?$"))
6
+ swagger_ui_dir = File.expand_path('../../swagger-ui', File.dirname(__FILE__))
7
+ @asset_file_server = Rack::File.new(swagger_ui_dir)
8
+ end
9
+
10
+ def handles?(env)
11
+ @url_regex === env["PATH_INFO"]
12
+ end
13
+
14
+ def call(env)
15
+ env["PATH_INFO"] = env["PATH_INFO"].gsub(@url_regex, '')
16
+ env["PATH_INFO"] = 'index.html' if env["PATH_INFO"] == ''
17
+ @asset_file_server.call(env)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,37 @@
1
+ module Swagui
2
+ class SwaggerDocHandler
3
+ def initialize(path, url)
4
+ @url_regex = Regexp.new("^#{url}")
5
+ app_doc_dir = File.expand_path(path || url, Dir.pwd)
6
+ @app_file_server = Rack::File.new(app_doc_dir)
7
+ end
8
+
9
+ def handles?(env)
10
+ @url_regex === env["PATH_INFO"]
11
+ end
12
+
13
+ def call(env)
14
+ path = env["PATH_INFO"].gsub(@url_regex, '') # root path renders index.html
15
+
16
+ env['HTTP_IF_MODIFIED_SINCE'] = nil # not 304s
17
+
18
+ response = [404, {"Content-Type"=>"application/json"}, '']
19
+ extension = ['', '.json', '.yml'].find do |ext|
20
+ response = @app_file_server.call(env.dup.merge!('PATH_INFO' => "#{path}#{ext}"))
21
+ response[0] == 200
22
+ end
23
+
24
+ # handles yaml parsing
25
+ if extension == '.yml'
26
+ body = ''
27
+ response[2].each {|f| body = YAML::load(f).to_json}
28
+ response[2] = [body]
29
+ response[1].merge!("Content-Length"=> body.size.to_s)
30
+ end
31
+
32
+ response[1].merge!("Content-Type"=>"application/json") # response is always json content
33
+
34
+ response
35
+ end
36
+ end
37
+ end
@@ -1,3 +1,3 @@
1
1
  module Swagui
2
- VERSION = "0.0.6"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/swagui.rb CHANGED
@@ -1,2 +1,4 @@
1
1
  require "swagui/version"
2
- require "swagui/middleware"
2
+ require "swagui/app"
3
+ require "swagui/asset_handler"
4
+ require "swagui/swagger_doc_handler"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Xu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-11 00:00:00.000000000 Z
11
+ date: 2014-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -67,7 +67,9 @@ files:
67
67
  - Rakefile
68
68
  - bin/swagui
69
69
  - lib/swagui.rb
70
- - lib/swagui/middleware.rb
70
+ - lib/swagui/app.rb
71
+ - lib/swagui/asset_handler.rb
72
+ - lib/swagui/swagger_doc_handler.rb
71
73
  - lib/swagui/version.rb
72
74
  - swagger-ui/css/reset.css
73
75
  - swagger-ui/css/screen.css
@@ -1,60 +0,0 @@
1
- require 'rack'
2
- require 'json'
3
- require 'yaml'
4
-
5
- module Swagui
6
-
7
- class Middleware
8
-
9
- def initialize(app, options={})
10
- @app = app
11
-
12
- @url_path = options[:url]
13
- @doc_path = options[:path] || @url_path
14
- @url_regex = Regexp.new("^#{@url_path}")
15
- @assets_url_regex = Regexp.new("^\/swagger-ui")
16
-
17
- app_doc_dir = File.expand_path(@doc_path, Dir.pwd)
18
- @app_file_server = Rack::File.new(app_doc_dir)
19
- swagger_ui_dir = File.expand_path('../../swagger-ui', File.dirname(__FILE__))
20
- @swagger_ui_file_server = Rack::File.new(swagger_ui_dir)
21
- end
22
-
23
- def call(env)
24
- path = env["PATH_INFO"].strip
25
- if @assets_url_regex.match(path) # for assets (css/js) files
26
- env["PATH_INFO"] = path.gsub(@assets_url_regex, '')
27
- @swagger_ui_file_server.call(env)
28
- elsif @url_regex.match(path)
29
- path = path.gsub(@url_regex, '') # root path renders index.html
30
- if path == '' || path == '/'
31
- env["PATH_INFO"] = 'index.html'
32
- @swagger_ui_file_server.call(env)
33
- else
34
-
35
- env['HTTP_IF_MODIFIED_SINCE'] = nil # not 304s
36
-
37
- response = [404, {"Content-Type"=>"application/json"}, '']
38
- extension = ['', '.json', '.yml'].find do |ext|
39
- response = @app_file_server.call(env.dup.merge!('PATH_INFO' => "#{path}#{ext}"))
40
- response[0] == 200
41
- end
42
-
43
- # handles yaml parsing
44
- if extension == '.yml'
45
- body = ''
46
- response[2].each {|f| body = YAML::load(f).to_json}
47
- response[2] = [body]
48
- response[1].merge!("Content-Length"=> body.size.to_s)
49
- end
50
-
51
- response[1].merge!("Content-Type"=>"application/json") # response is always json content
52
-
53
- response
54
- end
55
- else
56
- @app.call(env)
57
- end
58
- end
59
- end
60
- end