oas_parser 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b721f4ef0bb08e47e1dc472ec13d86c5fff894c1
4
+ data.tar.gz: 59925a5cf1a381b9a5b5e8841e1e505ef60b6546
5
+ SHA512:
6
+ metadata.gz: bbe143f53fffd4e9f945343217c5900fe8380d7b5fd5301a9554a755dfae14bfc722485d6daea0d2f3cab60f458409facf990a07091856af424a841806adf153
7
+ data.tar.gz: be8a889667e5c79c9f7f836423af1d930131c3f140066f2bb5e3e9a3e584a08f0ecc5c1bac0f3e1727e869cb5636199b8ab8cf2ab9d74b3d07ecd0c08e2c69ad
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.16.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at adam@lab.io. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
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 oas_parser.gemspec
6
+ gemspec
@@ -0,0 +1,89 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ oas_parser (0.1.0)
5
+ addressable (~> 2.3)
6
+ deep_merge (~> 1.2.1)
7
+ guard-rspec (~> 4.7.3)
8
+ json_schema (~> 0.15.0)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ addressable (2.5.2)
14
+ public_suffix (>= 2.0.2, < 4.0)
15
+ coderay (1.1.2)
16
+ deep_merge (1.2.1)
17
+ diff-lcs (1.3)
18
+ docile (1.1.5)
19
+ ffi (1.9.18)
20
+ formatador (0.2.5)
21
+ guard (2.14.1)
22
+ formatador (>= 0.2.4)
23
+ listen (>= 2.7, < 4.0)
24
+ lumberjack (~> 1.0)
25
+ nenv (~> 0.1)
26
+ notiffany (~> 0.0)
27
+ pry (>= 0.9.12)
28
+ shellany (~> 0.0)
29
+ thor (>= 0.18.1)
30
+ guard-compat (1.2.1)
31
+ guard-rspec (4.7.3)
32
+ guard (~> 2.1)
33
+ guard-compat (~> 1.1)
34
+ rspec (>= 2.99.0, < 4.0)
35
+ json (2.1.0)
36
+ json_schema (0.15.0)
37
+ listen (3.1.5)
38
+ rb-fsevent (~> 0.9, >= 0.9.4)
39
+ rb-inotify (~> 0.9, >= 0.9.7)
40
+ ruby_dep (~> 1.2)
41
+ lumberjack (1.0.12)
42
+ method_source (0.9.0)
43
+ nenv (0.3.0)
44
+ notiffany (0.1.1)
45
+ nenv (~> 0.1)
46
+ shellany (~> 0.0)
47
+ pry (0.11.3)
48
+ coderay (~> 1.1.0)
49
+ method_source (~> 0.9.0)
50
+ public_suffix (3.0.1)
51
+ rake (10.5.0)
52
+ rb-fsevent (0.10.2)
53
+ rb-inotify (0.9.10)
54
+ ffi (>= 0.5.0, < 2)
55
+ rspec (3.7.0)
56
+ rspec-core (~> 3.7.0)
57
+ rspec-expectations (~> 3.7.0)
58
+ rspec-mocks (~> 3.7.0)
59
+ rspec-core (3.7.0)
60
+ rspec-support (~> 3.7.0)
61
+ rspec-expectations (3.7.0)
62
+ diff-lcs (>= 1.2.0, < 2.0)
63
+ rspec-support (~> 3.7.0)
64
+ rspec-mocks (3.7.0)
65
+ diff-lcs (>= 1.2.0, < 2.0)
66
+ rspec-support (~> 3.7.0)
67
+ rspec-support (3.7.0)
68
+ ruby_dep (1.5.0)
69
+ shellany (0.0.1)
70
+ simplecov (0.15.1)
71
+ docile (~> 1.1.0)
72
+ json (>= 1.8, < 3)
73
+ simplecov-html (~> 0.10.0)
74
+ simplecov-html (0.10.2)
75
+ thor (0.20.0)
76
+
77
+ PLATFORMS
78
+ ruby
79
+
80
+ DEPENDENCIES
81
+ bundler (~> 1.16)
82
+ oas_parser!
83
+ pry (~> 0.11.3)
84
+ rake (~> 10.0)
85
+ rspec (~> 3.0)
86
+ simplecov (~> 0.15.1)
87
+
88
+ BUNDLED WITH
89
+ 1.16.0
@@ -0,0 +1,70 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ # Note: The cmd option is now required due to the increasing number of ways
19
+ # rspec may be run, below are examples of the most common uses.
20
+ # * bundler: 'bundle exec rspec'
21
+ # * bundler binstubs: 'bin/rspec'
22
+ # * spring: 'bin/rspec' (This will use spring if running and you have
23
+ # installed the spring binstubs per the docs)
24
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
25
+ # * 'just' rspec: 'rspec'
26
+
27
+ guard :rspec, cmd: "bundle exec rspec" do
28
+ require "guard/rspec/dsl"
29
+ dsl = Guard::RSpec::Dsl.new(self)
30
+
31
+ # Feel free to open issues for suggestions and improvements
32
+
33
+ # RSpec files
34
+ rspec = dsl.rspec
35
+ watch(rspec.spec_helper) { rspec.spec_dir }
36
+ watch(rspec.spec_support) { rspec.spec_dir }
37
+ watch(rspec.spec_files)
38
+
39
+ # Ruby files
40
+ ruby = dsl.ruby
41
+ dsl.watch_spec_files_for(ruby.lib_files)
42
+
43
+ # Rails files
44
+ rails = dsl.rails(view_extensions: %w(erb haml slim))
45
+ dsl.watch_spec_files_for(rails.app_files)
46
+ dsl.watch_spec_files_for(rails.views)
47
+
48
+ watch(rails.controllers) do |m|
49
+ [
50
+ rspec.spec.call("routing/#{m[1]}_routing"),
51
+ rspec.spec.call("controllers/#{m[1]}_controller"),
52
+ rspec.spec.call("acceptance/#{m[1]}")
53
+ ]
54
+ end
55
+
56
+ # Rails config changes
57
+ watch(rails.spec_helper) { rspec.spec_dir }
58
+ watch(rails.routes) { "#{rspec.spec_dir}/routing" }
59
+ watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
60
+
61
+ # Capybara features specs
62
+ watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
63
+ watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
64
+
65
+ # Turnip features and steps
66
+ watch(%r{^spec/acceptance/(.+)\.feature$})
67
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
68
+ Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
69
+ end
70
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Adam Butler
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.
@@ -0,0 +1,43 @@
1
+ # OasParser
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/oas_parser`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'oas_parser'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install oas_parser
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/oas_parser. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the OasParser project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/oas_parser/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "oas_parser"
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__)
@@ -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,19 @@
1
+ require 'json'
2
+ require 'uri'
3
+ require 'yaml'
4
+
5
+ require 'addressable/uri'
6
+ require 'json_schema'
7
+ require 'deep_merge'
8
+
9
+ require_relative 'oas_parser/raw_accessor.rb'
10
+
11
+ Dir["#{File.dirname(__FILE__)}/oas_parser/**/*.rb"].each do |file|
12
+ require file
13
+ end
14
+
15
+ File.expand_path('path', File.dirname(__FILE__))
16
+
17
+ module OasParser
18
+ # Your code goes here...
19
+ end
@@ -0,0 +1,20 @@
1
+ module OasParser
2
+ class Callback < Payload
3
+ include OasParser::RawAccessor
4
+ raw_keys :description, :content
5
+
6
+ attr_accessor :endpoint, :name, :raw
7
+
8
+ def initialize(endpoint, name, raw)
9
+ @endpoint = endpoint
10
+ @name = name
11
+ @raw = raw
12
+ end
13
+
14
+ def paths
15
+ raw.map do |path, definition|
16
+ OasParser::Path.new(self, path, definition)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,37 @@
1
+ module OasParser
2
+ class Definition
3
+ include OasParser::RawAccessor
4
+ raw_keys :info, :servers, :components, :openapi
5
+
6
+ attr_accessor :raw
7
+
8
+ def self.resolve(path)
9
+ raw = Parser.resolve(path)
10
+ Definition.new(raw)
11
+ end
12
+
13
+ def initialize(raw)
14
+ @raw = raw
15
+ end
16
+
17
+ def paths
18
+ raw['paths'].map do |path, definition|
19
+ OasParser::Path.new(self, path, definition)
20
+ end
21
+ end
22
+
23
+ def path_by_path(path)
24
+ definition = raw['paths'][path]
25
+ raise StandardError.new('So such path exists') unless definition
26
+ OasParser::Path.new(self, path, definition)
27
+ end
28
+
29
+ def security
30
+ raw['security'] || []
31
+ end
32
+
33
+ def endpoints
34
+ paths.flat_map(&:endpoints)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,99 @@
1
+ module OasParser
2
+ class Endpoint
3
+ include OasParser::RawAccessor
4
+ raw_keys :summary, :description, :operationId, :tags, :required
5
+
6
+ attr_accessor :path, :method, :raw
7
+
8
+ def initialize(path, method, raw)
9
+ @path = path
10
+ @method = method
11
+ @raw = raw
12
+ end
13
+
14
+ def definition
15
+ path.definition
16
+ end
17
+
18
+ def parameters
19
+ local_parameters + path.parameters
20
+ end
21
+
22
+ def path_parameters
23
+ parameters.select { |parameter| parameter.in == 'path' }
24
+ end
25
+
26
+ def query_parameters
27
+ parameters.select { |parameter| parameter.in == 'query' }
28
+ end
29
+
30
+ def parameter_by_name(name)
31
+ parameters.each do |parameter|
32
+ return parameter if parameter.name == name
33
+ end
34
+
35
+ raise StandardError.new('So such parameter exists')
36
+ end
37
+
38
+ def request_body
39
+ return false unless raw['requestBody']
40
+ OasParser::RequestBody.new(self, raw['requestBody'])
41
+ end
42
+
43
+ def responses
44
+ raw['responses'].map do |code, definition|
45
+ OasParser::Response.new(self, code, definition)
46
+ end
47
+ end
48
+
49
+ def response_by_code(code)
50
+ definition = raw['responses'][code]
51
+ raise StandardError.new('So such response exists') unless definition
52
+ OasParser::Response.new(self, code, definition)
53
+ end
54
+
55
+ def security
56
+ raw['security'] || []
57
+ end
58
+
59
+ def callbacks
60
+ return [] unless raw['callbacks']
61
+ raw['callbacks'].map do |name, definition|
62
+ OasParser::Callback.new(self, name, definition)
63
+ end
64
+ end
65
+
66
+ def callback_by_name(name)
67
+ definition = raw['callbacks'][name]
68
+ raise StandardError.new('So such callback exists') unless definition
69
+ OasParser::Callback.new(self, name, definition)
70
+ end
71
+
72
+ def jwt?
73
+ return false unless security
74
+
75
+ security_schemes.each do |security_schema|
76
+ return true if security_schema['bearerFormat'] == 'JWT'
77
+ end
78
+
79
+ false
80
+ end
81
+
82
+ def security_schemes
83
+ security_schemes = (security.flat_map(&:keys) + definition.security.flat_map(&:keys)).uniq
84
+
85
+ security_schemes.map do |security_scheme_name|
86
+ definition.components['securitySchemes'][security_scheme_name]
87
+ end
88
+ end
89
+
90
+ private
91
+
92
+ def local_parameters
93
+ return [] unless raw['parameters']
94
+ raw['parameters'].map do |definition|
95
+ OasParser::Parameter.new(self, definition)
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,46 @@
1
+ module OasParser
2
+ class Parameter
3
+ include OasParser::RawAccessor
4
+ raw_keys :name, :in, :description, :style, :enum, :schema,
5
+ :minimum, :maximum, :example, :default, :required
6
+
7
+ attr_accessor :owner, :raw
8
+
9
+ def initialize(owner, raw)
10
+ @owner = owner
11
+ @raw = raw
12
+ end
13
+
14
+ def type
15
+ raw['type'] || schema['type']
16
+ end
17
+
18
+ def array?
19
+ type == 'array'
20
+ end
21
+
22
+ def object?
23
+ type == 'object'
24
+ end
25
+
26
+ def collection?
27
+ array? || object?
28
+ end
29
+
30
+ def items
31
+ schema['items']
32
+ end
33
+
34
+ def properties
35
+ return convert_property_schema_to_properties(raw['properties']) if object?
36
+ return convert_property_schema_to_properties(items) if array?
37
+ nil
38
+ end
39
+
40
+ def convert_property_schema_to_properties(schema)
41
+ schema.map do |name, definition|
42
+ OasParser::Property.new(self, raw, name, definition)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,61 @@
1
+ module OasParser
2
+ class Parser
3
+ def self.resolve(path)
4
+ content = YAML.load_file(path)
5
+ Parser.new(path, content).resolve
6
+ end
7
+
8
+ def initialize(path, content)
9
+ @path = path
10
+ @content = content
11
+ end
12
+
13
+ def resolve
14
+ deeply_expand_refs(@content)
15
+ end
16
+
17
+ private
18
+
19
+ def deeply_expand_refs(fragment)
20
+ fragment = expand_refs(fragment)
21
+
22
+ if fragment.is_a?(Hash)
23
+ fragment.reduce({}) do |hash, (k, v)|
24
+ hash.merge(k => deeply_expand_refs(v))
25
+ end
26
+ elsif fragment.is_a?(Array)
27
+ fragment.map { |e| deeply_expand_refs(e) }
28
+ else
29
+ fragment
30
+ end
31
+ end
32
+
33
+ def expand_refs(fragment)
34
+ if fragment.is_a?(Hash) && fragment.has_key?("$ref")
35
+ ref = fragment["$ref"]
36
+
37
+ if ref =~ /\Afile:/
38
+ expand_file(ref)
39
+ else
40
+ expand_pointer(ref)
41
+ end
42
+ else
43
+ fragment
44
+ end
45
+ end
46
+
47
+ def expand_file(ref)
48
+ relative_path = ref.split(":").last
49
+ absolute_path = File.expand_path(File.join("..", relative_path), @path)
50
+
51
+ Parser.resolve(absolute_path)
52
+ end
53
+
54
+ def expand_pointer(ref)
55
+ pointer = OasParser::Pointer.new(ref)
56
+ fragment = pointer.resolve(@content)
57
+
58
+ expand_refs(fragment)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,45 @@
1
+ module OasParser
2
+ class Path
3
+ attr_accessor :owner, :path, :raw
4
+
5
+ def initialize(owner, path, raw)
6
+ @owner = owner
7
+ @path = path
8
+ @raw = raw
9
+ end
10
+
11
+ def endpoints
12
+ a = raw.map do |method, definition|
13
+ next unless %w[get head post put patch delete trace options].include? method
14
+ OasParser::Endpoint.new(self, method, definition)
15
+ end
16
+
17
+ a.compact
18
+ end
19
+
20
+ def endpoint_by_method(method)
21
+ definition = raw[method]
22
+ raise StandardError.new('So such endpoint exists') unless definition
23
+ OasParser::Endpoint.new(self, method, definition)
24
+ end
25
+
26
+ def parameter_keys
27
+ path.scan(/{(.+?)}/).flatten
28
+ end
29
+
30
+ def parameters
31
+ return [] unless raw['parameters']
32
+ raw['parameters'].map do |definition|
33
+ OasParser::Parameter.new(self, definition)
34
+ end
35
+ end
36
+
37
+ def definition
38
+ owner if owner.class == OasParser::Definition
39
+ end
40
+
41
+ def callback
42
+ owner if owner.class == OasParser::Callback
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,21 @@
1
+ module OasParser
2
+ class Payload
3
+ def formats
4
+ return [] unless content
5
+ content.keys
6
+ end
7
+
8
+ def schema(format)
9
+ content[format]['schema']
10
+ end
11
+
12
+ def split_schemas(format)
13
+ content[format]['schema']['oneOf']
14
+ end
15
+
16
+ def exhibits_one_of_multiple_schemas?(format)
17
+ return false unless content
18
+ schema(format).keys.include?('oneOf')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,45 @@
1
+ module OasParser
2
+ class Pointer
3
+ def initialize(raw_pointer)
4
+ @raw_pointer = raw_pointer
5
+ end
6
+
7
+ def resolve(document)
8
+ return document if escaped_pointer == ""
9
+
10
+ tokens.reduce(document) do |nested_doc, token|
11
+ nested_doc.fetch(token)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def escaped_pointer
18
+ if @raw_pointer.start_with?("#")
19
+ Addressable::URI.unencode(@raw_pointer[1..-1])
20
+ else
21
+ @raw_pointer
22
+ end
23
+ end
24
+
25
+ def parse_token(token)
26
+ if token =~ /\A\d+\z/
27
+ token.to_i
28
+ else
29
+ token.gsub("~0", "~").gsub("~1", "/")
30
+ end
31
+ end
32
+
33
+ def tokens
34
+ tokens = escaped_pointer[1..-1].split("/")
35
+
36
+ if @raw_pointer.end_with?("/")
37
+ tokens << ""
38
+ end
39
+
40
+ tokens.map do |token|
41
+ parse_token(token)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,46 @@
1
+ module OasParser
2
+ class Property
3
+ include OasParser::RawAccessor
4
+ raw_keys :name, :description, :type, :enum, :minimum, :maximum,
5
+ :example, :default, :items
6
+
7
+ attr_accessor :owner, :name, :schema, :raw
8
+
9
+ def initialize(owner, schema, name, raw)
10
+ @owner = owner
11
+ @name = name
12
+ @schema = schema
13
+ @raw = raw
14
+ end
15
+
16
+ def required
17
+ return false unless schema['required']
18
+ schema['required'].include? name
19
+ end
20
+
21
+ def array?
22
+ type == 'array'
23
+ end
24
+
25
+ def object?
26
+ type == 'object'
27
+ end
28
+
29
+ def collection?
30
+ array? || object?
31
+ end
32
+
33
+ def properties
34
+ return convert_property_schema_to_properties(raw['properties']) if object?
35
+ return convert_property_schema_to_properties(items) if array?
36
+ nil
37
+ end
38
+
39
+ def convert_property_schema_to_properties(schema)
40
+ schema = schema['properties'] if schema['properties']
41
+ schema.map do |name, definition|
42
+ OasParser::Property.new(self, raw, name, definition)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,26 @@
1
+ module OasParser
2
+ module RawAccessor
3
+ def self.included(klass)
4
+ klass.extend ClassMethods
5
+ end
6
+
7
+ def method_missing(method_name, *args, &block)
8
+ super unless self.class.get_raw_keys.include? method_name
9
+ raw[method_name.to_s]
10
+ end
11
+
12
+ def respond_to_missing?(method_name, include_private = false)
13
+ self.class.get_raw_keys.include?(method_name) || super
14
+ end
15
+
16
+ module ClassMethods
17
+ def raw_keys(*args)
18
+ @raw_keys = args
19
+ end
20
+
21
+ def get_raw_keys
22
+ @raw_keys || []
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ module OasParser
2
+ class RequestBody < Payload
3
+ include OasParser::RawAccessor
4
+ raw_keys :description, :required, :content
5
+
6
+ attr_accessor :endpoint, :raw
7
+
8
+ def initialize(endpoint, raw)
9
+ @endpoint = endpoint
10
+ @raw = raw
11
+ end
12
+
13
+ def properties_for_format(format)
14
+ schema(format)['properties'].map do |name, definition|
15
+ OasParser::Property.new(self, schema(format), name, definition)
16
+ end
17
+ end
18
+
19
+ def split_properties_for_format(format)
20
+ split_schemas(format).map do |schema|
21
+ schema['properties'].map do |name, definition|
22
+ OasParser::Property.new(self, schema(format), name, definition)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,18 @@
1
+ module OasParser
2
+ class Response < Payload
3
+ include OasParser::RawAccessor
4
+ raw_keys :description, :content
5
+
6
+ attr_accessor :endpoint, :code, :raw
7
+
8
+ def initialize(endpoint, code, raw)
9
+ @endpoint = endpoint
10
+ @code = code
11
+ @raw = raw
12
+ end
13
+
14
+ def success?
15
+ code.match?(/^2\d\d$/)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,103 @@
1
+ module OasParser
2
+ class ResponseParser
3
+ attr_accessor :raw
4
+
5
+ def initialize(raw)
6
+ @raw = raw
7
+ end
8
+
9
+ def parse
10
+ route(@raw)
11
+ end
12
+
13
+ private
14
+
15
+ def route(root_object)
16
+ case root_object['type']
17
+ when 'object'
18
+ parse_object(root_object)
19
+ when 'array' then parse_array(root_object)
20
+ when nil
21
+ return nil if root_object['additionalProperties'] == false
22
+ return nil if root_object['properties'] == {}
23
+
24
+ if treat_as_object?(root_object)
25
+ # Handle objects with missing type
26
+ return parse_object(root_object.merge({ 'type' => 'object' }))
27
+ end
28
+
29
+ raise StandardError.new("Unhandled object #{root_object} with missing type")
30
+ else
31
+ raise StandardError.new("Don't know how to parse #{root_object['type']}")
32
+ end
33
+ end
34
+
35
+ def parse_object(object)
36
+ raise StandardError.new("Not a hash") unless object.class == Hash
37
+ raise StandardError.new("Not an object") unless treat_as_object?(object)
38
+
39
+ if object['allOf']
40
+ merged_object = { 'type' => 'object' }
41
+ object['allOf'].each { |o| merged_object.deep_merge!(o) }
42
+ return parameter_value(merged_object)
43
+ elsif object['properties']
44
+ o = {}
45
+ object['properties'].each do |key, value|
46
+ o[key] = parameter_value(value)
47
+ end
48
+
49
+ return o
50
+ end
51
+
52
+ {}
53
+ end
54
+
55
+ def parse_array(object)
56
+ raise StandardError.new("Not an array") unless object['type'] == 'array'
57
+
58
+ case object['items']['type']
59
+ when 'object'
60
+ [parse_object(object['items'])]
61
+ else
62
+ if object['items']
63
+ # Handle objects with missing type
64
+ object['items']['type'] = 'object'
65
+ [parse_object(object['items'])]
66
+ else
67
+ raise StandardError.new("parse_array: Don't know how to parse object")
68
+ end
69
+ end
70
+ end
71
+
72
+ def parameter_value(parameter)
73
+ return parameter['example'] if parameter['example']
74
+ case (parameter['schema'] ? parameter['schema']['type'] : parameter['type'])
75
+ when 'integer' then return 1
76
+ when 'number' then return 1.0
77
+ when 'string' then return 'abc123'
78
+ when 'boolean' then return false
79
+ when 'object' then return parse_object(parameter)
80
+ when 'array' then return parse_array(parameter)
81
+ else
82
+ if treat_as_object?(parameter)
83
+ return parse_object(parameter)
84
+ end
85
+
86
+ if parameter['type']
87
+ raise StandardError.new("Can not resolve parameter type of #{parameter['type']}")
88
+ else
89
+ raise StandardError.new("Parameter #{parameter} is missing type.")
90
+ end
91
+ end
92
+ end
93
+
94
+
95
+ def treat_as_object?(object)
96
+ return true if object['type'] == 'object'
97
+ return true if object['allOf']
98
+ return true if object['oneOf']
99
+ return true if object['properties']
100
+ false
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,3 @@
1
+ module OasParser
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,33 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "oas_parser/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "oas_parser"
8
+ spec.version = OasParser::VERSION
9
+ spec.authors = ["Adam Butler"]
10
+ spec.email = ["adam@lab.io"]
11
+
12
+ spec.summary = %q{A parser for Open API specifications}
13
+ spec.homepage = "https://github.com/Nexmo/oas_parser"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "addressable", "~> 2.3"
24
+ spec.add_dependency "json_schema", "~> 0.15.0"
25
+ spec.add_dependency "guard-rspec", "~> 4.7.3"
26
+ spec.add_dependency "deep_merge", "~> 1.2.1"
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.16"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "pry", "~> 0.11.3"
32
+ spec.add_development_dependency "simplecov", "~> 0.15.1"
33
+ end
metadata ADDED
@@ -0,0 +1,198 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oas_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Butler
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-02 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: '2.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json_schema
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.15.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.15.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 4.7.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 4.7.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: deep_merge
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.2.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.2.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.16'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.16'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.11.3
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.11.3
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.15.1
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.15.1
139
+ description:
140
+ email:
141
+ - adam@lab.io
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".travis.yml"
149
+ - CODE_OF_CONDUCT.md
150
+ - Gemfile
151
+ - Gemfile.lock
152
+ - Guardfile
153
+ - LICENSE.txt
154
+ - README.md
155
+ - Rakefile
156
+ - bin/console
157
+ - bin/setup
158
+ - lib/oas_parser.rb
159
+ - lib/oas_parser/callback.rb
160
+ - lib/oas_parser/definition.rb
161
+ - lib/oas_parser/endpoint.rb
162
+ - lib/oas_parser/parameter.rb
163
+ - lib/oas_parser/parser.rb
164
+ - lib/oas_parser/path.rb
165
+ - lib/oas_parser/payload.rb
166
+ - lib/oas_parser/pointer.rb
167
+ - lib/oas_parser/property.rb
168
+ - lib/oas_parser/raw_accessor.rb
169
+ - lib/oas_parser/request_body.rb
170
+ - lib/oas_parser/response.rb
171
+ - lib/oas_parser/response_parser.rb
172
+ - lib/oas_parser/version.rb
173
+ - oas_parser.gemspec
174
+ homepage: https://github.com/Nexmo/oas_parser
175
+ licenses:
176
+ - MIT
177
+ metadata: {}
178
+ post_install_message:
179
+ rdoc_options: []
180
+ require_paths:
181
+ - lib
182
+ required_ruby_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ required_rubygems_version: !ruby/object:Gem::Requirement
188
+ requirements:
189
+ - - ">="
190
+ - !ruby/object:Gem::Version
191
+ version: '0'
192
+ requirements: []
193
+ rubyforge_project:
194
+ rubygems_version: 2.6.11
195
+ signing_key:
196
+ specification_version: 4
197
+ summary: A parser for Open API specifications
198
+ test_files: []