yard-grape 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c9db053d1be54cb6d7e09e27ba1cc39fb34f2ac5
4
+ data.tar.gz: 31889a545f7aa7ec854541253b88decfd6596117
5
+ SHA512:
6
+ metadata.gz: 56360ca28d50381beb8bc572e8f629a1a527a05c633c6e0641f384ba88b8be46e3966d66ea30728d7144e440548e827d8c0d2f17d0e31de006aeb7d8b70b975f
7
+ data.tar.gz: 8075830cfa1a5b047881f9afc569c96c0aa7cb9a6c8bf5bb6993d43d0ab00e80a41a91ca4cc05eec338c40a3d442c8fdc6b7400c026f973e08b74a190b740c10
data/LICENSE ADDED
File without changes
data/README.md ADDED
File without changes
data/lib/yard-grape.rb ADDED
@@ -0,0 +1 @@
1
+ require 'yard/grape'
data/lib/yard/grape.rb ADDED
@@ -0,0 +1,137 @@
1
+ require "yard"
2
+
3
+ module YARD
4
+
5
+ module Grape
6
+ def self.routes
7
+ YARD::Handlers::Grape::AbstractRouteHandler.routes
8
+ end
9
+
10
+ def self.error_handlers
11
+ YARD::Handlers::Grape::AbstractRouteHandler.error_handlers
12
+ end
13
+ end
14
+
15
+ module CodeObjects
16
+ class RouteObject < MethodObject
17
+ attr_accessor :http_verb, :http_path, :real_name
18
+
19
+ def name(prefix = false)
20
+ return super unless show_real_name?
21
+ prefix ? (sep == ISEP ? "#{sep}#{real_name}" : real_name.to_s) : real_name.to_sym
22
+ end
23
+
24
+ # @see YARD::Handlers::Grape::AbstractRouteHandler#register_route
25
+ # @see #name
26
+ def show_real_name?
27
+ real_name and caller[1] =~ /`signature'/
28
+ end
29
+ end
30
+ end
31
+
32
+ module Handlers
33
+
34
+ # Displays Grape routes in YARD documentation.
35
+ # Can also be used to parse routes from files without executing those files.
36
+ module Grape
37
+ YARD::Tags::Library.define_tag("A Sample Tag", :real_name)
38
+
39
+ # Logic both handlers have in common.
40
+ module AbstractRouteHandler
41
+ def self.routes
42
+ @routes ||= []
43
+ end
44
+
45
+ def self.error_handlers
46
+ @error_handlers ||= []
47
+ end
48
+
49
+ def process
50
+ case http_verb
51
+ when 'NOT_FOUND'
52
+ register_error_handler(http_verb)
53
+ else
54
+ path = http_path
55
+ path = $1 if path =~ /^"(.*)"$/
56
+ register_route(http_verb, path)
57
+ end
58
+ end
59
+
60
+ def register_route(verb, path, doc = nil)
61
+ # HACK: Removing some illegal letters.
62
+ method_name = "" << verb << "_" << path.gsub(/[^\w_]/, "_")
63
+ real_name = "" << verb << " " << path
64
+ route = register CodeObjects::RouteObject.new(namespace, method_name, :instance) do |o|
65
+ o.visibility = "public"
66
+ o.source = statement.source
67
+ o.signature = real_name
68
+ o.explicit = true
69
+ o.scope = scope
70
+ o.docstring = statement.comments
71
+ o.http_verb = verb
72
+ o.http_path = path
73
+ o.real_name = real_name
74
+ o.add_file(parser.file, statement.line)
75
+ end
76
+ AbstractRouteHandler.routes << route
77
+ yield(route) if block_given?
78
+ end
79
+
80
+ def register_error_handler(verb, doc = nil)
81
+ error_handler = register CodeObjects::RouteObject.new(namespace, verb, :instance) do |o|
82
+ o.visibility = "public"
83
+ o.source = statement.source
84
+ o.signature = verb
85
+ o.explicit = true
86
+ o.scope = scope
87
+ o.docstring = statement.comments
88
+ o.http_verb = verb
89
+ o.real_name = verb
90
+ o.add_file(parser.file, statement.line)
91
+ end
92
+ AbstractRouteHandler.error_handlers << error_handler
93
+ yield(error_handler) if block_given?
94
+ end
95
+ end
96
+
97
+ class ResourceHandler < YARD::Handlers::Ruby::Base
98
+ handles method_call(:resource)
99
+ namespace_only
100
+
101
+ def process
102
+ name = statement.parameters.first.jump(:tstring_content, :ident).source.capitalize
103
+ object = YARD::CodeObjects::ClassObject.new(namespace, name)
104
+ register(object)
105
+ if object.tags(:real_name).any?
106
+ object.name = object.tags(:real_name).first.text
107
+ object.path = [object.namespace, object.name].join('::')
108
+ end
109
+ p statement.last.last
110
+ parse_block(statement.last.last, :namespace => object)
111
+ register(object)
112
+ end
113
+ end
114
+
115
+ # Route handler for YARD's source parser.
116
+ class RouteHandler < Ruby::Base
117
+ include AbstractRouteHandler
118
+
119
+ handles method_call(:get)
120
+ handles method_call(:post)
121
+ handles method_call(:put)
122
+ handles method_call(:delete)
123
+ handles method_call(:head)
124
+ handles method_call(:not_found)
125
+
126
+ def http_verb
127
+ statement.method_name(true).to_s.upcase
128
+ end
129
+
130
+ def http_path
131
+ statement.parameters.first.source
132
+ end
133
+ end
134
+
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,46 @@
1
+ require "sinatra/base"
2
+ require "user"
3
+
4
+ class ExampleApp < Sinatra::Base
5
+
6
+ # Settings for a given user
7
+ #
8
+ # @param [User] some user
9
+ # @return [Hash] settings for that user
10
+ def settings(some_user)
11
+ raise NotImplementedMethod
12
+ end
13
+
14
+ # Displays a settings page for the current user
15
+ #
16
+ # @see ExampleApp#settings
17
+ get "/settings" do
18
+ haml :settings, {}, :settings => settings(current_user)
19
+ end
20
+
21
+ # Error 404 Page Not Found
22
+ not_found do
23
+ haml :'404'
24
+ end
25
+
26
+ put("/settings") { }
27
+ delete("/settings") { }
28
+ post("/settings") { }
29
+ head("/settings") { }
30
+
31
+ namespace "/nested" do
32
+ # root
33
+ get do
34
+ "this is a route at the root of the namespace"
35
+ end
36
+
37
+ # nested route
38
+ get("/route") {"this is a nested route!"}
39
+
40
+ namespace "/double" do
41
+ # double nested route
42
+ get("/route") { }
43
+ end
44
+ end
45
+
46
+ end
@@ -0,0 +1,52 @@
1
+ require "yard/sinatra"
2
+
3
+ describe YARD::Sinatra do
4
+ before(:all) do
5
+ $NO_CONTINUATION_WARNING = true
6
+ YARD::Registry.load [File.expand_path("../../example_app.rb", __FILE__)], true
7
+ end
8
+
9
+ it "reads sinatra routes" do
10
+ YARD::Sinatra.routes.size.should == 8
11
+ end
12
+
13
+ it "sets properties correctly" do
14
+ settings_routes = YARD::Sinatra.routes.find_all {|r| r.http_path == "/settings" }
15
+ settings_routes.length.should == 5
16
+ settings_routes.each do |route|
17
+ %w[GET HEAD POST PUT DELETE].should include(route.http_verb)
18
+ route.http_path.should == "/settings"
19
+ route.file.should =~ /example_app\.rb$/
20
+ route.docstring.should =~ /Displays a settings page for the current user/ if route.http_verb == "GET"
21
+ end
22
+ end
23
+
24
+ it "reads sinatra error handlers" do
25
+ YARD::Sinatra.error_handlers.size.should == 1
26
+ end
27
+
28
+ it "sets error handlers correctly" do
29
+ YARD::Sinatra.error_handlers.each do |error_handler|
30
+ %w[NOT_FOUND].should include(error_handler.http_verb)
31
+ error_handler.file.should =~ /example_app\.rb$/
32
+ error_handler.docstring.should =~ /Error 404 Page Not Found/ if error_handler.http_verb == "NOT_FOUND"
33
+ end
34
+ end
35
+
36
+ it "recognizes namespaced routes" do
37
+ nested_routes = YARD::Sinatra.routes.find_all {|r| r.http_path[0..6] == "/nested" }
38
+ nested_routes.length.should == 3
39
+ nested_routes.each do |route|
40
+ if route.http_path == "/nested"
41
+ route.docstring.should == "root"
42
+ elsif route.http_path == "/nested/double/route"
43
+ route.docstring.should == "double nested route"
44
+ elsif route.http_path == "/nested/route"
45
+ route.docstring.should == "nested route"
46
+ else
47
+ raise "unknown route: #{route.http_path}!"
48
+ end
49
+ end
50
+ end
51
+
52
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yard-grape
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - nobody
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '2.6'
41
+ description: Displays Grape routes (including comments) in YARD output.
42
+ email: Jianfeng@revibe.fm
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/yard/grape.rb
48
+ - lib/yard-grape.rb
49
+ - spec/example_app.rb
50
+ - spec/yard/sinatra_spec.rb
51
+ - README.md
52
+ - LICENSE
53
+ homepage: http://github.com/winfield/yard-grape
54
+ licenses: []
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 2.0.3
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Displays Grape routes (including comments) in YARD output.
76
+ test_files: []
77
+ has_rdoc: