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,55 @@
1
+ require 'ruby-swagger/io/file_system'
2
+
3
+ module Swagger::IO
4
+ class Paths
5
+
6
+ def self.read_paths
7
+ paths = {}
8
+
9
+ l = (Swagger::IO::FileSystem.default_path + '/paths').length
10
+
11
+ Swagger::IO::FileSystem.all_files('paths/**/*.yml').each do |file|
12
+ content = YAML::load_file(file)
13
+ paths[File.dirname(file[l..file.length])] ||= {}
14
+ paths[File.dirname(file[l..file.length])][File.basename(file, ".yml")] = content
15
+ end
16
+
17
+ paths
18
+ end
19
+
20
+ def self.write_paths(paths)
21
+ return if paths.nil?
22
+
23
+ l = (Swagger::IO::FileSystem.default_path + '/paths').length
24
+
25
+ #Remove dead endpoints
26
+ Swagger::IO::FileSystem.all_files("paths/**/*.yml").each do |file|
27
+ def_name = file[l..file.length]
28
+
29
+ if paths[File.dirname(file[l..file.length])].nil? || paths[File.dirname(file[l..file.length])][File.basename(file, ".yml")].nil?
30
+ STDERR.puts "#{def_name} is not present anymore, removing #{file}"
31
+ Swagger::IO::FileSystem.delete_file(file)
32
+ end
33
+
34
+ end
35
+
36
+ paths.each do |path, path_obj|
37
+ path_obj.each do |action, action_obj|
38
+
39
+ file = "paths/#{path}/#{action}.yml"
40
+
41
+ # If an old definition exists, we copy over the documentation to the generated definition
42
+ if Swagger::IO::FileSystem.file_exists?(file)
43
+ old_action = Swagger::IO::FileSystem.read_file(file)
44
+
45
+ Swagger::IO::Comparable.copy_description_old_definition(action_obj, old_action)
46
+ end
47
+
48
+
49
+ Swagger::IO::FileSystem.write_file(action_obj.to_yaml, file, true)
50
+ end
51
+ end
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,45 @@
1
+ require 'ruby-swagger/io/file_system'
2
+
3
+ module Swagger::IO
4
+ class Security
5
+
6
+ SECURITY_FILE_NAME = 'securityDefinitions.yml'
7
+
8
+ def self.write_security_definitions(security_definitions)
9
+ return if security_definitions.nil? || security_definitions.empty?
10
+
11
+ security_definitions.each do |definition_name, definition|
12
+ Swagger::IO::Security.write_scopes(definition_name, definition)
13
+ end
14
+
15
+ Swagger::IO::FileSystem.write_file(security_definitions.to_yaml, SECURITY_FILE_NAME)
16
+ end
17
+
18
+ def self.read_security_definitions
19
+ return nil unless Swagger::IO::FileSystem.file_exists?(SECURITY_FILE_NAME)
20
+ definitions = Swagger::IO::FileSystem.read_file(SECURITY_FILE_NAME)
21
+
22
+ Swagger::IO::FileSystem.all_files("/scopes/*.yml").each do |file|
23
+ definitions[File.basename(file, ".yml")]['scopes'] = YAML::load_file(file)
24
+ end
25
+
26
+ definitions
27
+ end
28
+
29
+ def self.write_scopes(definition_name, definition)
30
+ return unless definition && definition['scopes']
31
+
32
+ scopes = definition.delete('scopes') || {}
33
+ scope_file = "scopes/#{definition_name}.yml"
34
+
35
+ return Swagger::IO::FileSystem.write_file(scopes.to_yaml, scope_file) unless Swagger::IO::FileSystem.file_exists?(scope_file)
36
+
37
+ #Merging old scopes into new scope
38
+ old_scopes = Swagger::IO::FileSystem.read_file(scope_file)
39
+
40
+ Swagger::IO::FileSystem.write_file(scopes.merge(old_scopes).to_yaml, scope_file, true)
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,67 @@
1
+ module Swagger
2
+ class Object
3
+
4
+ @@swagger_attribs = {}
5
+
6
+ def self.attr_swagger(*attributes)
7
+ attr_accessor(*attributes)
8
+ @@swagger_attribs[self.to_s] = *attributes
9
+ end
10
+
11
+ def swagger_attributes
12
+ @@swagger_attribs[self.class.to_s]
13
+ end
14
+
15
+ def to_json(options = nil)
16
+ to_swagger.to_json(options)
17
+ end
18
+
19
+ def to_yaml
20
+ to_swagger.to_yaml
21
+ end
22
+
23
+ def to_swagger
24
+ as_swagger
25
+ end
26
+
27
+ def bulk_set(object)
28
+ swagger_attributes.each do |attribute|
29
+ self.send("#{attribute}=", object[attribute.to_s])
30
+ end
31
+
32
+ self
33
+ end
34
+
35
+ protected
36
+
37
+ def as_swagger
38
+ swagger = {}
39
+
40
+ return swagger unless swagger_attributes
41
+
42
+ swagger_attributes.each do |property|
43
+ obj = self.send(property)
44
+ obj = swaggerify(obj)
45
+
46
+ swagger[property.to_s] = obj if !obj.nil?
47
+ end
48
+
49
+ swagger
50
+ end
51
+
52
+ def swaggerify(object)
53
+ return nil if object.nil?
54
+
55
+ return object.to_swagger if object.respond_to?(:to_swagger)
56
+
57
+ if object.is_a?(Array)
58
+ object.map! do |element|
59
+ swaggerify(element)
60
+ end
61
+ end
62
+
63
+ object
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,7 @@
1
+ module Swagger
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ load "#{File.dirname(__FILE__)}/../tasks/swagger.rake"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ require 'ruby-swagger/data/document'
2
+
3
+ module Swagger
4
+ class Template
5
+
6
+ def self.generate
7
+ swagger_doc = Swagger::Data::Document.new
8
+
9
+ if defined?(Rails)
10
+ swagger_doc.info.title = Rails.application.class.name.split('::').first.underscore
11
+ swagger_doc.info.description = Rails.application.class.name.split('::').first.underscore
12
+ end
13
+
14
+ swagger_doc.host = 'localhost:80'
15
+ swagger_doc.basePath = '/api/v1'
16
+
17
+ swagger_doc.schemes = ['https', 'http']
18
+ swagger_doc.produces = ['application/json']
19
+ swagger_doc.consumes = ['application/json']
20
+
21
+ swagger_doc.info.contact = Swagger::Data::Contact.new
22
+ swagger_doc.info.license = Swagger::Data::License.new
23
+ swagger_doc.info.termsOfService = 'https://localhost/tos.html'
24
+
25
+ swagger_doc
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,125 @@
1
+ require 'ruby-swagger/io/file_system'
2
+ require 'ruby-swagger/grape/grape_template'
3
+
4
+ namespace :swagger do
5
+
6
+ namespace :grape do
7
+
8
+ unless defined?(Rails)
9
+ task :environment do
10
+ # for non-rails environment, we do not load the env
11
+ end
12
+ end
13
+
14
+ desc 'Generate a swagger meta documentation from Grape API definition and store it under doc/swagger'
15
+ task :generate_doc, [:base_class] => :environment do |t, args|
16
+ if args[:base_class].nil?
17
+ STDERR.puts "You need to pass a base class for your API"
18
+ STDERR.puts "For example: rake 'swagger:grape:generate_doc[ApiBase]'"
19
+ exit -1
20
+ end
21
+
22
+ puts "Exporting from Grape - base class #{args[:base_class]}"
23
+
24
+ swagger_doc = Swagger::Grape::Template.generate(Module.const_get(args[:base_class]))
25
+
26
+ # Get path data from Grape
27
+
28
+ Swagger::IO::FileSystem.new(swagger_doc).write!
29
+ puts "You should check your swagger meta documentation under #{Swagger::IO::FileSystem.default_path}"
30
+ end
31
+
32
+ end
33
+
34
+ desc 'Generate a swagger 2.0-compatible documentation from the metadata stored into doc/swagger'
35
+ task :compile_doc do
36
+ puts "Compiling documentation"
37
+
38
+ Swagger::IO::FileSystem.new(Swagger::IO::FileSystem.read).compile!
39
+
40
+ puts "Done. Your documentation file is #{Swagger::IO::FileSystem.default_path}/swagger.json"
41
+ end
42
+
43
+ desc 'Build an API client given the swagger definition in doc/swagger/swagger.json'
44
+ namespace :generate_client do
45
+
46
+ def build_client(language)
47
+ unless File.exists?('./doc/swagger/swagger.json')
48
+ STDERR.puts "File ./doc/swagger/swagger.json does not exist"
49
+ exit -1
50
+ end
51
+
52
+ unless File.exists?('vendor/swagger-codegen-cli.jar')
53
+ STDERR.puts "Swagger codegen does not exist, downloading it now..."
54
+ Dir.mkdir('./vendor') unless Dir.exists?('./vendor')
55
+
56
+ `wget -O ./vendor/swagger-codegen-cli.jar https://s3-us-west-2.amazonaws.com/tunamelt-production/swagger-codegen/swagger-codegen-cli.jar`
57
+ end
58
+
59
+ puts "Generating #{language} client (output in api_client/#{language})"
60
+
61
+ FileUtils.rm_rf("./api_client/#{language}") if Dir.exists?("./api_client/#{language}")
62
+
63
+ `java -jar ./vendor/swagger-codegen-cli.jar generate \
64
+ -i ./doc/swagger/swagger.json \
65
+ -l #{language} \
66
+ -o api_client/#{language}`
67
+ end
68
+
69
+ desc 'Build the Ruby API client given the swagger definition in doc/swagger/swagger.json'
70
+ task :ruby do
71
+ build_client 'ruby'
72
+ end
73
+
74
+ desc 'Build the Java API client given the swagger definition in doc/swagger/swagger.json'
75
+ task :java do
76
+ build_client 'java'
77
+ end
78
+
79
+ desc 'Build the Scala API client given the swagger definition in doc/swagger/swagger.json'
80
+ task :scala do
81
+ build_client 'scala'
82
+ end
83
+
84
+ desc 'Build the Python API client given the swagger definition in doc/swagger/swagger.json'
85
+ task :python do
86
+ build_client 'python'
87
+ end
88
+
89
+ desc 'Build the Python 3 API client given the swagger definition in doc/swagger/swagger.json'
90
+ task :python_3 do
91
+ build_client 'python'
92
+ end
93
+
94
+ desc 'Build the PHP API client given the swagger definition in doc/swagger/swagger.json'
95
+ task :php do
96
+ build_client 'php'
97
+ end
98
+
99
+ desc 'Build the Perl API client given the swagger definition in doc/swagger/swagger.json'
100
+ task :perl do
101
+ build_client 'perl'
102
+ end
103
+
104
+ desc 'Build the ObjectiveC API client given the swagger definition in doc/swagger/swagger.json'
105
+ task :objective_c do
106
+ build_client 'objc'
107
+ end
108
+
109
+ desc 'Build the C# API client given the swagger definition in doc/swagger/swagger.json'
110
+ task :c_sharp do
111
+ build_client 'csharp'
112
+ end
113
+
114
+ desc 'Build the Android API client given the swagger definition in doc/swagger/swagger.json'
115
+ task :android do
116
+ build_client 'android'
117
+ end
118
+
119
+ desc 'Build all the API clients'
120
+ task :all => [:ruby, :java, :python, :python_3, :php, :perl, :objective_c, :c_sharp, :android]
121
+
122
+ task :default => [:all]
123
+
124
+ end
125
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-swagger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Luca Bonmassar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: addressable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: grape
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: grape-entity
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack-test
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: |-
98
+ A super simple library to read or create (Swagger)[http://swagger.io/] API documents.
99
+ This is the engine used in other gems to translate API definitions (grape, rails) into Swagger definitions.
100
+ email:
101
+ - luca@gild.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - lib/ruby-swagger.rb
107
+ - lib/ruby-swagger/data/contact.rb
108
+ - lib/ruby-swagger/data/definitions.rb
109
+ - lib/ruby-swagger/data/document.rb
110
+ - lib/ruby-swagger/data/example.rb
111
+ - lib/ruby-swagger/data/external_documentation.rb
112
+ - lib/ruby-swagger/data/header.rb
113
+ - lib/ruby-swagger/data/headers.rb
114
+ - lib/ruby-swagger/data/info.rb
115
+ - lib/ruby-swagger/data/items.rb
116
+ - lib/ruby-swagger/data/license.rb
117
+ - lib/ruby-swagger/data/mime.rb
118
+ - lib/ruby-swagger/data/operation.rb
119
+ - lib/ruby-swagger/data/parameter.rb
120
+ - lib/ruby-swagger/data/parameters.rb
121
+ - lib/ruby-swagger/data/path.rb
122
+ - lib/ruby-swagger/data/paths.rb
123
+ - lib/ruby-swagger/data/reference.rb
124
+ - lib/ruby-swagger/data/response.rb
125
+ - lib/ruby-swagger/data/responses.rb
126
+ - lib/ruby-swagger/data/schema.rb
127
+ - lib/ruby-swagger/data/scopes.rb
128
+ - lib/ruby-swagger/data/security_definitions.rb
129
+ - lib/ruby-swagger/data/security_requirement.rb
130
+ - lib/ruby-swagger/data/security_scheme.rb
131
+ - lib/ruby-swagger/data/tag.rb
132
+ - lib/ruby-swagger/data/url.rb
133
+ - lib/ruby-swagger/data/xml_object.rb
134
+ - lib/ruby-swagger/grape/grape.rb
135
+ - lib/ruby-swagger/grape/grape_config.rb
136
+ - lib/ruby-swagger/grape/grape_presenter.rb
137
+ - lib/ruby-swagger/grape/grape_template.rb
138
+ - lib/ruby-swagger/grape/method.rb
139
+ - lib/ruby-swagger/grape/param.rb
140
+ - lib/ruby-swagger/grape/route_path.rb
141
+ - lib/ruby-swagger/grape/routes.rb
142
+ - lib/ruby-swagger/grape/type.rb
143
+ - lib/ruby-swagger/io/comparable.rb
144
+ - lib/ruby-swagger/io/definitions.rb
145
+ - lib/ruby-swagger/io/file_system.rb
146
+ - lib/ruby-swagger/io/paths.rb
147
+ - lib/ruby-swagger/io/security.rb
148
+ - lib/ruby-swagger/object.rb
149
+ - lib/ruby-swagger/railtie.rb
150
+ - lib/ruby-swagger/template.rb
151
+ - lib/tasks/swagger.rake
152
+ homepage: https://github.com/gild/ruby-swagger
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 2.2.2
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: A ruby DSL to read/write Swagger API documents
176
+ test_files: []