apia-schema-parser 1.0.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
+ SHA256:
3
+ metadata.gz: b3e42e725c2a89060d290b451c8adff18274bc63b74a7b0f5ee3bff57d8556e3
4
+ data.tar.gz: a90d656b05c35bb6a9490b114110c072bc4f5d68e2475f62b09dff5a45f42ff7
5
+ SHA512:
6
+ metadata.gz: ab8988054e80cf46b1b246d8f1e26848195720690b9d6b9c6e605cb66a3a94b8fa7fe9e4068ee8d9cded5e0f20a60adee37d0b0ee439b4b498a3e5cbc0004f91
7
+ data.tar.gz: a23a1fac9bb197e25945ed6c7a4931e689a529cc2b7fbdb89df66158edc186d232bf18336c7a8ea163844f7eb3bb84a413dbaa5eec4db592d35e081a485c48b0
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia_schema_parser'
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'apia_schema_parser/schema'
5
+
6
+ module ApiaSchemaParser
7
+
8
+ def self.load_from_file(path)
9
+ contents = File.read(path)
10
+ json = JSON.parse(contents)
11
+ Schema.new(json)
12
+ end
13
+
14
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia_schema_parser/simple_object'
4
+ require 'apia_schema_parser/authenticator'
5
+ require 'apia_schema_parser/controller'
6
+ require 'apia_schema_parser/route_set'
7
+ require 'apia_schema_parser/scope'
8
+
9
+ module ApiaSchemaParser
10
+ class API < SimpleObject
11
+
12
+ def authenticator
13
+ @authenticator ||= schema.objects[@raw['authenticator']]
14
+ end
15
+
16
+ def route_set
17
+ @route_set ||= RouteSet.new(schema, @raw['route_set'])
18
+ end
19
+
20
+ def scopes
21
+ @scopes ||= @raw['scopes'].map { |s| Scope.new(s) }.sort_by { |s| s.name.upcase }
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia_schema_parser/simple_object'
4
+
5
+ module ApiaSchemaParser
6
+ class Argument
7
+
8
+ def initialize(set, raw)
9
+ @set = set
10
+ @schema = set.schema
11
+ @raw = raw
12
+ end
13
+
14
+ def name
15
+ @raw['name']
16
+ end
17
+
18
+ def description
19
+ @raw['description']
20
+ end
21
+
22
+ def type
23
+ return nil if @raw['type'].nil?
24
+
25
+ @schema.objects[@raw['type']]
26
+ end
27
+
28
+ def required?
29
+ @raw['required'] == true
30
+ end
31
+
32
+ def array?
33
+ @raw['array'] == true
34
+ end
35
+
36
+ def default
37
+ @raw['default']
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia_schema_parser/simple_object'
4
+ require 'apia_schema_parser/argument'
5
+
6
+ module ApiaSchemaParser
7
+ class ArgumentSet < SimpleObject
8
+
9
+ def arguments
10
+ @arguments ||= @raw['arguments'].each_with_object({}) do |a, hash|
11
+ arg = Argument.new(self, a)
12
+ hash[arg.name] = arg
13
+ end
14
+ end
15
+
16
+ def all_potential_errors
17
+ errors = respond_to?(:potential_errors) ? potential_errors : []
18
+ arguments.values.each do |arg|
19
+ next unless arg.type.is_a?(ArgumentSet)
20
+
21
+ errors |= arg.type.all_potential_errors
22
+ end
23
+ errors
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia_schema_parser/simple_object'
4
+ require 'apia_schema_parser/has_potential_errors'
5
+
6
+ module ApiaSchemaParser
7
+ class Authenticator < SimpleObject
8
+
9
+ include HasPotentialErrors
10
+
11
+ def type
12
+ @raw['type']
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia_schema_parser/simple_object'
4
+
5
+ module ApiaSchemaParser
6
+ class Controller < SimpleObject
7
+
8
+ def authenticator
9
+ return nil if @raw['authenticator'].nil?
10
+
11
+ @authenticator ||= schema.objects[@raw['authenticator']]
12
+ end
13
+
14
+ def endpoints
15
+ @endpoints ||= @raw['endpoints'].each_with_object({}) do |endpoint, hash|
16
+ hash[endpoint['name']] = schema.objects[endpoint['endpoint']]
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia_schema_parser/simple_object'
4
+ require 'apia_schema_parser/has_fields'
5
+ require 'apia_schema_parser/has_potential_errors'
6
+ require 'apia_schema_parser/argument_set'
7
+
8
+ module ApiaSchemaParser
9
+ class Endpoint < SimpleObject
10
+
11
+ include HasFields
12
+ include HasPotentialErrors
13
+
14
+ def authenticator
15
+ return nil if @raw['authenticator'].nil?
16
+
17
+ @authenticator ||= schema.objects[@raw['authenticator']]
18
+ end
19
+
20
+ def routes
21
+ schema.api.route_set.routes_by_endpoint[id]
22
+ end
23
+
24
+ def argument_set
25
+ @argument_set ||= ArgumentSet.new(schema, @raw['argument_set'])
26
+ end
27
+
28
+ def http_status
29
+ @raw['http_status']
30
+ end
31
+
32
+ def http_method
33
+ @raw['http_method']
34
+ end
35
+
36
+ def scopes
37
+ @raw['scopes']
38
+ end
39
+
40
+ def all_potential_errors
41
+ (potential_errors + argument_set.all_potential_errors).uniq
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia_schema_parser/simple_object'
4
+ require 'apia_schema_parser/enum_value'
5
+
6
+ module ApiaSchemaParser
7
+ class Enum < SimpleObject
8
+
9
+ def values
10
+ @values ||= @raw['values'].map { |e| EnumValue.new(e) }
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiaSchemaParser
4
+ class EnumValue
5
+
6
+ def initialize(raw)
7
+ @raw = raw
8
+ end
9
+
10
+ def name
11
+ @raw['name']
12
+ end
13
+
14
+ def description
15
+ @raw['description']
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia_schema_parser/simple_object'
4
+ require 'apia_schema_parser/has_fields'
5
+
6
+ module ApiaSchemaParser
7
+ class Error < SimpleObject
8
+
9
+ include HasFields
10
+
11
+ def code
12
+ @raw['code']
13
+ end
14
+
15
+ def http_status
16
+ @raw['http_status']
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia/field_spec'
4
+ require 'apia_schema_parser/simple_object'
5
+
6
+ module ApiaSchemaParser
7
+ class Field < SimpleObject
8
+
9
+ def type
10
+ schema.objects[@raw['type']]
11
+ end
12
+
13
+ def null?
14
+ @raw['null'] == true
15
+ end
16
+
17
+ def array?
18
+ @raw['array'] == true
19
+ end
20
+
21
+ def spec
22
+ return :all if @raw['spec']['all'] == true
23
+ return Apia::FieldSpec.parse(@raw['spec']['spec']) if @raw['spec']['spec']
24
+
25
+ nil
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia_schema_parser/field'
4
+
5
+ module ApiaSchemaParser
6
+ module HasFields
7
+
8
+ def fields
9
+ @fields ||= @raw['fields'].map { |field| Field.new(schema, field) }
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia_schema_parser/error'
4
+
5
+ module ApiaSchemaParser
6
+ module HasPotentialErrors
7
+
8
+ def potential_errors
9
+ @potential_errors ||= @raw['potential_errors'].map { |id| schema.objects[id] }
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia_schema_parser/argument_set'
4
+ require 'apia_schema_parser/has_potential_errors'
5
+
6
+ module ApiaSchemaParser
7
+ class LookupArgumentSet < ArgumentSet
8
+
9
+ include HasPotentialErrors
10
+
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia_schema_parser/simple_object'
4
+ require 'apia_schema_parser/has_fields'
5
+
6
+ module ApiaSchemaParser
7
+ class Object < SimpleObject
8
+
9
+ include HasFields
10
+
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia_schema_parser/simple_object'
4
+ require 'apia_schema_parser/polymorph_option'
5
+
6
+ module ApiaSchemaParser
7
+ class Polymorph < SimpleObject
8
+
9
+ def options
10
+ @options ||= @raw['options'].map { |o| PolymorphOption.new(schema, o) }
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module ApiaSchemaParser
2
+ class PolymorphOption
3
+
4
+ def initialize(schema, raw)
5
+ @schema = schema
6
+ @raw = raw
7
+ end
8
+
9
+ def name
10
+ @raw['name']
11
+ end
12
+
13
+ def type
14
+ @schema.objects[@raw['type']]
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiaSchemaParser
4
+ class Route
5
+
6
+ def initialize(set, raw)
7
+ @route_set = set
8
+ @raw = raw
9
+ end
10
+
11
+ def url
12
+ "#{@route_set.schema.url}/#{path}"
13
+ end
14
+
15
+ def path
16
+ @raw['path']
17
+ end
18
+
19
+ def request_method
20
+ @raw['request_method']
21
+ end
22
+
23
+ def group
24
+ return nil if @raw['group'].nil?
25
+
26
+ parts = @raw['group'].split('.')
27
+ source = @route_set.groups
28
+ parts.size.times do |i|
29
+ part = parts[0, i + 1].join('.')
30
+
31
+ source = source[part]
32
+ return nil if source.nil?
33
+ return source if i == parts.size - 1
34
+
35
+ source = source.groups
36
+ end
37
+ nil
38
+ end
39
+
40
+ def controller
41
+ return if @raw['controller'].nil?
42
+
43
+ @route_set.schema.objects[@raw['controller']]
44
+ end
45
+
46
+ def endpoint
47
+ return if @raw['endpoint'].nil?
48
+
49
+ @route_set.schema.objects[@raw['endpoint']]
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiaSchemaParser
4
+ class RouteGroup
5
+
6
+ def initialize(set, raw)
7
+ @route_set = set
8
+ @raw = raw
9
+ end
10
+
11
+ def id
12
+ @raw['id']
13
+ end
14
+
15
+ def name
16
+ @raw['name']
17
+ end
18
+
19
+ def description
20
+ @raw['description']
21
+ end
22
+
23
+ def routes
24
+ @route_set.routes_by_group[id]
25
+ end
26
+
27
+ def group_path
28
+ @group_path ||= begin
29
+ parts = @raw['id'].split('.')
30
+ last_group = @route_set
31
+ groups = []
32
+ parts.size.times do |i|
33
+ inner_parts = parts[0, i + 1]
34
+ id = inner_parts.join('.')
35
+ group = last_group.groups[id]
36
+ groups << group
37
+ last_group = group
38
+ end
39
+ groups
40
+ end
41
+ end
42
+
43
+ def group
44
+ return @group if instance_variable_defined?('@group')
45
+
46
+ @group = group_path[group_path.size - 2]
47
+ end
48
+
49
+ def groups
50
+ @groups ||= @raw['groups'].each_with_object({}) do |group_hash, hash|
51
+ group = RouteGroup.new(@route_set, group_hash)
52
+ hash[group.id] = group
53
+ end
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia_schema_parser/route_set'
4
+ require 'apia_schema_parser/route_group'
5
+ require 'apia_schema_parser/route'
6
+
7
+ module ApiaSchemaParser
8
+ class RouteSet < SimpleObject
9
+
10
+ def routes
11
+ @routes ||= @raw['routes'].map do |route_hash|
12
+ next if route_hash['controller'] =~ /\AApia\//
13
+
14
+ Route.new(self, route_hash)
15
+ end.compact
16
+ end
17
+
18
+ def routes_by_group
19
+ @routes_by_group ||= routes.each_with_object({}) do |route, hash|
20
+ hash[route.group&.id] ||= []
21
+ hash[route.group&.id] << route
22
+ end
23
+ end
24
+
25
+ def routes_by_endpoint
26
+ @routes_by_endpoint ||= routes.each_with_object({}) do |route, hash|
27
+ hash[route.endpoint&.id] ||= []
28
+ hash[route.endpoint&.id] << route
29
+ end
30
+ end
31
+
32
+ def groups
33
+ @groups ||= @raw['groups'].each_with_object({}) do |group_hash, hash|
34
+ group = RouteGroup.new(self, group_hash)
35
+ hash[group.id] = group
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia_schema_parser/simple_object'
4
+
5
+ module ApiaSchemaParser
6
+ class Scalar < SimpleObject
7
+
8
+ end
9
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia_schema_parser/api'
4
+ require 'apia_schema_parser/controller'
5
+ require 'apia_schema_parser/endpoint'
6
+ require 'apia_schema_parser/argument_set'
7
+ require 'apia_schema_parser/authenticator'
8
+ require 'apia_schema_parser/error'
9
+ require 'apia_schema_parser/object'
10
+ require 'apia_schema_parser/enum'
11
+ require 'apia_schema_parser/scalar'
12
+ require 'apia_schema_parser/polymorph'
13
+ require 'apia_schema_parser/lookup_argument_set'
14
+
15
+ module ApiaSchemaParser
16
+ class Schema
17
+
18
+ OBJECT_MAP = {
19
+ 'api' => API,
20
+ 'authenticator' => Authenticator,
21
+ 'controller' => Controller,
22
+ 'argument_set' => ArgumentSet,
23
+ 'endpoint' => Endpoint,
24
+ 'error' => Error,
25
+ 'object' => Object,
26
+ 'enum' => Enum,
27
+ 'scalar' => Scalar,
28
+ 'polymorph' => Polymorph,
29
+ 'lookup_argument_set' => LookupArgumentSet
30
+ }.freeze
31
+
32
+ def initialize(raw)
33
+ @raw = raw
34
+ end
35
+
36
+ def inspect
37
+ "#<ApiaSchemaParser::Schema[#{@raw['api']}] #{@raw['objects'].size} objects>"
38
+ end
39
+
40
+ def host
41
+ @raw['host']
42
+ end
43
+
44
+ def namespace
45
+ @raw['namespace']
46
+ end
47
+
48
+ def url
49
+ "https://#{host}#{namespace}"
50
+ end
51
+
52
+ def api
53
+ objects[@raw['api']]
54
+ end
55
+
56
+ def objects
57
+ @raw['objects'].each_with_object({}) do |object, hash|
58
+ klass = OBJECT_MAP[object['type']]
59
+ next if klass.nil?
60
+
61
+ hash[object['value']['id']] = klass.new(self, object['value'])
62
+ end
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiaSchemaParser
4
+ class Scope
5
+
6
+ def initialize(raw)
7
+ @raw = raw
8
+ end
9
+
10
+ def name
11
+ @raw['name']
12
+ end
13
+
14
+ def description
15
+ @raw['description']
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiaSchemaParser
4
+ class SimpleObject
5
+
6
+ attr_reader :schema
7
+
8
+ def initialize(schema, raw)
9
+ @schema = schema
10
+ @raw = raw
11
+ end
12
+
13
+ def id
14
+ @raw['id']
15
+ end
16
+
17
+ def name
18
+ @raw['name'] || id.split('/').last
19
+ end
20
+
21
+ def description
22
+ @raw['description']
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiaSchemaParser
4
+
5
+ VERSION_FILE_ROOT = File.expand_path('../../VERSION', __dir__)
6
+ if File.file?(VERSION_FILE_ROOT)
7
+ VERSION = File.read(VERSION_FILE_ROOT).strip.sub(/\Av/, '')
8
+ else
9
+ VERSION = '0.0.0.dev'
10
+ end
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apia-schema-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Cooke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: apia
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '4.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '4.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: json
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: A little library for reading Apia Schema files.
48
+ email:
49
+ - adam@k.io
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - VERSION
55
+ - lib/apia_schema-parser.rb
56
+ - lib/apia_schema_parser.rb
57
+ - lib/apia_schema_parser/api.rb
58
+ - lib/apia_schema_parser/argument.rb
59
+ - lib/apia_schema_parser/argument_set.rb
60
+ - lib/apia_schema_parser/authenticator.rb
61
+ - lib/apia_schema_parser/controller.rb
62
+ - lib/apia_schema_parser/endpoint.rb
63
+ - lib/apia_schema_parser/enum.rb
64
+ - lib/apia_schema_parser/enum_value.rb
65
+ - lib/apia_schema_parser/error.rb
66
+ - lib/apia_schema_parser/field.rb
67
+ - lib/apia_schema_parser/has_fields.rb
68
+ - lib/apia_schema_parser/has_potential_errors.rb
69
+ - lib/apia_schema_parser/lookup_argument_set.rb
70
+ - lib/apia_schema_parser/object.rb
71
+ - lib/apia_schema_parser/polymorph.rb
72
+ - lib/apia_schema_parser/polymorph_option.rb
73
+ - lib/apia_schema_parser/route.rb
74
+ - lib/apia_schema_parser/route_group.rb
75
+ - lib/apia_schema_parser/route_set.rb
76
+ - lib/apia_schema_parser/scalar.rb
77
+ - lib/apia_schema_parser/schema.rb
78
+ - lib/apia_schema_parser/scope.rb
79
+ - lib/apia_schema_parser/simple_object.rb
80
+ - lib/apia_schema_parser/version.rb
81
+ homepage: https://github.com/krystal/apia-schema-parser
82
+ licenses: []
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubygems_version: 3.1.6
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: This gem provides tools for reading Apia schema definitions.
103
+ test_files: []