ruby-swagger 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ruby-swagger.rb +1 -5
  3. data/lib/ruby-swagger/data/contact.rb +3 -8
  4. data/lib/ruby-swagger/data/definitions.rb +5 -7
  5. data/lib/ruby-swagger/data/document.rb +22 -24
  6. data/lib/ruby-swagger/data/example.rb +3 -5
  7. data/lib/ruby-swagger/data/external_documentation.rb +4 -6
  8. data/lib/ruby-swagger/data/header.rb +2 -4
  9. data/lib/ruby-swagger/data/headers.rb +5 -8
  10. data/lib/ruby-swagger/data/info.rb +6 -8
  11. data/lib/ruby-swagger/data/items.rb +4 -6
  12. data/lib/ruby-swagger/data/license.rb +4 -6
  13. data/lib/ruby-swagger/data/mime.rb +0 -2
  14. data/lib/ruby-swagger/data/operation.rb +7 -9
  15. data/lib/ruby-swagger/data/parameter.rb +4 -10
  16. data/lib/ruby-swagger/data/parameters.rb +4 -6
  17. data/lib/ruby-swagger/data/path.rb +14 -18
  18. data/lib/ruby-swagger/data/paths.rb +5 -7
  19. data/lib/ruby-swagger/data/reference.rb +4 -8
  20. data/lib/ruby-swagger/data/response.rb +1 -3
  21. data/lib/ruby-swagger/data/responses.rb +1 -3
  22. data/lib/ruby-swagger/data/schema.rb +7 -9
  23. data/lib/ruby-swagger/data/scopes.rb +4 -6
  24. data/lib/ruby-swagger/data/security_definitions.rb +5 -7
  25. data/lib/ruby-swagger/data/security_requirement.rb +6 -8
  26. data/lib/ruby-swagger/data/security_scheme.rb +10 -12
  27. data/lib/ruby-swagger/data/tag.rb +3 -5
  28. data/lib/ruby-swagger/data/url.rb +1 -3
  29. data/lib/ruby-swagger/data/xml_object.rb +2 -4
  30. data/lib/ruby-swagger/grape/entity.rb +41 -0
  31. data/lib/ruby-swagger/grape/entity_exposure.rb +96 -0
  32. data/lib/ruby-swagger/grape/entity_nesting_exposure.rb +38 -0
  33. data/lib/ruby-swagger/grape/grape.rb +1 -1
  34. data/lib/ruby-swagger/grape/grape_config.rb +19 -22
  35. data/lib/ruby-swagger/grape/grape_presenter.rb +6 -9
  36. data/lib/ruby-swagger/grape/grape_template.rb +3 -5
  37. data/lib/ruby-swagger/grape/method.rb +79 -71
  38. data/lib/ruby-swagger/grape/param.rb +3 -3
  39. data/lib/ruby-swagger/grape/route_path.rb +1 -3
  40. data/lib/ruby-swagger/grape/routes.rb +6 -9
  41. data/lib/ruby-swagger/grape/type.rb +46 -119
  42. data/lib/ruby-swagger/io/comparable.rb +13 -16
  43. data/lib/ruby-swagger/io/definitions.rb +5 -11
  44. data/lib/ruby-swagger/io/file_system.rb +8 -9
  45. data/lib/ruby-swagger/io/paths.rb +6 -11
  46. data/lib/ruby-swagger/io/security.rb +3 -6
  47. data/lib/ruby-swagger/object.rb +5 -7
  48. data/lib/ruby-swagger/railtie.rb +1 -1
  49. data/lib/ruby-swagger/template.rb +2 -4
  50. data/lib/tasks/swagger.rake +13 -18
  51. metadata +10 -7
@@ -1,30 +1,27 @@
1
1
  module Swagger::IO
2
2
  class Comparable
3
-
4
3
  def self.copy_description_old_definition(definition, old_definition)
5
4
  return if definition.nil? || old_definition.nil? || definition.class != old_definition.class
6
5
 
7
6
  case definition
8
- when Hash
9
-
10
- definition.keys.each do |key|
11
- old_v = definition[key]
7
+ when Hash
12
8
 
13
- if (key == 'description' || key == 'summary') && old_definition[key]
14
- definition[key] = old_definition[key]
15
- end
9
+ definition.keys.each do |key|
10
+ old_v = definition[key]
16
11
 
17
- if old_v.is_a?(Hash) || old_v.is_a?(Array)
18
- copy_description_old_definition(definition[key], old_definition[key])
19
- end
12
+ if (key == 'description' || key == 'summary') && old_definition[key]
13
+ definition[key] = old_definition[key]
20
14
  end
21
- when Array
22
- definition.each_with_index do |item, index|
23
- copy_description_old_definition(definition[index], old_definition[index])
15
+
16
+ if old_v.is_a?(Hash) || old_v.is_a?(Array)
17
+ copy_description_old_definition(definition[key], old_definition[key])
24
18
  end
19
+ end
20
+ when Array
21
+ definition.each_with_index do |_item, index|
22
+ copy_description_old_definition(definition[index], old_definition[index])
23
+ end
25
24
  end
26
25
  end
27
-
28
26
  end
29
-
30
27
  end
@@ -3,12 +3,11 @@ require 'ruby-swagger/io/comparable'
3
3
 
4
4
  module Swagger::IO
5
5
  class Definitions
6
-
7
6
  def self.read_definitions
8
7
  definitions = {}
9
8
 
10
- Swagger::IO::FileSystem.all_files("definitions/**/*.yml").each do |file|
11
- definitions[File.basename(file, ".yml")] = YAML::load_file(file)
9
+ Swagger::IO::FileSystem.all_files('definitions/**/*.yml').each do |file|
10
+ definitions[File.basename(file, '.yml')] = YAML.load_file(file)
12
11
  end
13
12
 
14
13
  definitions
@@ -17,20 +16,18 @@ module Swagger::IO
17
16
  def self.write_definitions(definitions)
18
17
  return if definitions.nil?
19
18
 
20
- #Remove dead definitions
21
- Swagger::IO::FileSystem.all_files("definitions/**/*.yml").each do |file|
22
- def_name = File.basename(file, ".yml")
19
+ # Remove dead definitions
20
+ Swagger::IO::FileSystem.all_files('definitions/**/*.yml').each do |file|
21
+ def_name = File.basename(file, '.yml')
23
22
 
24
23
  unless definitions[def_name]
25
24
  STDERR.puts "#{def_name} is not present anymore, removing #{file}"
26
25
  Swagger::IO::FileSystem.delete_file(file)
27
26
  end
28
-
29
27
  end
30
28
 
31
29
  # Write new definitions
32
30
  definitions.each do |definition_name, definition|
33
-
34
31
  # If an old definition exists, we copy over the documentation to the generated definition
35
32
  if Swagger::IO::FileSystem.file_exists?("definitions/#{definition_name}.yml")
36
33
  old_definition = Swagger::IO::FileSystem.read_file("definitions/#{definition_name}.yml")
@@ -41,8 +38,5 @@ module Swagger::IO
41
38
  Swagger::IO::FileSystem.write_file(definition.to_yaml, "definitions/#{definition_name}.yml", true)
42
39
  end
43
40
  end
44
-
45
-
46
41
  end
47
-
48
42
  end
@@ -3,10 +3,10 @@ require 'ruby-swagger/data/document'
3
3
  require 'ruby-swagger/io/security'
4
4
  require 'ruby-swagger/io/definitions'
5
5
  require 'ruby-swagger/io/paths'
6
+ require 'erb'
6
7
 
7
8
  module Swagger::IO
8
9
  class FileSystem
9
-
10
10
  DOC_SUBPARTS = %w(responses security tags)
11
11
 
12
12
  @@default_path = './doc/swagger'
@@ -20,7 +20,7 @@ module Swagger::IO
20
20
  end
21
21
 
22
22
  def self.init_fs_structure
23
- FileUtils.mkdir_p(@@default_path) unless Dir.exists?(@@default_path)
23
+ FileUtils.mkdir_p(@@default_path) unless Dir.exist?(@@default_path)
24
24
  end
25
25
 
26
26
  def self.read_file(name)
@@ -30,16 +30,16 @@ module Swagger::IO
30
30
  def self.write_file(content, location, overwrite = false)
31
31
  file_path = @@default_path + '/' + location
32
32
 
33
- return if !overwrite && File.exists?(file_path)
33
+ return if !overwrite && File.exist?(file_path)
34
34
 
35
35
  dir_path = File.dirname(file_path)
36
36
 
37
- FileUtils.mkdir_p(dir_path) unless Dir.exists?(dir_path)
38
- File.open(file_path, 'w') {|f| f.write(content) }
37
+ FileUtils.mkdir_p(dir_path) unless Dir.exist?(dir_path)
38
+ File.open(file_path, 'w') { |f| f.write(content) }
39
39
  end
40
40
 
41
41
  def self.file_exists?(name)
42
- File.exists?(@@default_path + '/' + name)
42
+ File.exist?(@@default_path + '/' + name)
43
43
  end
44
44
 
45
45
  def self.all_files(pattern)
@@ -61,7 +61,7 @@ module Swagger::IO
61
61
 
62
62
  Swagger::IO::Paths.write_paths(swagger.delete('paths'))
63
63
 
64
- DOC_SUBPARTS.each {|doc_part| write_subpart(doc_part, swagger.delete(doc_part))}
64
+ DOC_SUBPARTS.each { |doc_part| write_subpart(doc_part, swagger.delete(doc_part)) }
65
65
  Swagger::IO::Definitions.write_definitions(swagger.delete('definitions'))
66
66
  Swagger::IO::Security.write_security_definitions(swagger.delete('securityDefinitions'))
67
67
  Swagger::IO::FileSystem.write_file(swagger.to_yaml, 'base_doc.yml')
@@ -78,7 +78,7 @@ module Swagger::IO
78
78
  doc['paths'] = Swagger::IO::Paths.read_paths
79
79
  doc['definitions'] = Swagger::IO::Definitions.read_definitions
80
80
  doc['securityDefinitions'] = Swagger::IO::Security.read_security_definitions
81
-
81
+
82
82
  Swagger::Data::Document.parse(doc)
83
83
  end
84
84
 
@@ -92,6 +92,5 @@ module Swagger::IO
92
92
  return unless content
93
93
  write_file(content.to_yaml, "#{subpart}.yml")
94
94
  end
95
-
96
95
  end
97
96
  end
@@ -2,16 +2,15 @@ require 'ruby-swagger/io/file_system'
2
2
 
3
3
  module Swagger::IO
4
4
  class Paths
5
-
6
5
  def self.read_paths
7
6
  paths = {}
8
7
 
9
8
  l = (Swagger::IO::FileSystem.default_path + '/paths').length
10
9
 
11
10
  Swagger::IO::FileSystem.all_files('paths/**/*.yml').each do |file|
12
- content = YAML::load_file(file)
11
+ content = YAML.load_file(file)
13
12
  paths[File.dirname(file[l..file.length])] ||= {}
14
- paths[File.dirname(file[l..file.length])][File.basename(file, ".yml")] = content
13
+ paths[File.dirname(file[l..file.length])][File.basename(file, '.yml')] = content
15
14
  end
16
15
 
17
16
  paths
@@ -22,20 +21,18 @@ module Swagger::IO
22
21
 
23
22
  l = (Swagger::IO::FileSystem.default_path + '/paths').length
24
23
 
25
- #Remove dead endpoints
26
- Swagger::IO::FileSystem.all_files("paths/**/*.yml").each do |file|
24
+ # Remove dead endpoints
25
+ Swagger::IO::FileSystem.all_files('paths/**/*.yml').each do |file|
27
26
  def_name = file[l..file.length]
28
27
 
29
- if paths[File.dirname(file[l..file.length])].nil? || paths[File.dirname(file[l..file.length])][File.basename(file, ".yml")].nil?
28
+ if paths[File.dirname(file[l..file.length])].nil? || paths[File.dirname(file[l..file.length])][File.basename(file, '.yml')].nil?
30
29
  STDERR.puts "#{def_name} is not present anymore, removing #{file}"
31
30
  Swagger::IO::FileSystem.delete_file(file)
32
31
  end
33
-
34
32
  end
35
33
 
36
34
  paths.each do |path, path_obj|
37
35
  path_obj.each do |action, action_obj|
38
-
39
36
  file = "paths/#{path}/#{action}.yml"
40
37
 
41
38
  # If an old definition exists, we copy over the documentation to the generated definition
@@ -45,11 +42,9 @@ module Swagger::IO
45
42
  Swagger::IO::Comparable.copy_description_old_definition(action_obj, old_action)
46
43
  end
47
44
 
48
-
49
45
  Swagger::IO::FileSystem.write_file(action_obj.to_yaml, file, true)
50
46
  end
51
47
  end
52
48
  end
53
-
54
49
  end
55
- end
50
+ end
@@ -2,7 +2,6 @@ require 'ruby-swagger/io/file_system'
2
2
 
3
3
  module Swagger::IO
4
4
  class Security
5
-
6
5
  SECURITY_FILE_NAME = 'securityDefinitions.yml'
7
6
 
8
7
  def self.write_security_definitions(security_definitions)
@@ -19,8 +18,8 @@ module Swagger::IO
19
18
  return nil unless Swagger::IO::FileSystem.file_exists?(SECURITY_FILE_NAME)
20
19
  definitions = Swagger::IO::FileSystem.read_file(SECURITY_FILE_NAME)
21
20
 
22
- Swagger::IO::FileSystem.all_files("/scopes/*.yml").each do |file|
23
- definitions[File.basename(file, ".yml")]['scopes'] = YAML::load_file(file)
21
+ Swagger::IO::FileSystem.all_files('/scopes/*.yml').each do |file|
22
+ definitions[File.basename(file, '.yml')]['scopes'] = YAML.load_file(file)
24
23
  end
25
24
 
26
25
  definitions
@@ -34,12 +33,10 @@ module Swagger::IO
34
33
 
35
34
  return Swagger::IO::FileSystem.write_file(scopes.to_yaml, scope_file) unless Swagger::IO::FileSystem.file_exists?(scope_file)
36
35
 
37
- #Merging old scopes into new scope
36
+ # Merging old scopes into new scope
38
37
  old_scopes = Swagger::IO::FileSystem.read_file(scope_file)
39
38
 
40
39
  Swagger::IO::FileSystem.write_file(scopes.merge(old_scopes).to_yaml, scope_file, true)
41
40
  end
42
-
43
41
  end
44
-
45
42
  end
@@ -1,11 +1,10 @@
1
1
  module Swagger
2
2
  class Object
3
-
4
3
  @@swagger_attribs = {}
5
4
 
6
5
  def self.attr_swagger(*attributes)
7
6
  attr_accessor(*attributes)
8
- @@swagger_attribs[self.to_s] = *attributes
7
+ @@swagger_attribs[to_s] = *attributes
9
8
  end
10
9
 
11
10
  def swagger_attributes
@@ -26,7 +25,7 @@ module Swagger
26
25
 
27
26
  def bulk_set(object)
28
27
  swagger_attributes.each do |attribute|
29
- self.send("#{attribute}=", object[attribute.to_s])
28
+ send("#{attribute}=", object[attribute.to_s])
30
29
  end
31
30
 
32
31
  self
@@ -40,10 +39,10 @@ module Swagger
40
39
  return swagger unless swagger_attributes
41
40
 
42
41
  swagger_attributes.each do |property|
43
- obj = self.send(property)
42
+ obj = send(property)
44
43
  obj = swaggerify(obj)
45
44
 
46
- swagger[property.to_s] = obj if !obj.nil?
45
+ swagger[property.to_s] = obj unless obj.nil?
47
46
  end
48
47
 
49
48
  swagger
@@ -62,6 +61,5 @@ module Swagger
62
61
 
63
62
  object
64
63
  end
65
-
66
64
  end
67
- end
65
+ end
@@ -4,4 +4,4 @@ module Swagger
4
4
  load "#{File.dirname(__FILE__)}/../tasks/swagger.rake"
5
5
  end
6
6
  end
7
- end
7
+ end
@@ -2,7 +2,6 @@ require 'ruby-swagger/data/document'
2
2
 
3
3
  module Swagger
4
4
  class Template
5
-
6
5
  def self.generate
7
6
  swagger_doc = Swagger::Data::Document.new
8
7
 
@@ -14,7 +13,7 @@ module Swagger
14
13
  swagger_doc.host = 'localhost:80'
15
14
  swagger_doc.basePath = '/api/v1'
16
15
 
17
- swagger_doc.schemes = ['https', 'http']
16
+ swagger_doc.schemes = %w(https http)
18
17
  swagger_doc.produces = ['application/json']
19
18
  swagger_doc.consumes = ['application/json']
20
19
 
@@ -24,6 +23,5 @@ module Swagger
24
23
 
25
24
  swagger_doc
26
25
  end
27
-
28
26
  end
29
- end
27
+ end
@@ -2,9 +2,7 @@ require 'ruby-swagger/io/file_system'
2
2
  require 'ruby-swagger/grape/grape_template'
3
3
 
4
4
  namespace :swagger do
5
-
6
5
  namespace :grape do
7
-
8
6
  unless defined?(Rails)
9
7
  task :environment do
10
8
  # for non-rails environment, we do not load the env
@@ -12,11 +10,11 @@ namespace :swagger do
12
10
  end
13
11
 
14
12
  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|
13
+ task :generate_doc, [:base_class] => :environment do |_t, args|
16
14
  if args[:base_class].nil?
17
- STDERR.puts "You need to pass a base class for your API"
15
+ STDERR.puts 'You need to pass a base class for your API'
18
16
  STDERR.puts "For example: rake 'swagger:grape:generate_doc[ApiBase]'"
19
- exit -1
17
+ exit(-1)
20
18
  end
21
19
 
22
20
  puts "Exporting from Grape - base class #{args[:base_class]}"
@@ -28,12 +26,11 @@ namespace :swagger do
28
26
  Swagger::IO::FileSystem.new(swagger_doc).write!
29
27
  puts "You should check your swagger meta documentation under #{Swagger::IO::FileSystem.default_path}"
30
28
  end
31
-
32
29
  end
33
30
 
34
31
  desc 'Generate a swagger 2.0-compatible documentation from the metadata stored into doc/swagger'
35
32
  task :compile_doc do
36
- puts "Compiling documentation"
33
+ puts 'Compiling documentation'
37
34
 
38
35
  Swagger::IO::FileSystem.new(Swagger::IO::FileSystem.read).compile!
39
36
 
@@ -42,23 +39,22 @@ namespace :swagger do
42
39
 
43
40
  desc 'Build an API client given the swagger definition in doc/swagger/swagger.json'
44
41
  namespace :generate_client do
45
-
46
42
  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
43
+ unless File.exist?('./doc/swagger/swagger.json')
44
+ STDERR.puts 'File ./doc/swagger/swagger.json does not exist'
45
+ exit(-1)
50
46
  end
51
47
 
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')
48
+ unless File.exist?('vendor/swagger-codegen-cli.jar')
49
+ STDERR.puts 'Swagger codegen does not exist, downloading it now...'
50
+ Dir.mkdir('./vendor') unless Dir.exist?('./vendor')
55
51
 
56
52
  `wget -O ./vendor/swagger-codegen-cli.jar https://s3-us-west-2.amazonaws.com/tunamelt-production/swagger-codegen/swagger-codegen-cli.jar`
57
53
  end
58
54
 
59
55
  puts "Generating #{language} client (output in api_client/#{language})"
60
56
 
61
- FileUtils.rm_rf("./api_client/#{language}") if Dir.exists?("./api_client/#{language}")
57
+ FileUtils.rm_rf("./api_client/#{language}") if Dir.exist?("./api_client/#{language}")
62
58
 
63
59
  `java -jar ./vendor/swagger-codegen-cli.jar generate \
64
60
  -i ./doc/swagger/swagger.json \
@@ -117,9 +113,8 @@ namespace :swagger do
117
113
  end
118
114
 
119
115
  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]
116
+ task all: [:ruby, :java, :python, :python_3, :php, :perl, :objective_c, :c_sharp, :android]
123
117
 
118
+ task default: [:all]
124
119
  end
125
120
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-swagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Bonmassar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-14 00:00:00.000000000 Z
11
+ date: 2016-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -70,16 +70,16 @@ dependencies:
70
70
  name: grape-entity
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: '0.5'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: '0.5'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rack-test
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -131,6 +131,9 @@ files:
131
131
  - lib/ruby-swagger/data/tag.rb
132
132
  - lib/ruby-swagger/data/url.rb
133
133
  - lib/ruby-swagger/data/xml_object.rb
134
+ - lib/ruby-swagger/grape/entity.rb
135
+ - lib/ruby-swagger/grape/entity_exposure.rb
136
+ - lib/ruby-swagger/grape/entity_nesting_exposure.rb
134
137
  - lib/ruby-swagger/grape/grape.rb
135
138
  - lib/ruby-swagger/grape/grape_config.rb
136
139
  - lib/ruby-swagger/grape/grape_presenter.rb
@@ -169,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
172
  version: '0'
170
173
  requirements: []
171
174
  rubyforge_project:
172
- rubygems_version: 2.2.2
175
+ rubygems_version: 2.4.5.1
173
176
  signing_key:
174
177
  specification_version: 4
175
178
  summary: A ruby DSL to read/write Swagger API documents