Class: JsonapiSwaggerHelpers::SchemaHelpers

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi_swagger_helpers/schema_helpers.rb

Class Method Summary collapse

Class Method Details

.attributes_schemaObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/jsonapi_swagger_helpers/schema_helpers.rb', line 3

def self.attributes_schema
  lambda do |attributes|
    property :attributes do
      key :type, :object

      attributes.each_pair do |name, attr_type|
        property name do
          key :name, name
          key :type, attr_type
        end
      end
    end
  end
end

.relationships_schemaObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/jsonapi_swagger_helpers/schema_helpers.rb', line 18

def self.relationships_schema
  lambda do |relationships|
    property :relationships do
      key :type, :object

      relationships.each_pair do |relation_name, opts|
        property relation_name do
          property :data do
            if opts[:array]
              key :type, :array
              items do
                if opts[:id]
                  property :id do
                    key :type, :string
                  end
                end

                property :type do
                  key :type, :string
                  key :required, true
                  key :enum, [opts[:jsonapi_type]]
                end

                if opts[:attributes]
                  instance_exec(opts[:attributes], &SchemaHelpers.attributes_schema)
                end

                if opts[:relationships]
                  instance_exec(opts[:relationships], &SchemaHelpers.relationships_schema)
                end
              end
            else
              if opts[:id]
                property :id do
                  key :type, :string
                end
              end

              property :type do
                key :type, :string
                key :required, true
                key :enum, [opts[:jsonapi_type]]
              end

              if opts[:attributes]
                instance_exec(opts[:attributes], &SchemaHelpers.attributes_schema)
              end

              if opts[:relationships]
                instance_exec(opts[:relationships], &SchemaHelpers.relationships_schema)
              end
            end
          end
        end
      end
    end
  end
end