swagger_yard-rails 0.2.0 → 0.3.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: c94696f2feaf6e856d6ea364376c58a27ee9a9ba
4
- data.tar.gz: 08976e63e0846806c8c2c94303a1c7191fa882e0
3
+ metadata.gz: c9a3c7979e9094f3ae93147cf96f9d5911f4e76a
4
+ data.tar.gz: 6d3f96edd8d111b2a04e66dd92d89c65cdf15bde
5
5
  SHA512:
6
- metadata.gz: 5c12fefc9e29ebc4e924f8734ae2559f98f013aea0b85743f1a7faa7c472417d1dfe141f6f684f253cd8a2250e6f2cf38d6dd1fe3a0e82545d31a3a435c18ba7
7
- data.tar.gz: afc7e95748615318a546b666ffb913753d8bdc61792c6bb6122d5be293c81458f053e1ab3f81c6b43c41bfcb194fd734abc40f6a733c3628b1acbe1dfad6a631
6
+ metadata.gz: 824fd5b2ea22087dbd9bd1605d575ffab6ae34367dc437eb6506594c51c27ae6460a0cd8c2be0f689ec7d2ed524e52dd5542c91f2c4c9fc585e265d7b1124fb5
7
+ data.tar.gz: 77316e1bdc076df24a5a72dc9c62c7fe745ca2c99cc89306cef1f056abdc7244856d4b41d9d9884c24d9abe865267979c5df3126a96b4dae80870d837d486d83
@@ -1,4 +1,14 @@
1
- ## SwaggerYard 0.2.0 -- 20-10-2015 ##
1
+ # SwaggerYard-Rails Changelog
2
+
3
+ ## 0.3.0 -- 16-12-2015 ##
4
+
5
+ * Don't clobber controller/model lookup paths in initializer if they have
6
+ already been set
7
+ * Look up routes using the Rails router; allows DRYing routes by omitting @path
8
+ tags in most cases.
9
+ * Fix `swagger:json` rake task
10
+
11
+ ## 0.2.0 -- 20-10-2015 ##
2
12
 
3
13
  * Support for Swagger's Spec v2
4
14
 
@@ -8,7 +18,7 @@
8
18
 
9
19
  *Tony Pitale <@tpitale>*
10
20
 
11
- ## SwaggerYard 0.1.0 -- 15-10-2015 ##
21
+ ## 0.1.0 -- 15-10-2015 ##
12
22
 
13
23
  * Add Engine from SwaggerYard
14
24
  * Update specs to make requests of rails app
@@ -1,13 +1,24 @@
1
1
  # Contributor Code of Conduct
2
2
 
3
- As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
3
+ As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
4
 
5
- We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality.
6
6
 
7
- Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
7
+ Examples of unacceptable behavior by participants include:
8
8
 
9
- Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
9
+ * The use of sexualized language or imagery
10
+ * Personal attacks
11
+ * Trolling or insulting/derogatory comments
12
+ * Public or private harassment
13
+ * Publishing other's private information, such as physical or electronic addresses, without explicit permission
14
+ * Other unethical or unprofessional conduct
10
15
 
11
- Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
16
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
12
17
 
13
- This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
18
+ By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team.
19
+
20
+ This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community.
21
+
22
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting a project maintainer at [INSERT EMAIL ADDRESS]. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. Maintainers are obligated to maintain confidentiality with regard to the reporter of an incident.
23
+
24
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.3.0, available at [http://contributor-covenant.org/version/1/3/0/](http://contributor-covenant.org/version/1/3/0/)
data/README.md CHANGED
@@ -49,16 +49,60 @@ in application.js:
49
49
 
50
50
  ### Configure SwaggerYard ###
51
51
 
52
+ See [Getting Started] in the swagger_yard docs for details.
53
+
52
54
  ```ruby
53
55
  # config/initializers/swagger_yard.rb
54
56
  SwaggerYard.configure do |config|
55
- # the rest of your configuration here
57
+ # your configuration here
58
+ end
59
+ ```
60
+
61
+ [Getting Started]: https://github.com/tpitale/swagger_yard#getting-started
62
+
63
+ ### Write YARD documentation ###
56
64
 
57
- # where your swagger spec json will show up
58
- config.swagger_spec_base_path = "http://localhost:3000/swagger/api"
65
+ Write YARD documentation for your controllers and models as described in
66
+ [SwaggerYard Usage].
67
+
68
+ To include a controller in your swagger definition, add a `@resource` tag to the
69
+ controller's class documentation.
70
+
71
+ ```ruby
72
+ # @resource Pet
73
+ class PetsController < ApplicationController
74
+ # ...
59
75
  end
60
76
  ```
61
77
 
78
+ Once a controller has been tagged as a swagger resource, all public instance
79
+ methods with docstrings will be candidates for inclusion as swagger apis, even
80
+ if they don't include `@path` tags. `swagger_yard-rails` includes a
81
+ [path discovery function] implementation that uses the Rails router to lookup
82
+ paths, allowing `@path` tags to be omitted.
83
+
84
+ `swagger_yard-rails` also provides a `@route` tag to indicate a named route
85
+ to be connected to an api action.
86
+
87
+ ```ruby
88
+ # config/routes.rb
89
+ Dummy::Application.routes.draw do
90
+ get '/greet', to: 'hello#greeting', as: 'greeting'
91
+ end
92
+ ```
93
+
94
+ ```ruby
95
+ # @resource Hello
96
+ class HelloController < ApplicationController
97
+ # @route greeting
98
+ def greeting
99
+ end
100
+ end
101
+ ```
102
+
103
+ [SwaggerYard Usage]: https://github.com/tpitale/swagger_yard#swaggeryard-usage
104
+ [path discovery function]: https://github.com/tpitale/swagger_yard#path-discovery-function
105
+
62
106
  ## Development
63
107
 
64
108
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -7,3 +7,4 @@ module SwaggerYard
7
7
  end
8
8
 
9
9
  require "swagger_yard/rails/engine"
10
+ require "swagger_yard/rails/route_inspector"
@@ -4,12 +4,17 @@ module SwaggerYard
4
4
  isolate_namespace SwaggerYard::Rails
5
5
  initializer "swagger_yard-rails.finisher_hook" do |app|
6
6
  SwaggerYard.register_custom_yard_tags!
7
+ # Usage:
8
+ # @route my-named-route
9
+ ::YARD::Tags::Library.define_tag("Route", :route)
7
10
  end
8
11
 
9
12
  initializer "swagger_yard-rails.paths" do |app|
10
13
  SwaggerYard.configure do |config|
11
- config.controller_path = ::Rails.root + 'app/controllers/**/*'
12
- config.model_path = ::Rails.root + 'app/models/**/*'
14
+ config.controller_path ||= ::Rails.root + 'app/controllers/**/*'
15
+ config.model_path ||= ::Rails.root + 'app/models/**/*'
16
+
17
+ config.path_discovery_function = RouteInspector.discovery_function
13
18
  end
14
19
  end
15
20
 
@@ -0,0 +1,75 @@
1
+ module SwaggerYard
2
+ module Rails
3
+ module RouteInspector
4
+ def self.discovery_function
5
+ JourneyRoutes.new(::Rails.application.routes.set)
6
+ end
7
+
8
+ class Error < ::RuntimeError
9
+ end
10
+
11
+ class JourneyRoutes
12
+ def initialize(routes)
13
+ @routes = routes
14
+ end
15
+
16
+ def call(yard_obj)
17
+ return nil if skip_object?(yard_obj)
18
+
19
+ info = yard_info(yard_obj)
20
+ route = find_route(info)
21
+ method = route.verb.source.gsub(/[$^]/, '')
22
+
23
+ raise Error, "no http method: #{info.inspect}" if method.empty?
24
+
25
+ path = route.parts.inject(route.ast.to_s) do |p,sym|
26
+ p.sub(sym.inspect, "{#{sym}}")
27
+ end
28
+
29
+ # FIXME: always remove format parameter?
30
+ path = path.sub('(.{format})', '')
31
+
32
+ [method, path]
33
+ end
34
+
35
+ alias_method :[], :call
36
+
37
+ private
38
+ def skip_object?(obj)
39
+ obj.scope != :instance ||
40
+ obj.visibility != :public ||
41
+ obj.docstring.blank?
42
+ end
43
+
44
+ def yard_info(obj)
45
+ {}.tap do |info|
46
+ route_tag = obj.tags.detect {|t| t.tag_name == 'route' }
47
+ if route_tag
48
+ info[:route] = route_tag.text
49
+ else
50
+ controller, action = obj.path.split obj.sep, 2
51
+ info[:action] = action
52
+ info[:controller] = controller.underscore.sub '_controller', ''
53
+ end
54
+ end
55
+ end
56
+
57
+ def find_route(info)
58
+ matching_routes = @routes.select do |r|
59
+ if info[:route]
60
+ r.name == info[:route]
61
+ else
62
+ r.defaults[:controller] == info[:controller] &&
63
+ r.defaults[:action] == info[:action]
64
+ end
65
+ end
66
+
67
+ raise Error, "too many matches: #{info.inspect}" if matching_routes.size > 1
68
+ raise Error, "no matching route: #{info.inspect}" if matching_routes.size < 1
69
+
70
+ matching_routes.first
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -1,7 +1,7 @@
1
1
  namespace :swagger do
2
2
  task :json, [:filename] => :environment do |t, args|
3
3
  File.open(args[:filename] || "swagger.json", "w") do |f|
4
- f.puts JSON.pretty_generate(SwaggerYard::Swagger.new.swagger_v2)
4
+ f.puts JSON.pretty_generate(SwaggerYard::Swagger.new.to_h)
5
5
  end
6
6
  end
7
7
  end
@@ -1,5 +1,5 @@
1
1
  module SwaggerYard
2
2
  module Rails
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.require_paths = ["lib"]
29
29
 
30
30
  spec.add_dependency "rails", ">= 3.2.8"
31
- spec.add_dependency "swagger_yard", "~> 0.2.0"
31
+ spec.add_dependency "swagger_yard", "~> 0.2"
32
32
 
33
33
  spec.add_development_dependency "bundler", "~> 1.10"
34
34
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagger_yard-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Pitale
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-21 00:00:00.000000000 Z
11
+ date: 2015-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.2.0
33
+ version: '0.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.2.0
40
+ version: '0.2'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -161,6 +161,7 @@ files:
161
161
  - config/routes.rb
162
162
  - lib/swagger_yard/rails.rb
163
163
  - lib/swagger_yard/rails/engine.rb
164
+ - lib/swagger_yard/rails/route_inspector.rb
164
165
  - lib/swagger_yard/rails/tasks.rake
165
166
  - lib/swagger_yard/rails/version.rb
166
167
  - swagger_yard-rails.gemspec