rswag-api 2.1.1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rswag/api/middleware.rb +29 -5
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fdb4e3352a6d79128a80330791bfe3a82561c4e658c2e531316368098ddc2d26
4
- data.tar.gz: b21ab1fd96389fb193f9fd487c214b2ef6f0a30f010e9a07c9a2ab4ebb6f2ae9
3
+ metadata.gz: 8f5a77aa4988d76dcd76d251288729514f07fc0e4cb28bb04a184d6be53001f6
4
+ data.tar.gz: f13c18e015741835531ed97014fffb3d0ba647044ce948ea1d334852f4a64f0b
5
5
  SHA512:
6
- metadata.gz: 3dd345e95c82de2c375b71bbe4b443ee78f3648924ef12d84d44a9a1a144df3abbc40d35e57497eb58b94927890d997df2981aa4ced47ad75055b4e824196124
7
- data.tar.gz: 3091da2ac5c5c54acdb23c4b643a27b6917a2685835a0599603078069e6eacf214705e992e535e005104b00bfd3fc14465670362c3a579ef33a7e7d6749f5723
6
+ metadata.gz: b3548dbef713f3e93890d7198c70fe69b9c201081d2797a266add689e0a7a47a8f7a7b85feb38ae01da73abf222138bd387e69beb9cb9c3ef0f24b3a2cc50676
7
+ data.tar.gz: 02a830a48706320dcdd8ffb3269793376ffb331353ef331a77c54e51a589d70c901f35b20fd3a95ef122e0b8e1d29410b6873be97d0f20f226d38ef58114bf5b
@@ -1,8 +1,10 @@
1
1
  require 'json'
2
+ require 'yaml'
3
+ require 'rack/mime'
2
4
 
3
5
  module Rswag
4
6
  module Api
5
- class Middleware
7
+ class Middleware
6
8
 
7
9
  def initialize(app, config)
8
10
  @app = app
@@ -14,24 +16,46 @@ module Rswag
14
16
  filename = "#{@config.resolve_swagger_root(env)}/#{path}"
15
17
 
16
18
  if env['REQUEST_METHOD'] == 'GET' && File.file?(filename)
17
- swagger = load_json(filename)
19
+ swagger = parse_file(filename)
18
20
  @config.swagger_filter.call(swagger, env) unless @config.swagger_filter.nil?
21
+ mime = Rack::Mime.mime_type(::File.extname(path), 'text/plain')
22
+ body = unload_swagger(filename, swagger)
19
23
 
20
24
  return [
21
25
  '200',
22
- { 'Content-Type' => 'application/json' },
23
- [ JSON.dump(swagger) ]
26
+ { 'Content-Type' => mime },
27
+ [ body ]
24
28
  ]
25
29
  end
26
-
30
+
27
31
  return @app.call(env)
28
32
  end
29
33
 
30
34
  private
31
35
 
36
+ def parse_file(filename)
37
+ if /\.ya?ml$/ === filename
38
+ load_yaml(filename)
39
+ else
40
+ load_json(filename)
41
+ end
42
+ end
43
+
44
+ def load_yaml(filename)
45
+ YAML.safe_load(File.read(filename))
46
+ end
47
+
32
48
  def load_json(filename)
33
49
  JSON.parse(File.read(filename))
34
50
  end
51
+
52
+ def unload_swagger(filename, swagger)
53
+ if /\.ya?ml$/ === filename
54
+ YAML.dump(swagger)
55
+ else
56
+ JSON.dump(swagger)
57
+ end
58
+ end
35
59
  end
36
60
  end
37
61
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rswag-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richie Morris
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-18 00:00:00.000000000 Z
11
+ date: 2019-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  version: '0'
68
68
  requirements: []
69
69
  rubyforge_project:
70
- rubygems_version: 2.7.7
70
+ rubygems_version: 2.7.8
71
71
  signing_key:
72
72
  specification_version: 4
73
73
  summary: A Rails Engine that exposes Swagger files as JSON endpoints