swagger_yard 0.4.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,31 +4,39 @@ module SwaggerYard
4
4
  new(types.first)
5
5
  end
6
6
 
7
- attr_reader :name, :source, :schema
7
+ # Default model location path
8
+ MODEL_PATH = '#/definitions/'.freeze
9
+
10
+ attr_reader :source
8
11
 
9
12
  def initialize(string)
10
13
  @source = string
11
- @schema = TypeParser.new.json_schema(string)
12
- @name = name_for(@schema)
13
- @name = name_for(@schema['items']) if @name == 'array'
14
+ @name = nil
14
15
  end
15
16
 
16
- # TODO: have this look at resource listing?
17
- def ref?
18
- schema["$ref"]
17
+ def name
18
+ return @name if @name
19
+ @name = name_for(schema)
20
+ @name = name_for(schema['items']) if @name == 'array'
21
+ @name
19
22
  end
20
23
 
21
- def model_name
22
- ref? ? name : nil
24
+ def schema
25
+ @schema ||= TypeParser.new.json_schema(source)
23
26
  end
24
27
 
25
- def to_h
26
- schema
28
+ def schema_with(options = {})
29
+ model_path = options && options[:model_path] || MODEL_PATH
30
+ if model_path != MODEL_PATH
31
+ TypeParser.new(model_path).json_schema(source)
32
+ else
33
+ schema
34
+ end
27
35
  end
28
36
 
29
37
  private
30
38
  def name_for(schema)
31
- schema["type"] || schema["$ref"][%r'#/definitions/(.*)', 1]
39
+ schema["type"] || schema["$ref"][%r'.*/([^/]*)$', 1]
32
40
  end
33
41
  end
34
42
  end
@@ -0,0 +1,34 @@
1
+ module SwaggerYard
2
+ class Type
3
+ def self.from_type_list(types)
4
+ new(types.first)
5
+ end
6
+
7
+ attr_reader :name, :source, :schema
8
+
9
+ def initialize(string)
10
+ @source = string
11
+ @schema = TypeParser.new.json_schema(string)
12
+ @name = name_for(@schema)
13
+ @name = name_for(@schema['items']) if @name == 'array'
14
+ end
15
+
16
+ # TODO: have this look at resource listing?
17
+ def ref?
18
+ schema["$ref"]
19
+ end
20
+
21
+ def model_name
22
+ ref? ? name : nil
23
+ end
24
+
25
+ def to_h
26
+ schema
27
+ end
28
+
29
+ private
30
+ def name_for(schema)
31
+ schema["type"] || schema["$ref"][%r'#/definitions/(.*)', 1]
32
+ end
33
+ end
34
+ end
@@ -69,7 +69,7 @@ module SwaggerYard
69
69
  else
70
70
  name = Model.mangle(v)
71
71
  if /[[:upper:]]/.match(name)
72
- { '$ref' => "#/definitions/#{name}" }
72
+ { '$ref' => "#{model_path}#{name}" }
73
73
  else
74
74
  { 'type' => name }
75
75
  end
@@ -81,7 +81,12 @@ module SwaggerYard
81
81
  unless url = SwaggerYard.config.external_schema[prefix]
82
82
  raise UndefinedSchemaError, "unknown prefix #{prefix} for #{name}"
83
83
  end
84
- { '$ref' => "#{url}#/definitions/#{Model.mangle(name)}"}
84
+ uri = URI(url)
85
+ fragment = uri.fragment ? "##{uri.fragment}" : model_path
86
+ uri.fragment = nil
87
+ fragment += '/' unless fragment.end_with?('/')
88
+ url = uri.to_s
89
+ { '$ref' => "#{url}#{fragment}#{Model.mangle(name)}"}
85
90
  end
86
91
 
87
92
  rule(formatted: { name: simple(:name), format: simple(:format) }) do
@@ -122,9 +127,10 @@ module SwaggerYard
122
127
  end
123
128
  end
124
129
 
125
- def initialize
130
+ def initialize(model_path = Type::MODEL_PATH)
126
131
  @parser = Parser.new
127
132
  @xform = Transform.new
133
+ @model_path = model_path
128
134
  end
129
135
 
130
136
  def parse(str)
@@ -132,7 +138,7 @@ module SwaggerYard
132
138
  end
133
139
 
134
140
  def json_schema(str)
135
- @xform.apply(parse(str))
141
+ @xform.apply(parse(str), model_path: @model_path)
136
142
  rescue Parslet::ParseFailed => e
137
143
  raise InvalidTypeError, "'#{str}': #{e.message}"
138
144
  end
@@ -1,3 +1,3 @@
1
1
  module SwaggerYard
2
- VERSION = "0.4.4"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagger_yard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - chtrinh (Chris Trinh)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-25 00:00:00.000000000 Z
11
+ date: 2018-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard
@@ -190,17 +190,21 @@ files:
190
190
  - README.md
191
191
  - Rakefile
192
192
  - lib/swagger_yard.rb
193
- - lib/swagger_yard/api.rb
194
- - lib/swagger_yard/api_declaration.rb
193
+ - lib/swagger_yard/api_group.rb
195
194
  - lib/swagger_yard/authorization.rb
196
195
  - lib/swagger_yard/configuration.rb
196
+ - lib/swagger_yard/example.rb
197
197
  - lib/swagger_yard/model.rb
198
+ - lib/swagger_yard/openapi.rb
198
199
  - lib/swagger_yard/operation.rb
200
+ - lib/swagger_yard/operation.rb.~9b471577ebed4e4ba6ed266566355dbe5990787d~
199
201
  - lib/swagger_yard/parameter.rb
202
+ - lib/swagger_yard/path_item.rb
200
203
  - lib/swagger_yard/property.rb
201
- - lib/swagger_yard/resource_listing.rb
204
+ - lib/swagger_yard/specification.rb
202
205
  - lib/swagger_yard/swagger.rb
203
206
  - lib/swagger_yard/type.rb
207
+ - lib/swagger_yard/type.rb.~master~
204
208
  - lib/swagger_yard/type_parser.rb
205
209
  - lib/swagger_yard/version.rb
206
210
  homepage: http://www.synctv.com
@@ -1,39 +0,0 @@
1
- module SwaggerYard
2
- class Api
3
- attr_accessor :path, :operations, :api_declaration
4
-
5
- def self.path_from_yard_object(yard_object)
6
- if tag = yard_object.tags.detect {|t| t.tag_name == "path"}
7
- tag.text
8
- elsif fn = SwaggerYard.config.path_discovery_function
9
- begin
10
- method, path = fn[yard_object]
11
- yard_object.add_tag YARD::Tags::Tag.new("path", path, [method]) if path
12
- path
13
- rescue => e
14
- SwaggerYard.log.warn e.message
15
- nil
16
- end
17
- end
18
- end
19
-
20
- def self.from_yard_object(yard_object, api_declaration)
21
- path = path_from_yard_object(yard_object)
22
- new(path, api_declaration)
23
- end
24
-
25
- def initialize(path, api_declaration)
26
- @api_declaration = api_declaration
27
- @path = path
28
- @operations = []
29
- end
30
-
31
- def add_operation(yard_object)
32
- @operations << Operation.from_yard_object(yard_object, self)
33
- end
34
-
35
- def operations_hash
36
- Hash[@operations.map {|op| [op.http_method.downcase, op.to_h]}]
37
- end
38
- end
39
- end
@@ -1,72 +0,0 @@
1
- module SwaggerYard
2
- class ApiDeclaration
3
- attr_accessor :description, :resource
4
- attr_reader :apis, :authorizations, :class_name
5
-
6
- def self.from_yard_object(yard_object)
7
- new.add_yard_object(yard_object)
8
- end
9
-
10
- def initialize
11
- @resource = nil
12
- @apis = {}
13
- @authorizations = {}
14
- end
15
-
16
- def valid?
17
- !@resource.nil?
18
- end
19
-
20
- def add_yard_object(yard_object)
21
- case yard_object.type
22
- when :class # controller
23
- add_info(yard_object)
24
- if valid?
25
- yard_object.children.each do |child_object|
26
- add_yard_object(child_object)
27
- end
28
- end
29
- when :method # actions
30
- add_api(yard_object)
31
- end
32
- self
33
- end
34
-
35
- def add_info(yard_object)
36
- @description = yard_object.docstring
37
- @class_name = yard_object.path
38
-
39
- if tag = yard_object.tags.detect {|t| t.tag_name == "resource"}
40
- @resource = tag.text
41
- end
42
-
43
- if tag = yard_object.tags.detect {|t| t.tag_name == "resource_path"}
44
- SwaggerYard.log.warn "DEPRECATED: @resource_path tag is obsolete."
45
- end
46
-
47
- # we only have api_key auth, the value for now is always empty array
48
- @authorizations = Hash[yard_object.tags.
49
- select {|t| t.tag_name == "authorize_with"}.
50
- map(&:text).uniq.
51
- map {|k| [k, []]}]
52
- end
53
-
54
- def add_api(yard_object)
55
- path = Api.path_from_yard_object(yard_object)
56
-
57
- return if path.nil?
58
-
59
- api = (apis[path] ||= Api.from_yard_object(yard_object, self))
60
- api.add_operation(yard_object)
61
- end
62
-
63
- def apis_hash
64
- Hash[apis.map {|path, api| [path, api.operations_hash]}]
65
- end
66
-
67
- def to_tag
68
- { "name" => resource,
69
- "description" => description }
70
- end
71
- end
72
- end