Module: Grape::Formatter::JSONAPIResources
- Defined in:
- lib/grape-jsonapi-resources/formatter.rb
Class Method Summary collapse
- .build_options_from_endpoint(endpoint) ⇒ Object
- .call(resource, env) ⇒ Object
- .get_resource_for(klass) ⇒ Object
- .resource_class_for(resource) ⇒ Object
- .resources_cache ⇒ Object
- .serialize_resource(resource, env) ⇒ Object
Class Method Details
.build_options_from_endpoint(endpoint) ⇒ Object
55 56 57 58 59 |
# File 'lib/grape-jsonapi-resources/formatter.rb', line 55 def (endpoint) = {} [:base_url] = endpoint.namespace_inheritable(:jsonapi_base_url) if endpoint.namespace_inheritable(:jsonapi_base_url) end |
.call(resource, env) ⇒ Object
5 6 7 |
# File 'lib/grape-jsonapi-resources/formatter.rb', line 5 def call(resource, env) serialize_resource(resource, env) || Grape::Formatter::Json.call(resource, env) end |
.get_resource_for(klass) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/grape-jsonapi-resources/formatter.rb', line 75 def get_resource_for(klass) resources_cache.fetch_or_store(klass) do resource_class_name = "#{klass.name}Resource" resource_class = resource_class_name.safe_constantize if resource_class resource_class elsif klass.superclass get_resource_for(klass.superclass) end end end |
.resource_class_for(resource) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/grape-jsonapi-resources/formatter.rb', line 61 def resource_class_for(resource) if resource.class.respond_to?(:jsonapi_resource_class) resource.class.jsonapi_resource_class elsif resource.respond_to?(:to_ary) resource_class_for(resource.to_ary.first) else get_resource_for(resource.class) end end |
.resources_cache ⇒ Object
71 72 73 |
# File 'lib/grape-jsonapi-resources/formatter.rb', line 71 def resources_cache @resources_cache ||= ThreadSafe::Cache.new end |
.serialize_resource(resource, env) ⇒ Object
9 10 11 12 13 14 15 16 17 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 |
# File 'lib/grape-jsonapi-resources/formatter.rb', line 9 def serialize_resource(resource, env) endpoint = env['api.endpoint'] = (endpoint) .merge!(env['jsonapi_options'] || {}) context = {} context.merge!(current_user: endpoint.current_user) if endpoint.respond_to?(:current_user) context.merge!(.delete(:context)) if [:context] resource_class = resource_class_for(resource) if resource_class.nil? if resource.blank? # Return blank object blank_return = {} if resource.respond_to?(:to_ary) blank_return[:data] = [] else blank_return[:data] = {} end blank_return[:meta] = [:meta] if [:meta] return blank_return.to_json else return nil end end resource_instances = nil if resource.respond_to?(:to_ary) resource_instances = resource.to_ary.collect do |each_resource| resource_class.new(each_resource, context) end else resource_instances = resource_class.new(resource, context) end resource_serialzer = JSONAPI::ResourceSerializer.new(resource_class, ).serialize_to_hash(resource_instances) if [:meta] # Add option to merge top level meta tag as jsonapi-resources does not appear to support this resource_serialzer.as_json.merge(meta: [:meta]).to_json if [:meta] else resource_serialzer.to_json end end |