yori 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +14 -0
  3. data/.gitignore +11 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +15 -0
  6. data/.ruby-version +1 -0
  7. data/.travis.yml +7 -0
  8. data/CODE_OF_CONDUCT.md +74 -0
  9. data/Gemfile +6 -0
  10. data/Gemfile.lock +35 -0
  11. data/LICENSE.txt +21 -0
  12. data/README.md +44 -0
  13. data/Rakefile +6 -0
  14. data/bin/console +14 -0
  15. data/bin/setup +8 -0
  16. data/lib/yori.rb +16 -0
  17. data/lib/yori/errors/field_must_not_be_specified_error.rb +5 -0
  18. data/lib/yori/errors/invalid_schema_error.rb +5 -0
  19. data/lib/yori/errors/missing_required_field_error.rb +5 -0
  20. data/lib/yori/errors/unknown_component_error.rb +7 -0
  21. data/lib/yori/schema/any.rb +22 -0
  22. data/lib/yori/schema/v3.rb +10 -0
  23. data/lib/yori/schema/v3/callback.rb +15 -0
  24. data/lib/yori/schema/v3/components.rb +70 -0
  25. data/lib/yori/schema/v3/composer.rb +36 -0
  26. data/lib/yori/schema/v3/contact.rb +15 -0
  27. data/lib/yori/schema/v3/discriminator.rb +25 -0
  28. data/lib/yori/schema/v3/encoding.rb +33 -0
  29. data/lib/yori/schema/v3/example.rb +20 -0
  30. data/lib/yori/schema/v3/external_documentation.rb +18 -0
  31. data/lib/yori/schema/v3/header.rb +32 -0
  32. data/lib/yori/schema/v3/info.rb +27 -0
  33. data/lib/yori/schema/v3/license.rb +18 -0
  34. data/lib/yori/schema/v3/link.rb +46 -0
  35. data/lib/yori/schema/v3/media_type.rb +35 -0
  36. data/lib/yori/schema/v3/oauth_flow.rb +35 -0
  37. data/lib/yori/schema/v3/oauth_flows.rb +51 -0
  38. data/lib/yori/schema/v3/openapi.rb +61 -0
  39. data/lib/yori/schema/v3/operation.rb +72 -0
  40. data/lib/yori/schema/v3/parameter.rb +63 -0
  41. data/lib/yori/schema/v3/path_item.rb +47 -0
  42. data/lib/yori/schema/v3/paths.rb +44 -0
  43. data/lib/yori/schema/v3/request_body.rb +27 -0
  44. data/lib/yori/schema/v3/response.rb +34 -0
  45. data/lib/yori/schema/v3/responses.rb +42 -0
  46. data/lib/yori/schema/v3/root.rb +40 -0
  47. data/lib/yori/schema/v3/schema.rb +35 -0
  48. data/lib/yori/schema/v3/security_requirement.rb +23 -0
  49. data/lib/yori/schema/v3/security_scheme.rb +93 -0
  50. data/lib/yori/schema/v3/server.rb +24 -0
  51. data/lib/yori/schema/v3/server_variable.rb +25 -0
  52. data/lib/yori/schema/v3/tag.rb +28 -0
  53. data/lib/yori/schema/v3/xml.rb +28 -0
  54. data/lib/yori/schema_base.rb +110 -0
  55. data/lib/yori/schema_validator.rb +40 -0
  56. data/lib/yori/version.rb +3 -0
  57. data/yori.gemspec +44 -0
  58. metadata +142 -0
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yori/schema/v3/server_variable'
4
+
5
+ module Yori
6
+ module Schema
7
+ module V3
8
+ # Server: An object representing a Server.
9
+ # @url: REQUIRED. A URL to the target host.
10
+ # This URL supports Server Variables and MAY be relative, to indicate that the host location is relative to the location where the OpenAPI document is being served.
11
+ # Variable substitutions will be made when a variable is named in {brackets}.
12
+ # @description: An optional string describing the host designated by the URL. CommonMark syntax MAY be used for rich text representation.
13
+ # @variables: A map between a variable name and its value. The value is used for substitution in the server's URL template.
14
+ class Server < Yori::SchemaBase
15
+ fields :url, :description
16
+ hash_field_block :variables, :variable, Yori::Schema::V3::ServerVariable
17
+
18
+ def validate!
19
+ validate_require_fields!('url')
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yori
4
+ module Schema
5
+ module V3
6
+ # ServerVariable: An object representing a Server Variable for server URL template substitution.
7
+ # @enum: An enumeration of string values to be used if the substitution options are from a limited set.
8
+ # @default: REQUIRED. The default value to use for substitution, and to send, if an alternate value is not supplied.
9
+ # Unlike the Schema Object's default, this value MUST be provided by the consumer.
10
+ # @description: An optional description for the server variable. CommonMark syntax MAY be used for rich text representation.
11
+ class ServerVariable < Yori::SchemaBase
12
+ fields :enum, :default, :description
13
+
14
+ def validate!
15
+ validate_require_fields!('default')
16
+ validate_enum! if key?('enum')
17
+ end
18
+
19
+ def validate_enum!
20
+ validate_field_value_type!('enum', Array)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yori/schema/v3/external_documentation'
4
+
5
+ module Yori
6
+ module Schema
7
+ module V3
8
+ # Tag
9
+ # Adds metadata to a single tag that is used by the Operation Object.
10
+ # It is not mandatory to have a Tag Object per tag defined in the Operation Object instances.
11
+ class Tag < Yori::SchemaBase
12
+ # @!method name
13
+ # REQUIRED. The name of the tag.
14
+ # @!method description
15
+ # A short description for the tag.
16
+ # CommonMark syntax MAY be used for rich text representation.
17
+ fields :name, :description
18
+ # @!method externalDocs
19
+ # Additional external documentation for this tag.
20
+ field_block :externalDocs, Yori::Schema::V3::ExternalDocumentation
21
+
22
+ def validate!
23
+ validate_require_fields!('name')
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yori
4
+ module Schema
5
+ module V3
6
+ # Xml
7
+ # A metadata object that allows for more fine-tuned XML model definitions.
8
+ # When using arrays, XML element names are not inferred (for singular/plural forms) and the name property SHOULD be used to add that information.
9
+ class XML < Yori::SchemaBase
10
+ # @!method name
11
+ # Replaces the name of the element/attribute used for the described schema property.
12
+ # When defined within items, it will affect the name of the individual XML elements within the list.
13
+ # When defined alongside type being array (outside the items), it will affect the wrapping element and only if wrapped is true.
14
+ # If wrapped is false, it will be ignored.
15
+ # @!method namespace
16
+ # The URI of the namespace definition. Value MUST be in the form of an absolute URI.
17
+ # @!method prefix
18
+ # The prefix to be used for the name.
19
+ # @!method attribute
20
+ # Declares whether the property definition translates to an attribute instead of an element. Default value is false.
21
+ # @!method wrapped
22
+ # MAY be used only for an array definition. Signifies whether the array is wrapped (for example, <books><book/><book/></books>) or unwrapped (<book/><book/>).
23
+ # Default value is false. The definition takes effect only when defined alongside type being array (outside the items).
24
+ fields :name, :namespace, :prefix, :attribute, :wrapped
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module Yori
6
+ # Yori::SchemaBase
7
+ class SchemaBase < Hash
8
+ extend Forwardable
9
+
10
+ def_delegators :validator,
11
+ :validate_require_fields!,
12
+ :validate_field_value_type!,
13
+ :validate_limit_field_values!,
14
+ :validate_mutually_exclusive_fields!
15
+
16
+ def validator
17
+ @validator ||= Yori::SchemaValidator.new(self)
18
+ end
19
+
20
+ def validate!; end
21
+
22
+ attr_accessor :id
23
+
24
+ def ref(value)
25
+ self['$ref'] = value
26
+ end
27
+
28
+ class << self
29
+ def eval_input!(klass, id, value = nil, &block)
30
+ return eval_class!(klass, id, &block) unless value
31
+
32
+ case value
33
+ when String, FalseClass
34
+ value # pass as a runtime expression
35
+ when Hash
36
+ eval_hash!(klass, id, value)
37
+ else
38
+ raise 'direct assignment value must be a Hash'
39
+ end
40
+ end
41
+
42
+ def eval_hash!(klass, id, value)
43
+ klass[value].tap do |c|
44
+ raise 'must inherit SchemaBase class' unless c.is_a?(SchemaBase)
45
+ c.id = id
46
+ c.validate!
47
+ end
48
+ end
49
+
50
+ def eval_class!(klass, id, &block)
51
+ klass.new.tap do |c|
52
+ raise 'must inherit SchemaBase class' unless c.is_a?(SchemaBase)
53
+ c.id = id
54
+ c.instance_eval(&block)
55
+ c.validate!
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def fields(*names)
62
+ names.each do |name|
63
+ define_method(name) { |value| self[name.to_s] = value }
64
+ end
65
+ end
66
+
67
+ def field_block(name, schema_class)
68
+ define_method(name) do |value = nil, &block|
69
+ c = self.class.eval_input!(schema_class, id, value, &block)
70
+ self[name.to_s] = c
71
+ end
72
+ end
73
+
74
+ def array_field_block(name, item_name, schema_class)
75
+ define_method(item_name) do |value = nil, &block|
76
+ c = self.class.eval_input!(schema_class, id, value, &block)
77
+ self[name.to_s] ||= []
78
+ self[name.to_s] << c
79
+ end
80
+ define_method(name) do |&block|
81
+ self[name.to_s] = []
82
+ instance_eval(&block)
83
+ end
84
+ end
85
+
86
+ def hash_field(name, key_name)
87
+ define_method(key_name) do |key, value|
88
+ self[name.to_s] ||= {}
89
+ self[name.to_s][key.to_s] = value
90
+ end
91
+ define_method(name) do |&block|
92
+ self[name.to_s] = {}
93
+ instance_eval(&block)
94
+ end
95
+ end
96
+
97
+ def hash_field_block(name, key_name, schema_class)
98
+ define_method(key_name) do |key, value = nil, &block|
99
+ c = self.class.eval_input!(schema_class, id, value, &block)
100
+ self[name.to_s] ||= {}
101
+ self[name.to_s][key.to_s] = c
102
+ end
103
+ define_method(name) do |&block|
104
+ self[name.to_s] = {}
105
+ instance_eval(&block)
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yori
4
+ # SchemaValidator: Common validate methods of Schema.
5
+ class SchemaValidator
6
+ def initialize(schema)
7
+ @schema = schema
8
+ end
9
+
10
+ attr_reader :schema
11
+
12
+ def validate_require_fields!(*fields)
13
+ fields.each do |field|
14
+ next if schema.key?(field)
15
+ raise Yori::Errors::MissingRequiredFieldError, "#{field} of #{simple_class_name} is Required."
16
+ end
17
+ end
18
+
19
+ def validate_field_value_type!(field, klass)
20
+ value = schema[field]
21
+ return if value.nil? || value.is_a?(klass)
22
+ raise Yori::Errors::InvalidSchemaError, "value of #{field} is not #{klass}."
23
+ end
24
+
25
+ def validate_limit_field_values!(field, *values)
26
+ value = schema[field]
27
+ return if values.include?(value)
28
+ raise Yori::Errors::InvalidSchemaError, "Valid values of #{field} are #{values.join(', ')}."
29
+ end
30
+
31
+ def validate_mutually_exclusive_fields!(field1, field2)
32
+ return if schema.key?(field1) ^ schema.key?(field2)
33
+ raise Yori::Errors::InvalidSchemaError, "#{field1} and #{field2} are mutually exclusive."
34
+ end
35
+
36
+ def simple_class_name
37
+ schema.class.name.split('::').last
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module Yori
2
+ VERSION = "0.1.0"
3
+ end
data/yori.gemspec ADDED
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "yori/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "yori"
9
+ spec.version = Yori::VERSION
10
+ spec.authors = ["nakakuki-shingo"]
11
+ spec.email = ["nakakuki.shingo@vareal.co.jp"]
12
+
13
+ spec.summary = "Yet another OpenAPI Ruby Implementation."
14
+ spec.homepage = "https://github.com/yiyenene/yori"
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
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+
22
+ # spec.metadata["homepage_uri"] = spec.homepage
23
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
24
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
25
+ # else
26
+ # raise "RubyGems 2.0 or newer is required to protect against " \
27
+ # "public gem pushes."
28
+ # end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_development_dependency "bundler", "~> 1.16"
40
+ spec.add_development_dependency "rake", "~> 10.0"
41
+ spec.add_development_dependency "rspec", "~> 3.0"
42
+
43
+ spec.required_ruby_version = ">= 2.3.1"
44
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yori
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - nakakuki-shingo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-21 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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - nakakuki.shingo@vareal.co.jp
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".editorconfig"
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".rubocop.yml"
66
+ - ".ruby-version"
67
+ - ".travis.yml"
68
+ - CODE_OF_CONDUCT.md
69
+ - Gemfile
70
+ - Gemfile.lock
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - bin/console
75
+ - bin/setup
76
+ - lib/yori.rb
77
+ - lib/yori/errors/field_must_not_be_specified_error.rb
78
+ - lib/yori/errors/invalid_schema_error.rb
79
+ - lib/yori/errors/missing_required_field_error.rb
80
+ - lib/yori/errors/unknown_component_error.rb
81
+ - lib/yori/schema/any.rb
82
+ - lib/yori/schema/v3.rb
83
+ - lib/yori/schema/v3/callback.rb
84
+ - lib/yori/schema/v3/components.rb
85
+ - lib/yori/schema/v3/composer.rb
86
+ - lib/yori/schema/v3/contact.rb
87
+ - lib/yori/schema/v3/discriminator.rb
88
+ - lib/yori/schema/v3/encoding.rb
89
+ - lib/yori/schema/v3/example.rb
90
+ - lib/yori/schema/v3/external_documentation.rb
91
+ - lib/yori/schema/v3/header.rb
92
+ - lib/yori/schema/v3/info.rb
93
+ - lib/yori/schema/v3/license.rb
94
+ - lib/yori/schema/v3/link.rb
95
+ - lib/yori/schema/v3/media_type.rb
96
+ - lib/yori/schema/v3/oauth_flow.rb
97
+ - lib/yori/schema/v3/oauth_flows.rb
98
+ - lib/yori/schema/v3/openapi.rb
99
+ - lib/yori/schema/v3/operation.rb
100
+ - lib/yori/schema/v3/parameter.rb
101
+ - lib/yori/schema/v3/path_item.rb
102
+ - lib/yori/schema/v3/paths.rb
103
+ - lib/yori/schema/v3/request_body.rb
104
+ - lib/yori/schema/v3/response.rb
105
+ - lib/yori/schema/v3/responses.rb
106
+ - lib/yori/schema/v3/root.rb
107
+ - lib/yori/schema/v3/schema.rb
108
+ - lib/yori/schema/v3/security_requirement.rb
109
+ - lib/yori/schema/v3/security_scheme.rb
110
+ - lib/yori/schema/v3/server.rb
111
+ - lib/yori/schema/v3/server_variable.rb
112
+ - lib/yori/schema/v3/tag.rb
113
+ - lib/yori/schema/v3/xml.rb
114
+ - lib/yori/schema_base.rb
115
+ - lib/yori/schema_validator.rb
116
+ - lib/yori/version.rb
117
+ - yori.gemspec
118
+ homepage: https://github.com/yiyenene/yori
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: 2.3.1
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.5.1
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Yet another OpenAPI Ruby Implementation.
142
+ test_files: []