class Wallaby::JsonApiResponder

Easter egg: simple responder for JSON API

Public Instance Methods

to_html() click to toggle source

@see to_json

# File lib/responders/wallaby/json_api_responder.rb, line 9
def to_html
  to_json
end
to_json(*) click to toggle source

@return [String] JSON

# File lib/responders/wallaby/json_api_responder.rb, line 14
def to_json(*)
  json_options = { content_type: 'application/vnd.api+json', status: options[:status] }
  if exception?
    render json_options.merge(json: exception_details)
  else
    render json_options.merge(action_options)
  end
end

Protected Instance Methods

action_options() click to toggle source
# File lib/responders/wallaby/json_api_responder.rb, line 25
def action_options
  if !get? && has_errors?
    { json: resource_errors, status: :unprocessable_entity }
  elsif index?
    { json: collection_data }
  else
    { json: resource_data }
  end
end
attributes_of(resource) click to toggle source
# File lib/responders/wallaby/json_api_responder.rb, line 74
def attributes_of(resource)
  decorated = controller.decorate resource
  field_names = index? ? decorated.index_field_names : decorated.show_field_names
  field_names.each_with_object({}) do |name, attributes|
    attributes[name] = decorated.public_send name
  end
end
collection_data() click to toggle source
# File lib/responders/wallaby/json_api_responder.rb, line 43
def collection_data
  {
    data: resource.map(&method(:single)),
    links: {
      self: controller.url_for(resources: params[:resources], action: 'index')
    }
  }
end
exception?() click to toggle source
# File lib/responders/wallaby/json_api_responder.rb, line 97
def exception?
  (resource.nil? || resource.is_a?(Exception)) && options[:template] == ERROR_PATH
end
exception_details() click to toggle source
# File lib/responders/wallaby/json_api_responder.rb, line 82
def exception_details
  {
    errors: [
      {
        status: options[:status],
        detail: resource.try(:message)
      }
    ]
  }
end
index?() click to toggle source
# File lib/responders/wallaby/json_api_responder.rb, line 93
def index?
  params[:action] == 'index'
end
resource_data() click to toggle source
# File lib/responders/wallaby/json_api_responder.rb, line 52
def resource_data
  {
    data: single(resource),
    links: {
      self: controller.url_for(resources: params[:resources], action: 'show', id: resource.id)
    }
  }
end
resource_errors() click to toggle source
# File lib/responders/wallaby/json_api_responder.rb, line 61
def resource_errors
  decorated = controller.decorate resource
  {
    errors: decorated.errors.each_with_object([]) do |(field, message), json|
      json.push(
        status: 422,
        source: { pointer: "/data/attributes/#{field}" },
        detail: message
      )
    end
  }
end
single(resource) click to toggle source
# File lib/responders/wallaby/json_api_responder.rb, line 35
def single(resource)
  {
    id: resource.id,
    type: params[:resources],
    attributes: attributes_of(resource)
  }
end