apia 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +7 -0
  2. data/VERSION +1 -0
  3. data/lib/apia.rb +21 -0
  4. data/lib/apia/api.rb +100 -0
  5. data/lib/apia/argument_set.rb +221 -0
  6. data/lib/apia/authenticator.rb +57 -0
  7. data/lib/apia/callable_with_environment.rb +43 -0
  8. data/lib/apia/controller.rb +32 -0
  9. data/lib/apia/defineable.rb +60 -0
  10. data/lib/apia/definition.rb +27 -0
  11. data/lib/apia/definitions/api.rb +51 -0
  12. data/lib/apia/definitions/argument.rb +77 -0
  13. data/lib/apia/definitions/argument_set.rb +33 -0
  14. data/lib/apia/definitions/authenticator.rb +46 -0
  15. data/lib/apia/definitions/controller.rb +41 -0
  16. data/lib/apia/definitions/endpoint.rb +74 -0
  17. data/lib/apia/definitions/enum.rb +31 -0
  18. data/lib/apia/definitions/error.rb +59 -0
  19. data/lib/apia/definitions/field.rb +117 -0
  20. data/lib/apia/definitions/lookup_argument_set.rb +27 -0
  21. data/lib/apia/definitions/object.rb +29 -0
  22. data/lib/apia/definitions/polymorph.rb +29 -0
  23. data/lib/apia/definitions/polymorph_option.rb +53 -0
  24. data/lib/apia/definitions/scalar.rb +23 -0
  25. data/lib/apia/definitions/type.rb +109 -0
  26. data/lib/apia/dsl.rb +23 -0
  27. data/lib/apia/dsls/api.rb +37 -0
  28. data/lib/apia/dsls/argument.rb +27 -0
  29. data/lib/apia/dsls/argument_set.rb +35 -0
  30. data/lib/apia/dsls/authenticator.rb +38 -0
  31. data/lib/apia/dsls/concerns/has_fields.rb +38 -0
  32. data/lib/apia/dsls/controller.rb +34 -0
  33. data/lib/apia/dsls/endpoint.rb +79 -0
  34. data/lib/apia/dsls/enum.rb +19 -0
  35. data/lib/apia/dsls/error.rb +26 -0
  36. data/lib/apia/dsls/field.rb +27 -0
  37. data/lib/apia/dsls/lookup_argument_set.rb +24 -0
  38. data/lib/apia/dsls/object.rb +19 -0
  39. data/lib/apia/dsls/polymorph.rb +19 -0
  40. data/lib/apia/dsls/route_group.rb +43 -0
  41. data/lib/apia/dsls/route_set.rb +40 -0
  42. data/lib/apia/dsls/scalar.rb +23 -0
  43. data/lib/apia/dsls/scope_descriptions.rb +17 -0
  44. data/lib/apia/endpoint.rb +110 -0
  45. data/lib/apia/enum.rb +43 -0
  46. data/lib/apia/environment_error_handling.rb +74 -0
  47. data/lib/apia/error.rb +61 -0
  48. data/lib/apia/error_set.rb +15 -0
  49. data/lib/apia/errors/error_exception_error.rb +32 -0
  50. data/lib/apia/errors/field_spec_parse_error.rb +23 -0
  51. data/lib/apia/errors/invalid_argument_error.rb +68 -0
  52. data/lib/apia/errors/invalid_enum_option_error.rb +21 -0
  53. data/lib/apia/errors/invalid_helper_error.rb +6 -0
  54. data/lib/apia/errors/invalid_json_error.rb +23 -0
  55. data/lib/apia/errors/invalid_polymorph_value_error.rb +21 -0
  56. data/lib/apia/errors/invalid_scalar_value_error.rb +21 -0
  57. data/lib/apia/errors/manifest_error.rb +43 -0
  58. data/lib/apia/errors/missing_argument_error.rb +40 -0
  59. data/lib/apia/errors/null_field_value_error.rb +37 -0
  60. data/lib/apia/errors/parse_error.rb +10 -0
  61. data/lib/apia/errors/runtime_error.rb +30 -0
  62. data/lib/apia/errors/scope_not_granted_error.rb +15 -0
  63. data/lib/apia/errors/standard_error.rb +6 -0
  64. data/lib/apia/field_set.rb +76 -0
  65. data/lib/apia/field_spec.rb +155 -0
  66. data/lib/apia/helpers.rb +34 -0
  67. data/lib/apia/hook_set.rb +30 -0
  68. data/lib/apia/lookup_argument_set.rb +57 -0
  69. data/lib/apia/lookup_environment.rb +27 -0
  70. data/lib/apia/manifest_errors.rb +62 -0
  71. data/lib/apia/mock_request.rb +18 -0
  72. data/lib/apia/object.rb +68 -0
  73. data/lib/apia/object_set.rb +21 -0
  74. data/lib/apia/pagination_object.rb +34 -0
  75. data/lib/apia/polymorph.rb +50 -0
  76. data/lib/apia/rack.rb +184 -0
  77. data/lib/apia/rack_error.rb +17 -0
  78. data/lib/apia/request.rb +67 -0
  79. data/lib/apia/request_environment.rb +84 -0
  80. data/lib/apia/request_headers.rb +42 -0
  81. data/lib/apia/response.rb +64 -0
  82. data/lib/apia/route.rb +61 -0
  83. data/lib/apia/route_group.rb +20 -0
  84. data/lib/apia/route_set.rb +89 -0
  85. data/lib/apia/scalar.rb +52 -0
  86. data/lib/apia/scalars.rb +25 -0
  87. data/lib/apia/scalars/base64.rb +31 -0
  88. data/lib/apia/scalars/boolean.rb +37 -0
  89. data/lib/apia/scalars/date.rb +45 -0
  90. data/lib/apia/scalars/decimal.rb +36 -0
  91. data/lib/apia/scalars/integer.rb +34 -0
  92. data/lib/apia/scalars/string.rb +24 -0
  93. data/lib/apia/scalars/unix_time.rb +40 -0
  94. data/lib/apia/schema/api_controller_schema_type.rb +17 -0
  95. data/lib/apia/schema/api_schema_type.rb +43 -0
  96. data/lib/apia/schema/argument_schema_type.rb +28 -0
  97. data/lib/apia/schema/argument_set_schema_type.rb +21 -0
  98. data/lib/apia/schema/authenticator_schema_type.rb +22 -0
  99. data/lib/apia/schema/controller.rb +39 -0
  100. data/lib/apia/schema/controller_endpoint_schema_type.rb +17 -0
  101. data/lib/apia/schema/controller_schema_type.rb +32 -0
  102. data/lib/apia/schema/endpoint_schema_type.rb +35 -0
  103. data/lib/apia/schema/enum_schema_type.rb +20 -0
  104. data/lib/apia/schema/enum_value_schema_type.rb +14 -0
  105. data/lib/apia/schema/error_schema_type.rb +23 -0
  106. data/lib/apia/schema/field_schema_type.rb +38 -0
  107. data/lib/apia/schema/field_spec_options_schema_type.rb +16 -0
  108. data/lib/apia/schema/lookup_argument_set_schema_type.rb +25 -0
  109. data/lib/apia/schema/object_schema_polymorph.rb +31 -0
  110. data/lib/apia/schema/object_schema_type.rb +21 -0
  111. data/lib/apia/schema/polymorph_option_schema_type.rb +16 -0
  112. data/lib/apia/schema/polymorph_schema_type.rb +20 -0
  113. data/lib/apia/schema/request_method_enum.rb +21 -0
  114. data/lib/apia/schema/route_group_schema_type.rb +19 -0
  115. data/lib/apia/schema/route_schema_type.rb +31 -0
  116. data/lib/apia/schema/route_set_schema_type.rb +20 -0
  117. data/lib/apia/schema/scalar_schema_type.rb +15 -0
  118. data/lib/apia/schema/scope_type.rb +14 -0
  119. data/lib/apia/version.rb +12 -0
  120. metadata +188 -0
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia/scalars'
4
+ require 'apia/scalar'
5
+
6
+ module Apia
7
+ module Scalars
8
+ class Decimal < Apia::Scalar
9
+
10
+ Scalars.register :decimal, self
11
+
12
+ name 'Decimal'
13
+
14
+ cast do |value|
15
+ value.to_f
16
+ end
17
+
18
+ validator do |value|
19
+ value.is_a?(::Float)
20
+ end
21
+
22
+ parse do |value|
23
+ if value.is_a?(::String) && value =~ /\A-?\d+(\.\d+)?\z/
24
+ value.to_f
25
+ elsif value.is_a?(::Float)
26
+ value
27
+ elsif value.is_a?(::Integer)
28
+ value.to_i
29
+ else
30
+ raise Apia::ParseError, 'Decimal must be provided as a decimal, integer or a string only containing numbers'
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia/scalars'
4
+ require 'apia/scalar'
5
+
6
+ module Apia
7
+ module Scalars
8
+ class Integer < Apia::Scalar
9
+
10
+ Scalars.register :integer, self
11
+
12
+ name 'Integer'
13
+
14
+ cast do |value|
15
+ value.to_i
16
+ end
17
+
18
+ validator do |value|
19
+ value.is_a?(::Integer)
20
+ end
21
+
22
+ parse do |value|
23
+ if value.is_a?(::String) && value =~ /\A-?\d+\z/
24
+ value.to_i
25
+ elsif value.is_a?(::Integer)
26
+ value
27
+ else
28
+ raise Apia::ParseError, 'Integer must be provided as an integer or a string only containing numbers'
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia/scalars'
4
+ require 'apia/scalar'
5
+
6
+ module Apia
7
+ module Scalars
8
+ class String < Apia::Scalar
9
+
10
+ Scalars.register :string, self
11
+
12
+ name 'String'
13
+
14
+ cast do |value|
15
+ value.to_s
16
+ end
17
+
18
+ validator do |value|
19
+ value.is_a?(::String) || value.is_a?(::Symbol)
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'time'
4
+ require 'apia/scalars'
5
+ require 'apia/scalar'
6
+ require 'apia/errors/parse_error'
7
+
8
+ module Apia
9
+ module Scalars
10
+ class UnixTime < Apia::Scalar
11
+
12
+ Scalars.register :unix_time, self
13
+
14
+ name 'Unix Timestamp'
15
+
16
+ cast do |time|
17
+ time.to_i
18
+ end
19
+
20
+ validator do |value|
21
+ value.is_a?(::Time)
22
+ end
23
+
24
+ parse do |integer|
25
+ next integer if integer.is_a?(::Time)
26
+
27
+ unless integer.is_a?(::Integer)
28
+ raise Apia::ParseError, 'Time must be provided as an integer'
29
+ end
30
+
31
+ if integer.negative?
32
+ raise Apia::ParseError, 'Integer must be positive or zero'
33
+ end
34
+
35
+ Time.at(integer).utc
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia/object'
4
+ require 'apia/schema/controller_schema_type'
5
+
6
+ module Apia
7
+ module Schema
8
+ class APIControllerSchemaType < Apia::Object
9
+
10
+ no_schema
11
+
12
+ field :name, type: :string
13
+ field :controller, type: :string
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia/object'
4
+ require 'apia/schema/authenticator_schema_type'
5
+ require 'apia/schema/api_controller_schema_type'
6
+ require 'apia/schema/object_schema_polymorph'
7
+ require 'apia/schema/route_set_schema_type'
8
+ require 'apia/schema/scope_type'
9
+
10
+ module Apia
11
+ module Schema
12
+ class APISchemaType < Apia::Object
13
+
14
+ no_schema
15
+
16
+ condition { |api| api.schema? }
17
+
18
+ field :id, type: :string do
19
+ backend { |api| api.id }
20
+ end
21
+
22
+ field :name, type: :string, null: true do
23
+ backend { |api| api.name }
24
+ end
25
+
26
+ field :description, type: :string, null: true do
27
+ backend { |api| api.description }
28
+ end
29
+
30
+ field :authenticator, type: :string, null: true do
31
+ backend { |api| api.authenticator&.definition&.id }
32
+ end
33
+
34
+ field :route_set, type: RouteSetSchemaType
35
+ field :scopes, type: [ScopeType] do
36
+ backend do |api|
37
+ api.scopes.map { |k, v| v.merge(name: k) }
38
+ end
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia/object'
4
+
5
+ module Apia
6
+ module Schema
7
+ class ArgumentSchemaType < Apia::Object
8
+
9
+ no_schema
10
+
11
+ field :name, type: :string
12
+ field :description, type: :string, null: true
13
+ field :type, type: :string do
14
+ backend { |f| f.type.id }
15
+ end
16
+ field :required, type: :boolean do
17
+ backend(&:required?)
18
+ end
19
+ field :array, type: :boolean do
20
+ backend(&:array?)
21
+ end
22
+ field :default, type: :string, null: true do
23
+ backend { |o| o.default&.to_s }
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia/object'
4
+ require 'apia/schema/argument_schema_type'
5
+
6
+ module Apia
7
+ module Schema
8
+ class ArgumentSetSchemaType < Apia::Object
9
+
10
+ no_schema
11
+
12
+ field :id, type: :string
13
+ field :name, type: :string, null: true
14
+ field :description, type: :string, null: true
15
+ field :arguments, type: [ArgumentSchemaType] do
16
+ backend { |as| as.arguments.values }
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia/object'
4
+ require 'apia/schema/error_schema_type'
5
+
6
+ module Apia
7
+ module Schema
8
+ class AuthenticatorSchemaType < Apia::Object
9
+
10
+ no_schema
11
+
12
+ field :id, type: :string
13
+ field :name, type: :string, null: true
14
+ field :description, type: :string, null: true
15
+ field :type, type: :string
16
+ field :potential_errors, type: [:string] do
17
+ backend { |a| a.potential_errors.map { |e| e.definition.id } }
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia/controller'
4
+ require 'apia/authenticator'
5
+ require 'apia/schema/object_schema_polymorph'
6
+ require 'apia/schema/route_set_schema_type'
7
+
8
+ module Apia
9
+ module Schema
10
+ class Controller < Apia::Controller
11
+
12
+ no_schema
13
+
14
+ authenticator do
15
+ type :anonymous
16
+ end
17
+
18
+ name 'API Schema'
19
+ description 'Provides endpoint to interrogate the API schema'
20
+ endpoint :schema do
21
+ no_schema
22
+ description 'Returns a payload outlining the full schema of the API'
23
+ field :schema_version, type: :integer
24
+ field :host, type: :string
25
+ field :namespace, type: :string
26
+ field :api, type: :string
27
+ field :objects, type: [ObjectSchemaPolymorph]
28
+ action do
29
+ response.add_field :schema_version, 1
30
+ response.add_field :objects, request.api.objects.map(&:definition).select(&:schema?)
31
+ response.add_field :api, request.api.definition.id
32
+ response.add_field :namespace, request.namespace
33
+ response.add_field :host, request.host
34
+ end
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia/object'
4
+ require 'apia/schema/endpoint_schema_type'
5
+
6
+ module Apia
7
+ module Schema
8
+ class ControllerEndpointSchemaType < Apia::Object
9
+
10
+ no_schema
11
+
12
+ field :name, type: :string
13
+ field :endpoint, type: :string
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia/object'
4
+ require 'apia/schema/authenticator_schema_type'
5
+ require 'apia/schema/controller_endpoint_schema_type'
6
+
7
+ module Apia
8
+ module Schema
9
+ class ControllerSchemaType < Apia::Object
10
+
11
+ no_schema
12
+
13
+ field :id, type: :string
14
+ field :name, type: :string, null: true
15
+ field :description, type: :string, null: true
16
+ field :authenticator, type: :string, null: true do
17
+ backend { |c| c.authenticator&.definition&.id }
18
+ end
19
+ field :endpoints, type: [ControllerEndpointSchemaType] do
20
+ backend do |c|
21
+ c.endpoints.map do |key, endpoint|
22
+ {
23
+ name: key.to_s,
24
+ endpoint: endpoint.definition.id
25
+ }
26
+ end
27
+ end
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia/object'
4
+ require 'apia/schema/authenticator_schema_type'
5
+ require 'apia/schema/field_schema_type'
6
+ require 'apia/schema/argument_set_schema_type'
7
+ require 'apia/schema/error_schema_type'
8
+
9
+ module Apia
10
+ module Schema
11
+ class EndpointSchemaType < Apia::Object
12
+
13
+ no_schema
14
+
15
+ field :id, type: :string
16
+ field :name, type: :string, null: true
17
+ field :description, type: :string, null: true
18
+ field :http_status, type: :integer, backend: :http_status_code
19
+ field :authenticator, type: :string, null: true do
20
+ backend { |e| e.authenticator&.definition&.id }
21
+ end
22
+ field :argument_set, type: ArgumentSetSchemaType do
23
+ backend { |e| e.argument_set.definition }
24
+ end
25
+ field :fields, type: [FieldSchemaType] do
26
+ backend { |e| e.fields.values }
27
+ end
28
+ field :potential_errors, type: [:string] do
29
+ backend { |a| a.potential_errors.map { |e| e.definition.id } }
30
+ end
31
+ field :scopes, type: [:string]
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia/schema/enum_value_schema_type'
4
+
5
+ module Apia
6
+ module Schema
7
+ class EnumSchemaType < Apia::Object
8
+
9
+ no_schema
10
+
11
+ field :id, type: :string
12
+ field :name, type: :string, null: true
13
+ field :description, type: :string, null: true
14
+ field :values, type: [EnumValueSchemaType] do
15
+ backend { |e| e.values.values }
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Apia
4
+ module Schema
5
+ class EnumValueSchemaType < Apia::Object
6
+
7
+ no_schema
8
+
9
+ field :name, type: :string
10
+ field :description, type: :string, null: true
11
+
12
+ end
13
+ end
14
+ end