Class: JsonapiSwaggerHelpers::Util
- Inherits:
-
Object
- Object
- JsonapiSwaggerHelpers::Util
- Defined in:
- lib/jsonapi_swagger_helpers/util.rb
Class Method Summary collapse
- .all_resources(resource, include_directive, memo = {}) ⇒ Object
- .controller_for(path) ⇒ Object
- .each_filter(resource, association_name = nil) ⇒ Object
- .find_route(path) ⇒ Object
- .id_in_url(node) ⇒ Object
- .include_directive_for(controller, action) ⇒ Object
- .jsonapi_extra_fields(node, resource) ⇒ Object
- .jsonapi_fields(node, jsonapi_type) ⇒ Object
- .jsonapi_filter(node, label) ⇒ Object
- .jsonapi_includes(node) ⇒ Object
- .jsonapi_pagination(node) ⇒ Object
- .jsonapi_sorting(node) ⇒ Object
- .jsonapi_stat(node, name, calculations) ⇒ Object
- .payload_tags_for(resource, include_hash) ⇒ Object
- .payloads_for(resource, include_hash) ⇒ Object
- .rails_route(path, method) ⇒ Object
- .sideload_hash(sideload, processed = []) ⇒ Object
- .sideload_label(include_directive) ⇒ Object
Class Method Details
.all_resources(resource, include_directive, memo = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 49 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 8 9 10 11 12 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 3 def self.controller_for(path) path = path.sub('{id}', '1') route = find_route(path) if route "#{route[:controller]}_controller".classify.constantize else raise ArgumentError.new("JsonapiSwaggerHelpers: No controller found for #{path}!") end end |
.each_filter(resource, association_name = nil) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 39 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 |
.find_route(path) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 14 def self.find_route(path) route = rails_route(path, 'GET') route ||= rails_route(path, 'POST') route ||= rails_route(path, 'PUT') route ||= rails_route(path, 'DELETE') route ||= rails_route("#{path}/1", 'PUT') route ||= rails_route("#{path}/1", 'DELETE') route end |
.id_in_url(node) ⇒ Object
191 192 193 194 195 196 197 198 199 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 191 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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 72 def self.include_directive_for(controller, action) resource_class = controller._jsonapi_compliable resource_class = resource_class[action] if resource_class.is_a?(Hash) includes = sideload_hash(resource_class.sideloading)[:base] whitelist = controller._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
178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 178 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
168 169 170 171 172 173 174 175 176 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 168 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
110 111 112 113 114 115 116 117 118 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 110 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
158 159 160 161 162 163 164 165 166 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 158 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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 130 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
120 121 122 123 124 125 126 127 128 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 120 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
148 149 150 151 152 153 154 155 156 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 148 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
106 107 108 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 106 def self.(resource, include_hash) payloads_for(resource, include_hash).map { |p| "payload-#{p.name}" } end |
.payloads_for(resource, include_hash) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 87 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.all_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 |
.rails_route(path, method) ⇒ Object
24 25 26 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 24 def self.rails_route(path, method) Rails.application.routes.recognize_path(path, method: method) rescue nil end |
.sideload_hash(sideload, processed = []) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 59 def self.sideload_hash(sideload, processed = []) if processed.select { |p| p == sideload.name }.length == 1 return { sideload.name => {} } end processed << sideload.name { sideload.name => {} }.tap do |hash| sideload.all_sideloads.each_pair do |key, sl| hash[sideload.name][key] = sideload_hash(sl, processed)[key] || {} end end end |
.sideload_label(include_directive) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 28 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 |