Sha256: 8d6e19ef0d51bb95305f6e43642d6524e1ef5c88531a1ed2fbd7de249bfaf7ea

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require 'delegate'
require 'active_support/core_ext/object/blank'

module JSONAPIonify::Api
  class Server
    extend JSONAPIonify::Autoload
    autoload_all

    attr_reader :api

    def initialize(api)
      @api = api
    end

    def call(env)
      Processor.new(env, api).response
    end

    class Processor
      attr_reader :request, :id, :more, :api

      def initialize(env, api)
        @api     = api
        @request = Request.new(env)
        request.path_info.split('/').tap(&:shift).tap do |parts|
          @resource, @id, @relationship, @relationship_name, *@more = parts
          request.env['jsonapionify.resource_name']                 = @resource if @resource
          request.env['jsonapionify.resource']                      = resource if @resource
          request.env['jsonapionify.id']                            = @id if @id
        end
      end

      def response
        @resource ? resource.process(request) : api_index
      rescue Errors::ResourceNotFound
        api.http_error(:not_found, request)
      end

      private

      def api_index
        api.process_index(request)
      end

      def resource
        api.resource(@resource)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jsonapionify-0.9.0 lib/jsonapionify/api/server.rb