Class: JsonapiSwaggerHelpers::Util

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

Class Method Summary collapse

Class Method Details

.all_resources(resource, include_directive, memo = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/jsonapi_swagger_helpers/util.rb', line 30

def self.all_resources(resource, include_directive, memo = {})
  resource.sideloading.sideloads.each_pair do |name, sideload|
    next if memo[name] || !include_directive.key?(name)

    memo[name] = sideload.resource.class
    all_resources(sideload.resource.class, include_directive[name], memo)
  end
  memo
end

.controller_for(path) ⇒ Object



3
4
5
6
7
# File 'lib/jsonapi_swagger_helpers/util.rb', line 3

def self.controller_for(path)
  path = path.sub('{id}', '1')
  route = Rails.application.routes.recognize_path(path)
  "#{route[:controller]}_controller".classify.constantize
end

.each_filter(resource, association_name = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/jsonapi_swagger_helpers/util.rb', line 20

def self.each_filter(resource, association_name = nil)
  resource.config[:filters].each_pair do |filter_name, opts|
    if association_name
      yield "filter[#{association_name}][#{filter_name}]"
    else
      yield "filter[#{filter_name}]"
    end
  end
end

.id_in_url(node) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/jsonapi_swagger_helpers/util.rb', line 157

def self.id_in_url(node)
  node.parameter do
    key :name, :id
    key :in, :path
    key :type, :string
    key :required, true
    key :description, 'record id'
  end
end

.include_directive_for(controller, action) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jsonapi_swagger_helpers/util.rb', line 40

def self.include_directive_for(controller, action)
  resource_class = controller._jsonapi_compliable
  includes       = resource_class.sideloading.to_hash[:base]
  whitelist      = resource_class.config[:sideload_whitelist]

  if whitelist && whitelist[action]
    includes = JsonapiCompliable::Util::IncludeParams
      .scrub(includes, whitelist[action])
  end

  JSONAPI::IncludeDirective.new(includes)
end

.jsonapi_extra_fields(node, resource) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/jsonapi_swagger_helpers/util.rb', line 144

def self.jsonapi_extra_fields(node, resource)
  jsonapi_type = resource.config[:type]

  extra_field_names = resource.config[:extra_fields].keys.join(',')
  node.parameter do
    key :description, "<a href=\"https://jsonapi-suite.github.io/jsonapi_suite/how-to-conditionally-render-fields\">JSONAPI Extra Fields</a><br /><b>Possible Fields:</b> #{extra_field_names}"
    key :name, "extra_fields[#{jsonapi_type}]"
    key :in, :query
    key :type, :string
    key :required, false
  end
end

.jsonapi_fields(node, jsonapi_type) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/jsonapi_swagger_helpers/util.rb', line 134

def self.jsonapi_fields(node, jsonapi_type)
  node.parameter do
    key :description, '<a href="http://jsonapi.org/format/#fetching-sparse-fieldsets">JSONAPI Sparse Fieldset</a>'
    key :name, "fields[#{jsonapi_type}]"
    key :in, :query
    key :type, :string
    key :required, false
  end
end

.jsonapi_filter(node, label) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/jsonapi_swagger_helpers/util.rb', line 76

def self.jsonapi_filter(node, label)
  node.parameter do
    key :description, '<a href="http://jsonapi.org/format/#fetching-filtering">JSONAPI Filter</a>'
    key :name, label
    key :in, :query
    key :type, :string
    key :required, false
  end
end

.jsonapi_includes(node) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/jsonapi_swagger_helpers/util.rb', line 124

def self.jsonapi_includes(node)
  node.parameter do
    key :description, '<a href="http://jsonapi.org/format/#fetching-includes">JSONAPI Includes</a>'
    key :name, :include
    key :in, :query
    key :type, :string
    key :required, false
  end
end

.jsonapi_pagination(node) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/jsonapi_swagger_helpers/util.rb', line 96

def self.jsonapi_pagination(node)
  node.parameter do
    key :description, '<a href="http://jsonapi.org/format/#fetching-pagination">JSONAPI Page Size</a>'
    key :name, "page[size]"
    key :in, :query
    key :type, :string
    key :required, false
  end

  node.parameter do
    key :description, '<a href="http://jsonapi.org/format/#fetching-pagination">JSONAPI Page Number</a>'
    key :name, "page[number]"
    key :in, :query
    key :type, :string
    key :required, false
  end
end

.jsonapi_sorting(node) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/jsonapi_swagger_helpers/util.rb', line 86

def self.jsonapi_sorting(node)
  node.parameter do
    key :description, '<a href="http://jsonapi.org/format/#fetching-sorting">JSONAPI Sorting</a>'
    key :name, :sort
    key :in, :query
    key :type, :string
    key :required, false
  end
end

.jsonapi_stat(node, name, calculations) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/jsonapi_swagger_helpers/util.rb', line 114

def self.jsonapi_stat(node, name, calculations)
  node.parameter do
    key :name, "stats[#{name}]"
    key :description, "<a href=\"https://jsonapi-suite.github.io/jsonapi_suite/how-to-return-statistics\">JSONAPI Stats</a><br /><b>Possible Calculations:</b> #{calculations}"
    key :in, :query
    key :type, :string
    key :required, false
  end
end

.payload_tags_for(resource, include_hash) ⇒ Object



72
73
74
# File 'lib/jsonapi_swagger_helpers/util.rb', line 72

def self.payload_tags_for(resource, include_hash)
  payloads_for(resource, include_hash).map { |p| "payload-#{p.name}" }
end

.payloads_for(resource, include_hash) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jsonapi_swagger_helpers/util.rb', line 53

def self.payloads_for(resource, include_hash)
  [].tap do |payloads|
    payloads << JsonapiSpecHelpers::Payload.by_type(resource.config[:type])

    include_hash.each_pair do |name, nested|
      sideload = resource.sideloading.sideloads[name]

      if sideload.polymorphic?
        sideload.polymorphic_groups.each_pair do |type, sl|
          payloads << payloads_for(sl.resource_class, nested)
        end
      else
        sideload_resource = sideload.resource_class
        payloads << payloads_for(sideload_resource, nested)
      end
    end
  end.flatten.uniq(&:name)
end

.sideload_label(include_directive) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/jsonapi_swagger_helpers/util.rb', line 9

def self.sideload_label(include_directive)
  sideloads = include_directive.to_string.split(",").sort.join(",<br/>")

  <<-HTML
  <label>
Possible sideloads:
<span class="possible-sideloads">#{sideloads}</span>
  </label>
  HTML
end