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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6d94ab24421b81598d6242602e17f2cefaf039d6
4
+ data.tar.gz: a7656dc9e774420f7a063d25cff63715bce4ca42
5
+ SHA512:
6
+ metadata.gz: e2677461caa8dacfcc5d68f4fef90e1dc78ba1188207e462067b078e94a629113d3ad2ff9cdbca8b522b6c11d2591378c607e731b395821930b5fae01b829101
7
+ data.tar.gz: 4dac18bf044a8dcc43caf9a94a5bb5c9e0484428fbf84ebc7172303e185fe9dd1f1dc0b227118a5900e676ac8917bc80813f989d04f2f6d43db8fea800dec021
@@ -0,0 +1,7 @@
1
+ require 'ruby-swagger/data/document'
2
+
3
+ require 'ruby-swagger/railtie' if defined?(Rails)
4
+
5
+ if defined?(Grape)
6
+ require 'ruby-swagger/grape/grape'
7
+ end
@@ -0,0 +1,49 @@
1
+ require 'json'
2
+ require 'yaml'
3
+ require 'ruby-swagger/data/url'
4
+ require 'ruby-swagger/object'
5
+
6
+ module Swagger::Data
7
+ class Contact < Swagger::Object #https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#contactObject
8
+
9
+ DEFAULT_NAME = 'John Doe'
10
+ DEFAULT_EMAIL = 'john.doe@example.com'
11
+ DEFAULT_URL = 'https://google.com/?q=john%20doe'
12
+
13
+ attr_swagger :name, :email, :url
14
+
15
+ def initialize
16
+ @name = DEFAULT_NAME
17
+ @email = DEFAULT_EMAIL
18
+ @url = Swagger::Data::Url.new DEFAULT_URL
19
+ end
20
+
21
+ def self.parse(contact)
22
+ return nil unless contact
23
+
24
+ c = Swagger::Data::Contact.new.bulk_set(contact)
25
+ c.validate_url!
26
+
27
+ c
28
+ end
29
+
30
+ def url=(url)
31
+ return nil unless url
32
+
33
+ @url = Swagger::Data::Url.new(url)
34
+ end
35
+
36
+ def url
37
+ @url.url
38
+ end
39
+
40
+ def valid?
41
+ true
42
+ end
43
+
44
+ def validate_url!
45
+ raise (ArgumentError.new("Swagger::Data::Contact - contact url is invalid")) if @url && !@url.valid?
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,49 @@
1
+ require 'ruby-swagger/object'
2
+ require 'ruby-swagger/data/schema'
3
+
4
+ module Swagger::Data
5
+ class Definitions < Swagger::Object #https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#definitionsObject
6
+
7
+ def initialize
8
+ @definitions = {}
9
+ end
10
+
11
+ def self.parse(definitions)
12
+ return nil unless definitions
13
+
14
+ definition = Swagger::Data::Definitions.new
15
+
16
+ definitions.each do |definition_name, definition_value|
17
+ definition.add_definition(definition_name, definition_value)
18
+ end
19
+
20
+ definition
21
+ end
22
+
23
+ def add_definition(definition_name, definition_value)
24
+ raise ArgumentError.new("Swagger::Data::Definitions#add_definition - definition_name is nil") unless definition_name
25
+ raise ArgumentError.new("Swagger::Data::Definitions#add_definition - definition_value is nil") unless definition_value
26
+
27
+ if !definition_value.is_a?(Swagger::Data::Schema)
28
+ definition_value = Swagger::Data::Schema.parse(definition_value)
29
+ end
30
+
31
+ @definitions[definition_name] = definition_value
32
+ end
33
+
34
+ def [](key)
35
+ @definitions[key]
36
+ end
37
+
38
+ def as_swagger
39
+ swagger_defs = {}
40
+
41
+ @definitions.each do |def_k, def_v|
42
+ swagger_defs[def_k] = def_v.to_swagger
43
+ end
44
+
45
+ swagger_defs
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,181 @@
1
+ require 'json'
2
+ require 'ruby-swagger/object'
3
+ require 'ruby-swagger/data/info'
4
+ require 'ruby-swagger/data/mime'
5
+ require 'ruby-swagger/data/paths'
6
+ require 'ruby-swagger/data/definitions'
7
+ require 'ruby-swagger/data/parameters'
8
+ require 'ruby-swagger/data/responses'
9
+ require 'ruby-swagger/data/security_definitions'
10
+ require 'ruby-swagger/data/security_requirement'
11
+ require 'ruby-swagger/data/tag'
12
+ require 'ruby-swagger/data/external_documentation'
13
+
14
+ module Swagger::Data
15
+ class Document < Swagger::Object #https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#swagger-object
16
+
17
+ SPEC_VERSION = '2.0' #https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#fixed-fields
18
+ DEFAULT_HOST = 'localhost:80'
19
+
20
+ attr_swagger :swagger, :info, :host, :basePath, :schemes, :consumes,
21
+ :produces, :paths, :definitions, :parameters, :responses, :securityDefinitions,
22
+ :security, :tags, :externalDocs
23
+
24
+ # create an empty document
25
+ def initialize
26
+ @swagger = '2.0'
27
+ @info = Swagger::Data::Info.new
28
+ @paths = Swagger::Data::Paths.new
29
+ end
30
+
31
+ # parse an hash document into a set of Swagger objects
32
+ # document is a hash
33
+ def self.parse(document)
34
+ raise (ArgumentError.new("Swagger::Document#parse - document object is nil")) unless document
35
+
36
+ Swagger::Data::Document.new.bulk_set(document)
37
+ end
38
+
39
+ def swagger=(new_swagger)
40
+ raise (ArgumentError.new("Swagger::Document#swagger= - the document is not a swagger #{SPEC_VERSION} version")) unless "2.0" == new_swagger
41
+ @swagger = new_swagger
42
+ end
43
+
44
+ def info=(new_info)
45
+ raise (ArgumentError.new("Swagger::Document#info= - info object is nil")) unless new_info
46
+
47
+ new_info = Swagger::Data::Info.parse(new_info) unless new_info.is_a?(Swagger::Data::Info)
48
+
49
+ @info = new_info
50
+ end
51
+
52
+ def basePath=(new_path)
53
+ new_path = new_path.nil? ? '/' : new_path
54
+
55
+ if !(new_path =~ /^\/.+$/)
56
+ new_path = "/#{new_path}" #new path must start with a /
57
+ end
58
+
59
+ @basePath ||= new_path
60
+ end
61
+
62
+ def schemes=(new_schemes)
63
+ return nil unless new_schemes
64
+
65
+ new_schemes.each do |scheme|
66
+ raise (ArgumentError.new("Swagger::Data::Document#schemes= - unrecognized scheme #{scheme}")) unless %w(http https ws wss).include?(scheme)
67
+ end
68
+
69
+ @schemes = new_schemes
70
+ end
71
+
72
+ def produces=(new_produces)
73
+ return nil unless new_produces
74
+
75
+ new_produces.each do |produce|
76
+ raise (ArgumentError.new("Swagger::Data::Document#produces= - unrecognized produce type #{produce}")) unless Swagger::Data::Mime.valid?(produce)
77
+ end
78
+
79
+ @produces = new_produces
80
+ end
81
+
82
+ def consumes=(new_consumes)
83
+ return nil unless new_consumes
84
+
85
+ new_consumes.each do |consume|
86
+ raise (ArgumentError.new("Swagger::Data::Document#consumes= - unrecognized consume type #{consume}]")) unless Swagger::Data::Mime.valid?(consume)
87
+ end
88
+
89
+ @consumes= new_consumes
90
+ end
91
+
92
+ def paths=(new_paths)
93
+ raise ArgumentError.new("Swagger::Data::Document#paths= - paths is nil") unless paths
94
+
95
+ new_paths = Swagger::Data::Paths.parse(new_paths) if(!new_paths.is_a?(Swagger::Data::Paths))
96
+
97
+ @paths = new_paths
98
+ end
99
+
100
+ def definitions=(new_definitions)
101
+ return nil unless new_definitions
102
+
103
+ if (!new_definitions.is_a?(Swagger::Data::Definitions))
104
+ new_definitions = Swagger::Data::Definitions.parse(new_definitions)
105
+ end
106
+
107
+ @definitions = new_definitions
108
+ end
109
+
110
+ def parameters=(new_parameters)
111
+ return nil unless new_parameters
112
+
113
+ if (!new_parameters.is_a?(Swagger::Data::Parameters))
114
+ new_parameters = Swagger::Data::Parameters.parse(new_parameters)
115
+ end
116
+
117
+ @parameters = new_parameters
118
+ end
119
+
120
+ def responses=(new_responses)
121
+ return nil unless new_responses
122
+
123
+ if (!new_responses.is_a?(Swagger::Data::Responses))
124
+ new_responses = Swagger::Data::Responses.parse(new_responses)
125
+ end
126
+
127
+ @responses = new_responses
128
+ end
129
+
130
+ def securityDefinitions=(newSecurityDef)
131
+ return nil unless newSecurityDef
132
+
133
+ if (!newSecurityDef.is_a?(Swagger::Data::SecurityDefinitions))
134
+ newSecurityDef = Swagger::Data::SecurityDefinitions.parse(newSecurityDef)
135
+ end
136
+
137
+ @securityDefinitions= newSecurityDef
138
+ end
139
+
140
+ def security=(new_security)
141
+ return nil unless new_security
142
+
143
+ if (!new_security.is_a?(Swagger::Data::SecurityRequirement))
144
+ new_security = Swagger::Data::SecurityRequirement.parse(new_security)
145
+ end
146
+
147
+ @security = new_security
148
+ end
149
+
150
+ def tags=(new_tags)
151
+ return nil unless new_tags
152
+
153
+ @tags = []
154
+
155
+ new_tags.each do |tag|
156
+ add_tag(tag)
157
+ end
158
+ end
159
+
160
+ def add_tag(new_tag)
161
+ return nil unless new_tag
162
+
163
+ if (!new_tag.is_a?(Swagger::Data::Tag))
164
+ new_tag = Swagger::Data::Tag.parse(new_tag)
165
+ end
166
+
167
+ @tags.push(new_tag)
168
+ end
169
+
170
+ def externalDocs=(new_externalDocs)
171
+ return nil unless new_externalDocs
172
+
173
+ if (!new_externalDocs.is_a?(Swagger::Data::ExternalDocumentation))
174
+ new_externalDocs = Swagger::Data::ExternalDocumentation.parse(new_externalDocs)
175
+ end
176
+
177
+ @externalDocs = new_externalDocs
178
+ end
179
+
180
+ end
181
+ end
@@ -0,0 +1,29 @@
1
+ require 'ruby-swagger/object'
2
+
3
+ module Swagger::Data
4
+ class Example < Swagger::Object #https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#exampleObject
5
+
6
+ attr_accessor :examples
7
+
8
+ def initialize
9
+ @examples = {}
10
+ end
11
+
12
+ def self.parse(new_examples)
13
+ return nil unless new_examples
14
+
15
+ ex_obj = Swagger::Data::Example.new
16
+
17
+ examples = {}
18
+ new_examples.each {|example_mime, example| examples[example_mime] = example }
19
+ ex_obj.examples = examples
20
+
21
+ ex_obj
22
+ end
23
+
24
+ def as_swagger
25
+ @examples
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,24 @@
1
+ require 'ruby-swagger/object'
2
+
3
+ module Swagger::Data
4
+ class ExternalDocumentation < Swagger::Object #https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#externalDocumentationObject
5
+
6
+ attr_swagger :url, :description
7
+
8
+ def initialize
9
+ @url = "http://localhost"
10
+ end
11
+
12
+ def self.parse(external)
13
+ return nil unless external
14
+
15
+ Swagger::Data::ExternalDocumentation.new.bulk_set(external)
16
+ end
17
+
18
+ def url=(new_url)
19
+ raise (ArgumentError.new("Swagger::Data::ExternalDocumentation#url - url is nil")) unless new_url
20
+ @url = new_url
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,34 @@
1
+ require 'ruby-swagger/object'
2
+ require 'ruby-swagger/data/items'
3
+
4
+ module Swagger::Data
5
+ class Header < Swagger::Object # https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#header-object
6
+
7
+ attr_swagger :description, :type, :format, :items,
8
+ :collectionFormat, :default, :maximum,
9
+ :exclusiveMaximum, :minimum, :exclusiveMinimum,
10
+ :maxLength, :minLength, :pattern, :maxItems,
11
+ :minItems, :uniqueItems, :enum, :multipleOf
12
+
13
+ def self.parse(header)
14
+ return nil unless header
15
+
16
+ Swagger::Data::Header.new.bulk_set(header)
17
+ end
18
+
19
+ def type=(new_type)
20
+ raise ArgumentError.new('Swagger::Data::Header#type called with nil') if new_type.nil?
21
+ @type = new_type
22
+ end
23
+
24
+ def items=(new_items)
25
+ raise ArgumentError.new("Swagger::Data::Header#items= items is nil") if new_items.nil? && @type == 'array'
26
+ if !new_items.nil? && !new_items.is_a?(Swagger::Data::Items)
27
+ new_items = Swagger::Data::Items.parse(new_items)
28
+ end
29
+
30
+ @items = new_items
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,48 @@
1
+ require 'ruby-swagger/object'
2
+ require 'ruby-swagger/data/header'
3
+
4
+ module Swagger::Data
5
+ class Headers < Swagger::Object # https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#headersObject
6
+
7
+ def initialize
8
+ @headers = {}
9
+ end
10
+
11
+ def self.parse(headers)
12
+ return nil unless headers
13
+
14
+ h = Swagger::Data::Headers.new
15
+
16
+ headers.each {|header_key, header_value| h.add_header(header_key, header_value) }
17
+
18
+ h
19
+ end
20
+
21
+ def add_header(header_key, header_value)
22
+ raise ArgumentError.new("Swagger::Data::Headers#add_header - parameter name is nil") unless header_key
23
+ raise ArgumentError.new("Swagger::Data::Headers#add_header - parameter value is nil") unless header_value
24
+
25
+ if !header_value.is_a?(Swagger::Data::Header)
26
+ header_value = Swagger::Data::Header.parse(header_value)
27
+ end
28
+
29
+ @headers[header_key] = header_value
30
+ end
31
+
32
+ def [](key)
33
+ @headers[key]
34
+ end
35
+
36
+ def as_swagger
37
+ res = {}
38
+
39
+ @headers.each do |key, value|
40
+ res[key] = value.to_swagger
41
+ end
42
+
43
+ res
44
+ end
45
+
46
+
47
+ end
48
+ end
@@ -0,0 +1,56 @@
1
+ require 'ruby-swagger/object'
2
+ require 'ruby-swagger/data/contact'
3
+ require 'ruby-swagger/data/license'
4
+
5
+ module Swagger::Data
6
+ class Info < Swagger::Object #https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#info-object
7
+
8
+ DEFAULT_TITLE = 'My uber-duper API'
9
+ DEFAULT_DESCRIPTION = 'My uber-duper API description'
10
+ DEFAULT_VERSION = '0.1'
11
+
12
+ attr_swagger :title, :description, :termsOfService, :contact, :license, :version
13
+
14
+ def initialize
15
+ @title = DEFAULT_TITLE
16
+ @description = DEFAULT_DESCRIPTION
17
+ @version = DEFAULT_VERSION
18
+ end
19
+
20
+ def self.parse(info)
21
+ raise (ArgumentError.new("Swagger::Data::Info#parse - info object is nil")) unless info
22
+
23
+ Swagger::Data::Info.new.bulk_set(info)
24
+ end
25
+
26
+ def title=(new_title)
27
+ raise (ArgumentError.new("Swagger::Data::Info#title= - title is invalid")) if new_title.nil? || new_title.empty?
28
+ @title = new_title
29
+ end
30
+
31
+ def contact=(new_contact)
32
+ return nil unless new_contact
33
+
34
+ if(!new_contact.is_a?(Swagger::Data::Contact))
35
+ new_contact = Swagger::Data::Contact.parse(new_contact)
36
+ end
37
+
38
+ @contact = new_contact
39
+ end
40
+
41
+ def license=(new_license)
42
+ return nil unless new_license
43
+
44
+ if(!new_license.is_a?(Swagger::Data::License))
45
+ new_license = Swagger::Data::License.parse(new_license)
46
+ end
47
+
48
+ @license = new_license
49
+ end
50
+
51
+ def valid?
52
+ @license.valid? && @contact.valid?
53
+ end
54
+
55
+ end
56
+ end