rails-swagger 0.0.2 → 0.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: 042e9785d813113e7b564fbf749427f2ffe1e1b1
4
- data.tar.gz: a0c8e5dc0cdb6bfab7aeb99d90320daeeab983e3
3
+ metadata.gz: 8b89c6bd9042fab14f38e6fd3521e3fa12a33490
4
+ data.tar.gz: fd06df4097b8b28ffb1b25f7eb70fb9a39adeab3
5
5
  SHA512:
6
- metadata.gz: 9c105c75ccfc2ba21357a383f66cdc979c9643d1850c9d35e7f69fbb33951095265ae601c79cf3e2740c80d843953be0212a67249ab53a67233d64cef1a8bc5a
7
- data.tar.gz: 3b2957c92d4b5645cb845f24e6e439bda15d90b9413496c6b57e98cdb87ca4ebc0c9d630de8de91acc59ccfebb47aabd6cdd03231e7c072f78c405f004f29a42
6
+ metadata.gz: 75d982c6a1d3813c9d3ffb413314305f1bfc8626ea63d218df0c8e0c57a6d03e921dfea64f2f9d2b007f56f561773f9c5cf217ec7544ee054a595240005ae754
7
+ data.tar.gz: 0f4da4227fb7fac70c09d58804e009182ed4faa6503eeae552a13eb85a5f68fd2181654d22a5fdeb766d6962c1d6b5619d9d987ddd98b91f9dc3ce28042cbe99
data/lib/rails/swagger.rb CHANGED
@@ -1,4 +1,4 @@
1
- require "jschema"
1
+ require "json-schema"
2
2
  require "rails/swagger/controller"
3
3
  require "rails/swagger/router"
4
4
  require "rails/swagger/engine"
@@ -2,26 +2,35 @@ module Rails
2
2
  module Swagger
3
3
  module Controller
4
4
 
5
+ # METHODS_WITH_BODIES = [
6
+ # :post,
7
+ # :patch,
8
+ # :put
9
+ # ].freeze
10
+
5
11
  # Injects swagger-related code into the controller when included
6
12
  def self.included base
7
13
 
8
14
  # Add controller hooks
9
- base.class_eval do
10
- before_action :validate_swagger_params
11
- end
15
+ # base.class_eval do
16
+ # before_action :swagger_validate_params
17
+ # end
12
18
 
13
19
  # Returns the swagger spec definition for the endpoint serving
14
20
  # the current request.
15
21
  def swagger_endpoint
16
22
  key = "#{params[:controller]}##{params[:action]}"
17
- endpoint = rails_swagger_engine.endpoints[key]
23
+ endpoint = swagger_engine.endpoints[key]
18
24
  end
19
25
 
20
26
  # Validates request parameters against the Swagger API spec
21
27
  # associated with this controller.
22
- def validate_swagger_params
23
- #puts swagger_endpoint.inspect.white
24
- end
28
+ # def swagger_validate_params
29
+ # if METHODS_WITH_BODIES.include? request.method_symbol
30
+ # body = request.POST
31
+ # # TODO: add validation here
32
+ # end
33
+ # end
25
34
 
26
35
  end
27
36
 
@@ -45,14 +45,6 @@ module Rails
45
45
  raise "Unsupported swagger version: #{document["swagger"]}. #{self} supports only version 2.0"
46
46
  end
47
47
 
48
- # # Parse the swagger schema
49
- # schema = nil
50
- # begin
51
- # schema = JSchema.build document
52
- # rescue JSchema::UnknownError, JSchema::InvalidSchema => e
53
- # raise $!, "Problem parsing swagger spec file \"#{file}\": #{e.message}", $@
54
- # end
55
-
56
48
  # Builds a routing tree based on the swagger spec file.
57
49
  # We'll add each endpoint to the routing tree and additionally
58
50
  # store it in an array to be used below.
@@ -90,14 +82,26 @@ module Rails
90
82
  end
91
83
  end
92
84
 
85
+ # Rack app for serving the original swagger file
86
+ swagger_app = Class.new do
87
+ def inspect
88
+ "Rails::Swagger::Engine"
89
+ end
90
+ define_method :call do |env|
91
+ [
92
+ 200,
93
+ {"Content-Type" => "application/json"},
94
+ [engine.schema.to_json]
95
+ ]
96
+ end
97
+ end
98
+
93
99
  # Adds routes to the engine by passing the Mapper to the top
94
100
  # of the routing tree. `self` inside the block refers to an
95
101
  # instance of `ActionDispatch::Routing::Mapper`.
96
102
  self.routes.draw do
97
103
  scope module: base_module.name.underscore, format: false do
98
- get "swagger.json", to: -> (env) do
99
- [200, {"Content-Type" => "application/json"}, [engine.schema.to_json]]
100
- end
104
+ get "swagger.json", to: swagger_app.new
101
105
  router.draw self
102
106
  end
103
107
  end
@@ -136,7 +140,7 @@ module Rails
136
140
  def self.included controller
137
141
  base_module = @base
138
142
  controller.include Controller
139
- define_method :rails_swagger_engine do
143
+ define_method :swagger_engine do
140
144
  base_module.const_get :Engine
141
145
  end
142
146
  end
@@ -1,16 +1,19 @@
1
1
  module Rails
2
2
  module Swagger
3
3
 
4
+ # Internally represents individual routes
4
5
  Endpoint = Struct.new(:method, :url, :definition, :_path) do
5
6
  def initialize *opts
6
7
  super
7
8
  self[:_path] = self.path
8
9
  end
10
+ # Translates path params from {bracket} syntax to :symbol syntax
9
11
  def path
10
12
  self[:url].gsub /\{(.+)\}/, ':\\1'
11
13
  end
12
14
  end
13
15
 
16
+ # Defines RESTful routing conventions
14
17
  RESOURCE_ROUTES = {
15
18
  get: :index,
16
19
  post: :create
@@ -28,7 +31,7 @@ module Rails
28
31
 
29
32
  def initialize prefix = [], parent = nil
30
33
  @parent = parent
31
- @prefix = prefix
34
+ @prefix = prefix.freeze
32
35
  @endpoints = []
33
36
  @subroutes = Hash.new do |hash, k|
34
37
  hash[k] = Router.new(@prefix + [k], self)
@@ -92,19 +95,22 @@ module Rails
92
95
  when :resource
93
96
 
94
97
  # Find collection-level resource actions
95
- actions = @endpoints.map{ |route| self.action_for route }.select{ |action| Symbol === action }
98
+ actions = @endpoints.map{ |r| self.action_for r }.select{ |a| Symbol === a }
96
99
 
97
100
  # Find parameter-level resource actions
98
- @subroutes.select{ |k, subroute| /^:/ === k}.values.each do |subroute|
99
- actions += subroute.endpoints.map{ |route| subroute.action_for route }.select{ |action| Symbol === action }
101
+ @subroutes.select{ |k, _| /^:/ === k }.values.each do |subroute|
102
+ actions += subroute.endpoints.map{ |r| subroute.action_for r }.select{ |a| Symbol === a }
100
103
  end
101
104
 
105
+ # Draw a resource
102
106
  map.resources @prefix.last.to_sym, only: actions do
103
107
  draw_actions! map
104
108
  draw_subroutes! map
105
109
  end
106
110
 
107
111
  when :namespace
112
+
113
+ # Draw a namespace (unless at the top)
108
114
  if @prefix.join("/").blank?
109
115
  draw_subroutes! map
110
116
  else
@@ -112,23 +118,17 @@ module Rails
112
118
  draw_subroutes! map
113
119
  end
114
120
  end
115
- when :action
116
- draw_actions! map
117
- end
118
121
 
119
- end
122
+ when :action
120
123
 
121
- def routing_tree
124
+ # Draw actions directly
125
+ draw_actions! map
122
126
 
123
- puts self.path + " - #{self.route_mode}"
124
- @endpoints.each do |route|
125
- puts "\t#{route[:method].to_s.upcase} to ##{self.action_for route} (#{self.action_mode})"
126
127
  end
127
- @subroutes.each do |k, subroute| subroute.routing_tree end
128
128
 
129
129
  end
130
130
 
131
- # Outputs the routing tree in text format
131
+ # Returns the routing tree in text format
132
132
  def to_s
133
133
 
134
134
  output = ""
@@ -145,13 +145,24 @@ module Rails
145
145
 
146
146
  end
147
147
 
148
+ # Outputs a visual representation of the routing tree
149
+ def _debug_routing_tree
150
+
151
+ puts self.path + " - #{self.route_mode}"
152
+ @endpoints.each do |route|
153
+ puts "\t#{route[:method].to_s.upcase} to ##{self.action_for route} (#{self.action_mode})"
154
+ end
155
+ @subroutes.each do |k, subroute| subroute._debug_routing_tree end
156
+
157
+ end
158
+
148
159
  protected
149
160
 
150
161
  def draw_actions! map
151
- indent = "\t" * @prefix.count
152
- endpoint = @prefix.last
162
+
153
163
  @endpoints.each do |route|
154
164
 
165
+ # Params hash for the route to be added
155
166
  params = Hash.new
156
167
  params[:via] = route[:method]
157
168
  params[:on] = self.action_mode unless self.action_mode == :param
@@ -161,15 +172,14 @@ module Rails
161
172
  next if Symbol === params[:action]
162
173
 
163
174
  # Add this individual route
164
- map.match endpoint, params
175
+ map.match @prefix.last, params
165
176
 
166
177
  end
178
+
167
179
  end
168
180
 
169
181
  def draw_subroutes! map
170
- @subroutes.values.each do |subroute|
171
- subroute.draw map
172
- end
182
+ @subroutes.values.each { |r| r.draw map }
173
183
  end
174
184
 
175
185
  end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :rails_swagger do
3
+ # # Task goes here
4
+ # end
File without changes
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-swagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenaniah Cerny
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-25 00:00:00.000000000 Z
11
+ date: 2017-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: jschema
14
+ name: json-schema
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -40,6 +40,20 @@ dependencies:
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - ">="
@@ -53,7 +67,7 @@ dependencies:
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
- name: rake
70
+ name: sqlite3
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
73
  - - ">="
@@ -77,6 +91,8 @@ files:
77
91
  - lib/rails/swagger/controller.rb
78
92
  - lib/rails/swagger/engine.rb
79
93
  - lib/rails/swagger/router.rb
94
+ - lib/tasks/rails/swagger_tasks.rake
95
+ - test/dummy/lib/assets/.keep
80
96
  homepage: https://github.com/kenaniah/rails-swagger
81
97
  licenses: []
82
98
  metadata: {}