swagger_model 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 51972d3305b7ba48ff8ce0f8ecd1f4b7f8b5f5dc
4
+ data.tar.gz: 77baeb25d4b56948b90a750aead95ca2328fda63
5
+ SHA512:
6
+ metadata.gz: a963e2a9c5e4a1e074a7e16b203dde310cf4cb0cab7f5977daba55761fecb42bb140614a09fbeab9f0cb477cc482c6a7f6d65086ddeded653afa6d0126f8e844
7
+ data.tar.gz: 0166626bdd4e20fbbbb6267d7d1e7553a845be043627f6978ac4a8ee0b79eaaee632fd247703d0e1ae18fab580dc0a5a15d7ff0d5f04410aec3214e75a531171
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in swagger_model.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ swagger_model (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.5.0)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 1.16)
16
+ rake (~> 10.0)
17
+ swagger_model!
18
+
19
+ BUNDLED WITH
20
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 marumemomo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # SwaggerModel
2
+
3
+ Convert json to swagger model yaml
4
+
5
+ Example.json
6
+ ```json
7
+ {
8
+ "user": {
9
+ "name": "marumemomo",
10
+ "age": 24
11
+ },
12
+ "message": "hello",
13
+ "created_at": "2018-05-05T20:02:24.000+09:00",
14
+ "updated_at": null
15
+ }
16
+
17
+ ```
18
+
19
+ to Example_model.yaml
20
+ ```yaml
21
+ ---
22
+ Example:
23
+ type: object
24
+ properties:
25
+ user:
26
+ type: object
27
+ properties:
28
+ name:
29
+ type: string
30
+ example: marumemomo
31
+ age:
32
+ type: integer
33
+ example: 24
34
+ required:
35
+ - name
36
+ - age
37
+ message:
38
+ type: string
39
+ example: hello
40
+ created_at:
41
+ type: string
42
+ example: '2018-05-05T20:02:24.000+09:00'
43
+ format: date-time
44
+ updated_at:
45
+ type: ''
46
+ example: ''
47
+ required:
48
+ - user
49
+ - message
50
+ - created_at
51
+ ```
52
+
53
+ ## Installation
54
+
55
+ Add this line to your application's Gemfile:
56
+
57
+ ```ruby
58
+ gem 'swagger_model'
59
+ ```
60
+
61
+ And then execute:
62
+
63
+ $ bundle
64
+
65
+ Or install it yourself as:
66
+
67
+ $ gem install swagger_model
68
+
69
+ ## Usage
70
+
71
+ ```ruby
72
+ require 'swagger_model'
73
+
74
+ SwaggerModel.create_from_json(json_file_path: 'example/Example.json', output_path: './example/output/')
75
+ ```
76
+
77
+ ## Development
78
+
79
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
80
+
81
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
82
+
83
+ ## Contributing
84
+
85
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marumemomo/swagger_model.
86
+
87
+ ## License
88
+
89
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "swagger_model"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ {
2
+ "user": {
3
+ "name": "marumemomo",
4
+ "age": 24
5
+ },
6
+ "message": "hello",
7
+ "created_at": "2018-05-05T20:02:24.000+09:00",
8
+ "updated_at": null
9
+ }
@@ -0,0 +1,30 @@
1
+ ---
2
+ Example:
3
+ type: object
4
+ properties:
5
+ user:
6
+ type: object
7
+ properties:
8
+ name:
9
+ type: string
10
+ example: marumemomo
11
+ age:
12
+ type: integer
13
+ example: 24
14
+ required:
15
+ - name
16
+ - age
17
+ message:
18
+ type: string
19
+ example: hello
20
+ created_at:
21
+ type: string
22
+ example: '2018-05-05T20:02:24.000+09:00'
23
+ format: date-time
24
+ updated_at:
25
+ type: ''
26
+ example: ''
27
+ required:
28
+ - user
29
+ - message
30
+ - created_at
@@ -0,0 +1,3 @@
1
+ module SwaggerModel
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,97 @@
1
+ require "swagger_model/version"
2
+ require 'json'
3
+ require 'yaml'
4
+ require 'date'
5
+
6
+ module SwaggerModel
7
+ def self.date_time_valid?(str)
8
+ if (!! Date.parse(str) rescue false)
9
+ date = Date._parse(str)
10
+ !date[:zone].nil? && !date[:hour].nil? && !date[:min].nil? && !date[:sec].nil? && !date[:year].nil? && !date[:mon].nil?
11
+ else
12
+ false
13
+ end
14
+ end
15
+
16
+ def self.get_property(value)
17
+ type_class = value.class.to_s
18
+ obj = {}
19
+ case type_class
20
+ when 'Hash'
21
+ obj['type'] = 'object'
22
+ object = parse_object value
23
+ obj['properties'] = object['properties']
24
+ if object['required'].size > 0
25
+ obj['required'] = object['required']
26
+ end
27
+ when 'Array'
28
+ obj['type'] = 'array'
29
+ obj['items'] = parse_array value
30
+ when 'String'
31
+ obj['type'] = 'string'
32
+ obj['example'] = value
33
+ if date_time_valid?(value)
34
+ obj['format'] = 'date-time'
35
+ end
36
+ when 'Fixnum'
37
+ obj['type'] = 'integer'
38
+ obj['example'] = value
39
+ when 'TrueClass', 'FalseClass'
40
+ obj['type'] = 'boolean'
41
+ obj['example'] = value
42
+ when 'Float'
43
+ obj['type'] = 'number'
44
+ obj['format'] = 'float'
45
+ obj['example'] = value
46
+ else
47
+ obj['type'] = ''
48
+ obj['example'] = ''
49
+ end
50
+ obj
51
+ end
52
+
53
+ def self.parse_array items
54
+ m = {}
55
+ value = items.first
56
+ get_property(value)
57
+ end
58
+
59
+ def self.parse_object res
60
+ keys = res.keys
61
+ m = {}
62
+ required = []
63
+ for key in keys do
64
+ value = res[key]
65
+ m[key] = get_property(value)
66
+ if m[key]['type'] != ''
67
+ required.push(key)
68
+ end
69
+ end
70
+ obj = {}
71
+ obj['properties'] = m
72
+ obj['required'] = required
73
+ obj
74
+ end
75
+ def self.create_from_json(params)
76
+ json_file_path = params[:json_file_path] || gets
77
+ json = open(json_file_path) do |io|
78
+ JSON.load(io)
79
+ end
80
+ model_name = params[:model_name] || File.basename(json_file_path, '.json')
81
+ response = json
82
+ model = {}
83
+ model[model_name] = {}
84
+ model[model_name]['type'] = "object"
85
+ object = parse_object response
86
+ model[model_name]['properties'] = object['properties']
87
+ if object['required'].size > 0
88
+ model[model_name]['required'] = object['required']
89
+ end
90
+ output_path = params[:output_path] || './'
91
+ if params[:output_type] == 'json'
92
+ File.write(File.join(output_path, "#{model_name}_model.json"), model.to_json)
93
+ else
94
+ File.write(File.join(output_path, "#{model_name}_model.yaml"), model.to_yaml)
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,35 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "swagger_model/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "swagger_model"
8
+ spec.version = SwaggerModel::VERSION
9
+ spec.authors = ["marumemomo"]
10
+ spec.email = ["marumemomo@gmail.com"]
11
+
12
+ spec.summary = "swagger_model"
13
+ spec.description = "swagger_model"
14
+ spec.homepage = "https://github.com/marumemomo/swagger_model"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.16"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swagger_model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - marumemomo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: swagger_model
42
+ email:
43
+ - marumemomo@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - example/Example.json
57
+ - example/output/Example_model.yaml
58
+ - lib/swagger_model.rb
59
+ - lib/swagger_model/version.rb
60
+ - swagger_model.gemspec
61
+ homepage: https://github.com/marumemomo/swagger_model
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.5.2
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: swagger_model
85
+ test_files: []