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_label(include_directive) ⇒ Object
Class Method Details
.all_resources(resource, include_directive, memo = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 47 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 Rails.logger.error("JsonapiSwaggerHelpers: No controller found for #{path}!") unless route end end |
.each_filter(resource, association_name = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 37 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 |
# 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 end |
.id_in_url(node) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 174 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
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 57 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
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 161 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
151 152 153 154 155 156 157 158 159 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 151 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
93 94 95 96 97 98 99 100 101 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 93 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
141 142 143 144 145 146 147 148 149 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 141 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
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 113 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
103 104 105 106 107 108 109 110 111 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 103 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
131 132 133 134 135 136 137 138 139 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 131 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
89 90 91 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 89 def self.(resource, include_hash) payloads_for(resource, include_hash).map { |p| "payload-#{p.name}" } end |
.payloads_for(resource, include_hash) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 70 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 |
.rails_route(path, method) ⇒ Object
22 23 24 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 22 def self.rails_route(path, method) Rails.application.routes.recognize_path(path) rescue nil end |
.sideload_label(include_directive) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/jsonapi_swagger_helpers/util.rb', line 26 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 |