ruby-swagger 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ruby-swagger.rb +7 -0
  3. data/lib/ruby-swagger/data/contact.rb +49 -0
  4. data/lib/ruby-swagger/data/definitions.rb +49 -0
  5. data/lib/ruby-swagger/data/document.rb +181 -0
  6. data/lib/ruby-swagger/data/example.rb +29 -0
  7. data/lib/ruby-swagger/data/external_documentation.rb +24 -0
  8. data/lib/ruby-swagger/data/header.rb +34 -0
  9. data/lib/ruby-swagger/data/headers.rb +48 -0
  10. data/lib/ruby-swagger/data/info.rb +56 -0
  11. data/lib/ruby-swagger/data/items.rb +45 -0
  12. data/lib/ruby-swagger/data/license.rb +51 -0
  13. data/lib/ruby-swagger/data/mime.rb +31 -0
  14. data/lib/ruby-swagger/data/operation.rb +82 -0
  15. data/lib/ruby-swagger/data/parameter.rb +88 -0
  16. data/lib/ruby-swagger/data/parameters.rb +53 -0
  17. data/lib/ruby-swagger/data/path.rb +115 -0
  18. data/lib/ruby-swagger/data/paths.rb +50 -0
  19. data/lib/ruby-swagger/data/reference.rb +30 -0
  20. data/lib/ruby-swagger/data/response.rb +25 -0
  21. data/lib/ruby-swagger/data/responses.rb +50 -0
  22. data/lib/ruby-swagger/data/schema.rb +68 -0
  23. data/lib/ruby-swagger/data/scopes.rb +44 -0
  24. data/lib/ruby-swagger/data/security_definitions.rb +49 -0
  25. data/lib/ruby-swagger/data/security_requirement.rb +35 -0
  26. data/lib/ruby-swagger/data/security_scheme.rb +67 -0
  27. data/lib/ruby-swagger/data/tag.rb +24 -0
  28. data/lib/ruby-swagger/data/url.rb +26 -0
  29. data/lib/ruby-swagger/data/xml_object.rb +15 -0
  30. data/lib/ruby-swagger/grape/grape.rb +2 -0
  31. data/lib/ruby-swagger/grape/grape_config.rb +160 -0
  32. data/lib/ruby-swagger/grape/grape_presenter.rb +48 -0
  33. data/lib/ruby-swagger/grape/grape_template.rb +67 -0
  34. data/lib/ruby-swagger/grape/method.rb +295 -0
  35. data/lib/ruby-swagger/grape/param.rb +33 -0
  36. data/lib/ruby-swagger/grape/route_path.rb +37 -0
  37. data/lib/ruby-swagger/grape/routes.rb +52 -0
  38. data/lib/ruby-swagger/grape/type.rb +141 -0
  39. data/lib/ruby-swagger/io/comparable.rb +30 -0
  40. data/lib/ruby-swagger/io/definitions.rb +48 -0
  41. data/lib/ruby-swagger/io/file_system.rb +97 -0
  42. data/lib/ruby-swagger/io/paths.rb +55 -0
  43. data/lib/ruby-swagger/io/security.rb +45 -0
  44. data/lib/ruby-swagger/object.rb +67 -0
  45. data/lib/ruby-swagger/railtie.rb +7 -0
  46. data/lib/ruby-swagger/template.rb +29 -0
  47. data/lib/tasks/swagger.rake +125 -0
  48. metadata +176 -0
@@ -0,0 +1,45 @@
1
+ require 'ruby-swagger/object'
2
+
3
+ module Swagger::Data
4
+ class Items < Swagger::Object # https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#itemsObject
5
+
6
+ attr_swagger :type, :format, :items, :collectionFormat, :default,
7
+ :maximum, :exclusiveMaximum, :minimum, :exclusiveMinimum,
8
+ :maxLength, :minLength, :pattern, :maxItems,
9
+ :minItems, :uniqueItems, :enum, :multipleOf
10
+
11
+ def initialize
12
+ @type = "string" #we default to an array of strings
13
+ end
14
+
15
+ def self.parse(items)
16
+ return nil if items.nil?
17
+
18
+ Swagger::Data::Items.new.bulk_set(items)
19
+ end
20
+
21
+ def type=(new_type)
22
+ raise ArgumentError.new("Swagger::Data::Items#type= - type is nil") unless new_type
23
+ raise ArgumentError.new("Swagger::Data::Items#type= - type #{new_type} is invalid") unless %w(string number integer boolean array).include?(new_type)
24
+
25
+ @type = new_type
26
+ end
27
+
28
+ def items=(new_items)
29
+ raise ArgumentError.new("Swagger::Data::Items#items= - is nil") if new_items.nil? && @type == 'array'
30
+
31
+ if !new_items.nil? && !new_items.is_a?(Swagger::Data::Items)
32
+ new_items = Swagger::Data::Items.parse(new_items)
33
+ end
34
+
35
+ @items = new_items
36
+ end
37
+
38
+ def collectionFormat=(new_collection_format)
39
+ return nil unless new_collection_format
40
+ raise ArgumentError.new("Swagger::Data::Items#collectionFormat= - collectionFormat #{new_collection_format} is invalid") unless %w(csv ssv tsv pipes).include?(new_collection_format)
41
+ @collectionFormat = new_collection_format
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,51 @@
1
+ require 'ruby-swagger/data/url'
2
+ require 'ruby-swagger/object'
3
+
4
+ module Swagger::Data
5
+ class License < Swagger::Object #https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#license-object
6
+
7
+ DEFAULT_NAME = 'Apache 2.0'
8
+ DEFAULT_URL = 'http://www.apache.org/licenses/LICENSE-2.0.html'
9
+
10
+ attr_swagger :name, :url
11
+
12
+ def initialize
13
+ @name = DEFAULT_NAME
14
+ @url = Swagger::Data::Url.new(DEFAULT_URL)
15
+ end
16
+
17
+ def self.parse(license)
18
+ return nil unless license
19
+
20
+ Swagger::Data::License.new.bulk_set(license)
21
+ end
22
+
23
+ def name=(new_name)
24
+ raise (ArgumentError.new("Swagger::Data::License - license name is invalid ")) if new_name.nil? || new_name.empty?
25
+ @name = new_name
26
+ end
27
+
28
+ def url=(url)
29
+ return nil unless url
30
+
31
+ @url = Swagger::Data::Url.new(url)
32
+ validate_url!
33
+ @url
34
+ end
35
+
36
+ def url
37
+ @url.url
38
+ end
39
+
40
+ def valid?
41
+ @url.valid?
42
+ end
43
+
44
+ private
45
+
46
+ def validate_url!
47
+ raise (ArgumentError.new("Swagger::Data::License - contact url is invalid")) unless @url.valid?
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,31 @@
1
+ module Swagger::Data
2
+ class Mime
3
+
4
+ @@types = [
5
+ 'text/plain',
6
+ 'text/plain; charset=utf-8',
7
+ 'application/octet-stream',
8
+ 'application/json',
9
+ 'application/postscript',
10
+ 'application/pdf',
11
+ 'application/postscript',
12
+ 'application/octet-stream',
13
+ 'application/xml',
14
+ 'application/zip',
15
+ 'text/xml',
16
+ 'text/tab-separated-values',
17
+ 'application/vnd.github+json',
18
+ 'application/vnd.github.v3+json',
19
+ 'application/vnd.github.v3.raw+json',
20
+ 'application/vnd.github.v3.text+json',
21
+ 'application/vnd.github.v3.html+json',
22
+ 'application/vnd.github.v3.full+json',
23
+ 'application/vnd.github.v3.diff',
24
+ 'application/vnd.github.v3.patch']
25
+
26
+ def self.valid?(type)
27
+ @@types.include?(type)
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,82 @@
1
+ require 'ruby-swagger/object'
2
+ require 'ruby-swagger/data/external_documentation'
3
+ require 'ruby-swagger/data/responses'
4
+ require 'ruby-swagger/data/security_requirement'
5
+
6
+ module Swagger::Data
7
+ class Operation < Swagger::Object #https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#operationObject
8
+
9
+ attr_swagger :tags, :summary, :description, :externalDocs, :operationId,
10
+ :consumes, :produces, :parameters, :responses,
11
+ :schemes, :deprecated, :security
12
+
13
+ def self.parse(operation)
14
+ return unless operation
15
+
16
+ Swagger::Data::Operation.new.bulk_set(operation)
17
+ end
18
+
19
+ def externalDocs=(newDoc)
20
+ return nil unless newDoc
21
+
22
+ unless newDoc.is_a?(Swagger::Data::ExternalDocumentation)
23
+ newDoc = Swagger::Data::ExternalDocumentation.parse(newDoc)
24
+ end
25
+
26
+ @externalDocs=newDoc
27
+ end
28
+
29
+ def responses=(newResp)
30
+ return nil unless newResp
31
+
32
+ unless newResp.is_a?(Swagger::Data::Responses)
33
+ newResp = Swagger::Data::Responses.parse(newResp)
34
+ end
35
+
36
+ @responses = newResp
37
+ end
38
+
39
+ def security=(newSecurity)
40
+ return nil unless newSecurity
41
+
42
+ @security = []
43
+
44
+ newSecurity.each do |sec_object|
45
+ unless sec_object.is_a?(Swagger::Data::SecurityRequirement)
46
+ sec_object = Swagger::Data::SecurityRequirement.parse(sec_object)
47
+ end
48
+
49
+ @security.push(sec_object)
50
+ end
51
+ end
52
+
53
+ def parameters=(newParams)
54
+ return nil unless newParams
55
+
56
+ @parameters = []
57
+
58
+ newParams.each do |parameter|
59
+ add_parameter(parameter)
60
+ end
61
+ end
62
+
63
+ def add_parameter(new_parameter)
64
+ @parameters ||= []
65
+
66
+ if new_parameter.is_a?(Hash)
67
+
68
+ new_parameter = if new_parameter['$ref']
69
+ #it's a reference object
70
+ Swagger::Data::Reference.parse(new_parameter)
71
+ else
72
+ #it's a parameter object
73
+ Swagger::Data::Parameter.parse(new_parameter)
74
+ end
75
+
76
+ end
77
+
78
+ @parameters.push(new_parameter)
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,88 @@
1
+ require 'ruby-swagger/object'
2
+ require 'ruby-swagger/data/items'
3
+ require 'ruby-swagger/data/schema'
4
+ require 'ruby-swagger/grape/param'
5
+
6
+ module Swagger::Data
7
+ class Parameter < Swagger::Object # https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#parameter-object
8
+
9
+ attr_swagger :name, :in, :description, :required, :schema,
10
+ :type, :format, :allowEmptyValue, :items,
11
+ :collectionFormat, :default, :maximum,
12
+ :exclusiveMaximum, :minimum, :exclusiveMinimum,
13
+ :maxLength, :minLength, :pattern, :maxItems,
14
+ :minItems, :uniqueItems, :enum, :multipleOf
15
+
16
+ def self.parse(new_parameter)
17
+ return nil unless new_parameter
18
+
19
+ p = Swagger::Data::Parameter.new
20
+ %w(name in description required).each do |field|
21
+ p.send("#{field}=", new_parameter[field])
22
+ end
23
+
24
+ if p.in == 'body'
25
+ p.schema = Swagger::Data::Schema.parse(new_parameter['schema'])
26
+ else
27
+ %w(type format allowEmptyValue collectionFormat default maximum exclusiveMaximum minimum exclusiveMinimum maxLength minLength pattern maxItems minItems uniqueItems enum multipleOf).each do |field|
28
+ p.send("#{field}=", new_parameter[field])
29
+ end
30
+ p.items = Swagger::Data::Items.parse(new_parameter['items'])
31
+ end
32
+
33
+ p
34
+ end
35
+
36
+ def name=(new_name)
37
+ raise ArgumentError.new('Swagger::Data::Parameter#name called with nil') if new_name.nil?
38
+ @name = new_name
39
+ end
40
+
41
+ def in=(new_in)
42
+ raise ArgumentError.new('Swagger::Data::Parameter#in= called with nil') if new_in.nil?
43
+ raise ArgumentError.new("Swagger::Data::Parameter#in= called with invalid value #{new_in}") unless %w(query header path formData body).include?(new_in)
44
+
45
+ @in = new_in
46
+ end
47
+
48
+ def items=(new_items)
49
+ raise ArgumentError.new("Swagger::Data::Parameter#items= items is nil") if new_items.nil? && @type == 'array'
50
+ if !new_items.nil? && !new_items.is_a?(Swagger::Data::Items)
51
+ new_items = Swagger::Data::Items.parse(new_items)
52
+ end
53
+
54
+ @items = new_items
55
+ end
56
+
57
+ def self.from_grape(grape_parameter)
58
+ return nil if grape_parameter.nil? || grape_parameter.last[:type].is_a?(Hash) || grape_parameter.last[:type] == 'Hash'
59
+
60
+ grape_type = Swagger::Grape::Param.new(grape_parameter.last).to_swagger
61
+
62
+ parameter = Swagger::Data::Parameter.new
63
+ parameter.name= grape_parameter.first
64
+ parameter.in= 'formData'
65
+ parameter.description = grape_type['description']
66
+ parameter.required = grape_type['required']
67
+ parameter.default = grape_type['default']
68
+ parameter.type = grape_type['type']
69
+ parameter.format = grape_type['format']
70
+
71
+ if parameter.type == 'array'
72
+ items = Swagger::Data::Items.new
73
+ items.type = 'string'
74
+ parameter.items = items
75
+ end
76
+
77
+ parameter.type.nil? ? nil : parameter
78
+ rescue => e
79
+ puts "error processing parameter #{grape_parameter} [#{e}]"
80
+ raise e
81
+ end
82
+
83
+ private
84
+
85
+
86
+
87
+ end
88
+ end
@@ -0,0 +1,53 @@
1
+ require 'ruby-swagger/object'
2
+ require 'ruby-swagger/data/parameter'
3
+
4
+ module Swagger::Data
5
+ class Parameters < Swagger::Object # https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#parametersDefinitionsObject
6
+
7
+ def initialize
8
+ @parameters = {}
9
+ end
10
+
11
+ def self.parse(parameters)
12
+ return nil unless parameters
13
+
14
+ params = Swagger::Data::Parameters.new
15
+
16
+ parameters.each do |pname, pvalue|
17
+ params.add_param(pname, pvalue)
18
+ end
19
+
20
+ params
21
+ end
22
+
23
+ def add_param(pname, pvalue)
24
+ raise ArgumentError.new("Swagger::Data::Parameters#add_param - parameter name is nil") unless pname
25
+ raise ArgumentError.new("Swagger::Data::Parameters#add_param - parameter value is nil") unless pvalue
26
+
27
+ if !pvalue.is_a?(Swagger::Data::Parameter)
28
+ pvalue = Swagger::Data::Parameter.parse(pvalue)
29
+ end
30
+
31
+ @parameters[pname] = pvalue
32
+ end
33
+
34
+ def [](pname)
35
+ @parameters[pname]
36
+ end
37
+
38
+ def params
39
+ @parameters.keys
40
+ end
41
+
42
+ def as_swagger
43
+ swagger_params = {}
44
+
45
+ @parameters.each do |p_k, p_v|
46
+ swagger_params[p_k] = p_v.to_swagger
47
+ end
48
+
49
+ swagger_params
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,115 @@
1
+ require 'ruby-swagger/object'
2
+ require 'ruby-swagger/data/operation'
3
+ require 'ruby-swagger/data/parameter'
4
+ require 'ruby-swagger/data/reference'
5
+
6
+ module Swagger::Data
7
+ class Path < Swagger::Object #https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#path-item-object
8
+
9
+ attr_swagger :get, :put, :post, :delete, :options, :head, :patch, :parameters #and $ref
10
+ @ref = nil
11
+
12
+ def self.parse(path)
13
+ raise (ArgumentError.new("Swagger::Data::Path - path is nil")) unless path
14
+
15
+ res = Swagger::Data::Path.new.bulk_set(path)
16
+ res.ref= path['$ref'] if path['$ref']
17
+ res
18
+ end
19
+
20
+ def get=(new_get)
21
+ return nil unless new_get
22
+ unless new_get.is_a?(Swagger::Data::Operation)
23
+ new_get = Swagger::Data::Operation.parse(new_get)
24
+ end
25
+
26
+ @get = new_get
27
+ end
28
+
29
+ def self.put=(new_put)
30
+ return nil unless new_put
31
+ unless new_put.is_a?(Swagger::Data::Operation)
32
+ new_put = Swagger::Data::Operation.parse(new_put)
33
+ end
34
+ @put = new_put
35
+ end
36
+
37
+ def post=(new_post)
38
+ return nil unless new_post
39
+ unless new_post.is_a?(Swagger::Data::Operation)
40
+ new_post = Swagger::Data::Operation.parse(new_post)
41
+ end
42
+ @post= new_post
43
+ end
44
+
45
+ def delete=(new_delete)
46
+ return nil unless new_delete
47
+ unless new_delete.is_a?(Swagger::Data::Operation)
48
+ new_delete = Swagger::Data::Operation.parse(new_delete)
49
+ end
50
+ @delete= new_delete
51
+ end
52
+
53
+ def options=(new_options)
54
+ return nil unless new_options
55
+ unless new_options.is_a?(Swagger::Data::Operation)
56
+ new_options = Swagger::Data::Operation.parse(new_options)
57
+ end
58
+ @options= new_options
59
+ end
60
+
61
+ def head=(new_head)
62
+ return nil unless new_head
63
+ unless new_head.is_a?(Swagger::Data::Operation)
64
+ new_head = Swagger::Data::Operation.parse(new_head)
65
+ end
66
+
67
+ @head= new_head
68
+ end
69
+
70
+ def patch=(new_patch)
71
+ return nil unless new_patch
72
+ unless new_patch.is_a?(Swagger::Data::Operation)
73
+ new_patch = Swagger::Data::Operation.parse(new_patch)
74
+ end
75
+ @patch= new_patch
76
+ end
77
+
78
+ def parameters=(new_parameters)
79
+ return nil unless new_parameters
80
+ raise (ArgumentError.new("Swagger::Data::Path#parameters= - parameters is not an array")) unless new_parameters.is_a?(Array)
81
+
82
+ @parameters = []
83
+
84
+ new_parameters.each do |parameter|
85
+ new_param = if parameter['$ref']
86
+ #it's a reference object
87
+ Swagger::Data::Reference.parse(parameter)
88
+ else
89
+ #it's a parameter object
90
+ Swagger::Data::Parameter.parse(parameter)
91
+ end
92
+
93
+ @parameters.push(new_param)
94
+ end
95
+
96
+ end
97
+
98
+ def ref=(new_ref)
99
+ return nil unless new_ref
100
+ raise (ArgumentError.new("Swagger::Data::Path#ref= - $ref is not a string")) unless new_ref.is_a?(String)
101
+
102
+ @ref = new_ref
103
+ end
104
+
105
+ def ref
106
+ @ref
107
+ end
108
+
109
+ def as_swagger
110
+ res = super
111
+ res['$ref'] = @ref if @ref
112
+ res
113
+ end
114
+ end
115
+ end