brujula 0.0.1
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 +7 -0
- data/CONTRIBUTING.md +9 -0
- data/Gemfile +5 -0
- data/README.md +64 -0
- data/Rakefile +21 -0
- data/SPEC_SUPPORT.md +183 -0
- data/TODO.md +1 -0
- data/lib/brujula.rb +71 -0
- data/lib/brujula/basic_type.rb +46 -0
- data/lib/brujula/data_transformers/body_declaration.rb +39 -0
- data/lib/brujula/data_transformers/property_declaration.rb +28 -0
- data/lib/brujula/data_transformers/security_scheme_settings_declaration.rb +23 -0
- data/lib/brujula/initializers/inflecto_ramelize.rb +9 -0
- data/lib/brujula/initializers/yaml_include.rb +42 -0
- data/lib/brujula/key.rb +45 -0
- data/lib/brujula/map_object.rb +99 -0
- data/lib/brujula/mergers/map_object_merger.rb +78 -0
- data/lib/brujula/mergers/merger.rb +27 -0
- data/lib/brujula/mergers/object_merger.rb +41 -0
- data/lib/brujula/object.rb +40 -0
- data/lib/brujula/object_builder.rb +114 -0
- data/lib/brujula/object_parser.rb +65 -0
- data/lib/brujula/raml.rb +5 -0
- data/lib/brujula/raml/definition.rb +45 -0
- data/lib/brujula/raml/exceptions.rb +6 -0
- data/lib/brujula/raml/v1_0/array.rb +8 -0
- data/lib/brujula/raml/v1_0/base_uri_parameters.rb +9 -0
- data/lib/brujula/raml/v1_0/body.rb +9 -0
- data/lib/brujula/raml/v1_0/body_type.rb +9 -0
- data/lib/brujula/raml/v1_0/header.rb +9 -0
- data/lib/brujula/raml/v1_0/markdown.rb +8 -0
- data/lib/brujula/raml/v1_0/media_type.rb +8 -0
- data/lib/brujula/raml/v1_0/method.rb +22 -0
- data/lib/brujula/raml/v1_0/null_security_scheme.rb +8 -0
- data/lib/brujula/raml/v1_0/property.rb +34 -0
- data/lib/brujula/raml/v1_0/query_parameter.rb +9 -0
- data/lib/brujula/raml/v1_0/raml_type.rb +37 -0
- data/lib/brujula/raml/v1_0/resource.rb +19 -0
- data/lib/brujula/raml/v1_0/resource_type.rb +15 -0
- data/lib/brujula/raml/v1_0/response.rb +16 -0
- data/lib/brujula/raml/v1_0/root.rb +30 -0
- data/lib/brujula/raml/v1_0/security_scheme.rb +15 -0
- data/lib/brujula/raml/v1_0/security_scheme_part.rb +18 -0
- data/lib/brujula/raml/v1_0/security_scheme_settings.rb +24 -0
- data/lib/brujula/raml/v1_0/string.rb +8 -0
- data/lib/brujula/raml/v1_0/trait.rb +17 -0
- data/lib/brujula/raml/v1_0/uri_parameter.rb +9 -0
- data/lib/brujula/raml/v1_0/uri_template.rb +8 -0
- data/lib/brujula/scheme.rb +77 -0
- data/lib/brujula/type_extender/method.rb +29 -0
- data/lib/brujula/type_extender/resource.rb +52 -0
- data/lib/brujula/type_extender/resource_type.rb +36 -0
- data/lib/brujula/version.rb +3 -0
- data/lib/brujula/yaml_parser.rb +67 -0
- metadata +224 -0
@@ -0,0 +1,15 @@
|
|
1
|
+
module Brujula
|
2
|
+
module Raml
|
3
|
+
module V1_0
|
4
|
+
class ResourceType < Brujula::Object
|
5
|
+
scheme do
|
6
|
+
key :display_name, as: :string
|
7
|
+
key :description, as: :markdown
|
8
|
+
key /^(get|patch|put|post|delete|options|header)[?]*$/,
|
9
|
+
as: :map_object, children: :method, accessor: :methods
|
10
|
+
key :type, as: :string
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Brujula
|
2
|
+
module Raml
|
3
|
+
module V1_0
|
4
|
+
class Response < Brujula::Object
|
5
|
+
scheme do
|
6
|
+
key :display_name, as: :string
|
7
|
+
key :description, as: :markdown
|
8
|
+
key :headers, as: :map_object, children: :header,
|
9
|
+
data_transformer: :property_declaration
|
10
|
+
key :body, as: :map_object, children: :body,
|
11
|
+
data_transformer: :body_declaration
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Brujula
|
2
|
+
module Raml
|
3
|
+
module V1_0
|
4
|
+
class Root < Brujula::Object
|
5
|
+
scheme do
|
6
|
+
key :title, as: :string
|
7
|
+
key :description, as: :markdown
|
8
|
+
key :version, as: :string
|
9
|
+
key :base_uri, as: :uri_template
|
10
|
+
key :base_uri_parameters, as: :query_parameter
|
11
|
+
key :media_type, as: :media_type
|
12
|
+
key :security_schemes, as: :map_object, children: :security_scheme
|
13
|
+
key :secured_by, as: :map_object, referrable: true
|
14
|
+
key :protocols, as: :map_object, children: :string
|
15
|
+
key :types, as: :map_object, children: :raml_type
|
16
|
+
key :resource_types, as: :map_object, children: :resource_type
|
17
|
+
key :traits, as: :map_object, children: :trait
|
18
|
+
key /^\/(.*)/, as: :map_object, children: :resource, accessor: :resources
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_reader :base_dir
|
22
|
+
|
23
|
+
def initialize(data:, name:, base_dir:)
|
24
|
+
@base_dir = base_dir
|
25
|
+
super(data: data, name: name)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Brujula
|
2
|
+
module Raml
|
3
|
+
module V1_0
|
4
|
+
class SecurityScheme < Brujula::Object
|
5
|
+
scheme do
|
6
|
+
key :type, as: :string, required: true
|
7
|
+
key :description, as: :markdown
|
8
|
+
key :described_by, as: :security_scheme_part
|
9
|
+
key :settings, as: :security_scheme_settings,
|
10
|
+
data_transformer: :security_scheme_settings_declaration
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Brujula
|
2
|
+
module Raml
|
3
|
+
module V1_0
|
4
|
+
class SecuritySchemePart < Brujula::Object
|
5
|
+
scheme do
|
6
|
+
key :display_name, as: :string
|
7
|
+
key :description, as: :markdown
|
8
|
+
key :responses, as: :map_object, children: :response
|
9
|
+
key :headers, as: :map_object, children: :header,
|
10
|
+
data_transformer: :property_declaration
|
11
|
+
key :query_parameters, as: :map_object, children: :query_parameter,
|
12
|
+
data_transformer: :property_declaration
|
13
|
+
key :query_string, as: :string
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Brujula
|
2
|
+
module Raml
|
3
|
+
module V1_0
|
4
|
+
class SecuritySchemeSettings < Brujula::Object
|
5
|
+
scheme typed: true do
|
6
|
+
key :type, as: :string
|
7
|
+
|
8
|
+
# When security scheme is OAuth 1.0
|
9
|
+
key :request_token_uri, as: :string, for_types: [ 'OAuth 1.0' ]
|
10
|
+
key :token_credentials_uri, as: :string, for_types: [ 'OAuth 1.0' ]
|
11
|
+
|
12
|
+
# When security scheme is OAuth 2.0
|
13
|
+
key :access_token_uri, as: :string, for_types: [ 'OAuth 2.0' ]
|
14
|
+
key :authorization_uri, as: :string, for_types: [ 'OAuth 2.0' ]
|
15
|
+
key :authorization_grants, as: :array, for_types: [ 'OAuth 2.0' ]
|
16
|
+
key :scopes, as: :array, for_types: [ 'OAuth 2.0' ]
|
17
|
+
|
18
|
+
# When security scheme is either OAuth 1.0
|
19
|
+
key :authorization_uri, as: :string, for_types: [ 'OAuth 1.0', 'OAuth 2.0' ]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Brujula
|
2
|
+
module Raml
|
3
|
+
module V1_0
|
4
|
+
class Trait < Brujula::Object
|
5
|
+
scheme do
|
6
|
+
key :usage, as: :markdown
|
7
|
+
key :description, as: :markdown
|
8
|
+
# key :parameters
|
9
|
+
key :query_parameters, as: :map_object, children: :query_parameter
|
10
|
+
key :headers, as: :map_object, children: :header,
|
11
|
+
data_transformer: :property_declaration
|
12
|
+
key :responses, as: :map_object, children: :response
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module Brujula
|
2
|
+
class Scheme
|
3
|
+
attr_reader :keys, :typed, :default_type, :reference,
|
4
|
+
:store_for_reference, :allow_any
|
5
|
+
|
6
|
+
alias :typed? :typed
|
7
|
+
alias :store_object_for_reference? :store_for_reference
|
8
|
+
alias :allow_any? :allow_any
|
9
|
+
|
10
|
+
def initialize(klass, options = {}, block = nil)
|
11
|
+
@klass = klass
|
12
|
+
@options = options
|
13
|
+
@keys = {}
|
14
|
+
@typed = !!options[:typed]
|
15
|
+
@default_type = options[:default_type] || :string
|
16
|
+
@store_for_reference = !!options[:store_for_reference]
|
17
|
+
@allow_any = !!options[:allow_any]
|
18
|
+
|
19
|
+
if options[:as]
|
20
|
+
block = Brujula::Raml::V1_0.const_get(
|
21
|
+
Inflecto.camelize(options[:as])
|
22
|
+
).block
|
23
|
+
end
|
24
|
+
|
25
|
+
instance_exec(&block)
|
26
|
+
end
|
27
|
+
|
28
|
+
def key_collection
|
29
|
+
@keys.values
|
30
|
+
end
|
31
|
+
|
32
|
+
def validate_data!(data)
|
33
|
+
valid_keys?(data) || raise(Brujula::Raml::RequiredProperty) # && enough_data?(data))
|
34
|
+
end
|
35
|
+
|
36
|
+
def typed_keys(type)
|
37
|
+
keys.values.select do |brujula_key|
|
38
|
+
brujula_key.valid_for_type?(type)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
attr_reader :klass
|
45
|
+
|
46
|
+
def key(name, options = {})
|
47
|
+
store_key(name, options)
|
48
|
+
end
|
49
|
+
|
50
|
+
def valid_keys?(data)
|
51
|
+
(ramelized_required_keys - data.keys).empty?
|
52
|
+
end
|
53
|
+
|
54
|
+
def ramelized_required_keys
|
55
|
+
@ramelized_required_keys ||= keys_for_type.values
|
56
|
+
.select(&:required?).select(&:fixed?)
|
57
|
+
.map { |object| ramelize_key(object.expression) }
|
58
|
+
end
|
59
|
+
|
60
|
+
def keys_for_type
|
61
|
+
return keys unless typed
|
62
|
+
|
63
|
+
keys.select { |key| key.include_in_type?() }
|
64
|
+
end
|
65
|
+
|
66
|
+
def ramelize_key(key)
|
67
|
+
Inflecto.ramelize(key.to_s)
|
68
|
+
end
|
69
|
+
|
70
|
+
def store_key(name, options)
|
71
|
+
key = Brujula::Key.new(name, options)
|
72
|
+
klass.class_eval { attr_reader key.accessor_name }
|
73
|
+
|
74
|
+
@keys.merge!(key.accessor_name => key)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Brujula
|
2
|
+
module TypeExtender
|
3
|
+
class Method
|
4
|
+
attr_reader :definition
|
5
|
+
|
6
|
+
def initialize(definition:)
|
7
|
+
@definition = definition
|
8
|
+
end
|
9
|
+
|
10
|
+
def call
|
11
|
+
return definition if definition.is.nil?
|
12
|
+
|
13
|
+
extended_object
|
14
|
+
end
|
15
|
+
|
16
|
+
def extended_object
|
17
|
+
@extended_object ||= apply_inherit_chain
|
18
|
+
end
|
19
|
+
|
20
|
+
def apply_inherit_chain
|
21
|
+
definition.is.inject(definition.dup) do |object, trait|
|
22
|
+
Brujula::Mergers::ObjectMerger.new(
|
23
|
+
superinstance: trait, instance: object
|
24
|
+
).call
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Brujula
|
2
|
+
module TypeExtender
|
3
|
+
class Resource
|
4
|
+
attr_reader :definition
|
5
|
+
|
6
|
+
def initialize(definition:)
|
7
|
+
@definition = definition
|
8
|
+
end
|
9
|
+
|
10
|
+
def call
|
11
|
+
return definition if definition.type.nil? && definition.is.nil?
|
12
|
+
|
13
|
+
extended_object
|
14
|
+
end
|
15
|
+
|
16
|
+
def extended_object
|
17
|
+
@extended_object ||= apply_inherit_chain
|
18
|
+
end
|
19
|
+
|
20
|
+
def apply_inherit_chain
|
21
|
+
object = definition.dup
|
22
|
+
object = apply_type(object) unless definition.type.nil?
|
23
|
+
object = apply_traits(object) unless definition.is.nil?
|
24
|
+
object
|
25
|
+
end
|
26
|
+
|
27
|
+
def apply_type(object)
|
28
|
+
Brujula::Mergers::ObjectMerger.new(
|
29
|
+
superinstance: definition.type, instance: object
|
30
|
+
).call
|
31
|
+
end
|
32
|
+
|
33
|
+
def apply_traits(object)
|
34
|
+
return object if definition.methods.nil?
|
35
|
+
|
36
|
+
object.methods.each do |method|
|
37
|
+
new_method = apply_traits_to_method(method)
|
38
|
+
object.methods.merge(new_method.name, new_method)
|
39
|
+
end
|
40
|
+
object
|
41
|
+
end
|
42
|
+
|
43
|
+
def apply_traits_to_method(method)
|
44
|
+
definition.is.inject(method.dup) do |object, trait|
|
45
|
+
Brujula::Mergers::ObjectMerger.new(
|
46
|
+
superinstance: trait, instance: object
|
47
|
+
).call
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Brujula
|
2
|
+
module TypeExtender
|
3
|
+
class ResourceType
|
4
|
+
attr_reader :definition
|
5
|
+
|
6
|
+
def initialize(definition:)
|
7
|
+
@definition = definition
|
8
|
+
end
|
9
|
+
|
10
|
+
def call
|
11
|
+
return definition if definition.type.nil?
|
12
|
+
|
13
|
+
extended_object
|
14
|
+
end
|
15
|
+
|
16
|
+
def parent_definition
|
17
|
+
if within_root_resource_types? # maybe its not preload
|
18
|
+
definition.parent.fetch(definition.type)
|
19
|
+
else
|
20
|
+
definition.root.resource_types.fetch(definition.type)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def extended_object
|
25
|
+
@extended_object ||= Brujula::Mergers::ObjectMerger.new(
|
26
|
+
superinstance: parent_definition, instance: definition
|
27
|
+
).call
|
28
|
+
end
|
29
|
+
|
30
|
+
def within_root_resource_types?
|
31
|
+
definition.parent.is_a?(Brujula::MapObject) &&
|
32
|
+
definition.parent.parent.is_a?(Brujula::Raml::V1_0::Root)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# TODO
|
2
|
+
require 'psych'
|
3
|
+
|
4
|
+
# `stolen` from:
|
5
|
+
# http://stackoverflow.com/questions/29462856/loading-yaml-with-line-number-for-each-key
|
6
|
+
|
7
|
+
class Psych::Nodes::Node
|
8
|
+
attr_accessor :_brujula_line
|
9
|
+
end
|
10
|
+
|
11
|
+
class Psych::Visitors::ToRuby
|
12
|
+
def revive_hash hash, o
|
13
|
+
o.children.each_slice(2) { |k,v|
|
14
|
+
key = accept(k)
|
15
|
+
val = accept(v)
|
16
|
+
|
17
|
+
# TODO
|
18
|
+
# This is not working because instead of returning a `val`, which could
|
19
|
+
# be an string for example, it's returning a hash
|
20
|
+
# Actually, I am not really interested in adding the line number to
|
21
|
+
# object values. I am only interested in adding the lines to the object
|
22
|
+
# itself, probably, as some kind of raml anotation, so it could be used
|
23
|
+
# as another anotated property, and thus, displayed in the documentation
|
24
|
+
# TODO
|
25
|
+
|
26
|
+
if v.is_a? ::Psych::Nodes::Scalar
|
27
|
+
val = { "value" => val, "_brujula_line" => v._brujula_line + 1} # line is 0 based, so + 1
|
28
|
+
end
|
29
|
+
|
30
|
+
# Code dealing with << (for merging hashes) omitted.
|
31
|
+
# If you need this you will probably need to copy it
|
32
|
+
# in here. See the method:
|
33
|
+
# https://github.com/tenderlove/psych/blob/v2.0.13/lib/psych/visitors/to_ruby.rb#L333-L365
|
34
|
+
|
35
|
+
hash[key] = val
|
36
|
+
}
|
37
|
+
hash
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module Brujula
|
42
|
+
class LineNumberHandler < Psych::TreeBuilder
|
43
|
+
attr_accessor :parser
|
44
|
+
|
45
|
+
def scalar value, anchor, tag, plain, quoted, style
|
46
|
+
mark = parser.mark
|
47
|
+
s = super
|
48
|
+
s._brujula_line = mark.line
|
49
|
+
s
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class YamlParser
|
54
|
+
class << self
|
55
|
+
def load(string)
|
56
|
+
handler = Brujula::LineNumberHandler.new
|
57
|
+
parser = Psych::Parser.new(handler)
|
58
|
+
|
59
|
+
handler.parser = parser
|
60
|
+
|
61
|
+
parser.parse string
|
62
|
+
|
63
|
+
handler.root.to_ruby.first
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|