Module: Croods::Resource::JsonSchema::Links::Collection

Defined in:
lib/croods/resource/json_schema/links/collection.rb

Class Method Summary collapse

Class Method Details

.definition(attribute) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/croods/resource/json_schema/links/collection.rb', line 47

def definition(attribute)
  definition = Definition.schema(attribute)

  {
    anyOf: [{ type: ['array'], items: definition }, definition]
  }
end

.filters(resource) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/croods/resource/json_schema/links/collection.rb', line 22

def filters(resource)
  filters = {}

  resource.filters.each do |attribute|
    attribute.name = "#{attribute.name}_id" unless resource.model.has_attribute?(attribute.name)

    filters[attribute.name] = definition(attribute)
  end

  filters
end


9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/croods/resource/json_schema/links/collection.rb', line 9

def link(resource, action = nil)
  href = resource.collection_path
  href += "/#{action.name}" if action

  {
    href: href,
    method: action&.method&.upcase || 'GET',
    rel: 'instances',
    schema: schema(resource),
    targetSchema: target_schema(resource)
  }
end

.properties(resource) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/croods/resource/json_schema/links/collection.rb', line 34

def properties(resource)
  properties = {}

  params = resource.pagination_params + resource.order_params +
           resource.search_params

  params.each do |attribute|
    properties[attribute.name] = definition(attribute)
  end

  properties.merge(filters(resource))
end

.required(resource) ⇒ Object



55
56
57
58
59
60
# File 'lib/croods/resource/json_schema/links/collection.rb', line 55

def required(resource)
  collection_properties = resource.filters +
                          resource.pagination_params

  collection_properties.reject(&:null).map(&:name)
end

.schema(resource) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/croods/resource/json_schema/links/collection.rb', line 62

def schema(resource)
  {
    additionalProperties: false,
    properties: properties(resource),
    required: required(resource),
    type: ['object']
  }
end

.target_schema(resource) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/croods/resource/json_schema/links/collection.rb', line 71

def target_schema(resource)
  {
    anyOf: [
      { type: ['array'], items: resource.ref },
      { '$ref': '#/definitions/error' }
    ]
  }
end