Sha256: 53bbc901ea28ded706ae40045114e2faabac5dfaede0b723daf5c3c52193e260

Contents?: true

Size: 1.73 KB

Versions: 7

Compression:

Stored size: 1.73 KB

Contents

# encoding: utf-8

module Hyperdrive
  class Endpoint

    def self.call(env)
      @env = env
      @request = Rack::Request.new(@env)
      @media_type = env['hyperdrive.media_type']
      @params = env['hyperdrive.params']
      @resource = env['hyperdrive.resource']
      @headers = Hyperdrive::Values.default_headers.dup
      @headers.merge!('Allow' => resource.allowed_methods.join(', '), 'Content-Type' => @media_type)

      response.finish
    end

    private

    class << self
      attr_reader   :env, :request, :media_type, :params, :resource
      attr_accessor :headers
    end

    def self.json?
      media_type =~ /json$/
    end

    def self.xml?
      media_type =~ /xml$/
    end

    def self.page
      env['hyperdrive.page']
    end

    def self.per_page
      env['hyperdrive.per_page']
    end

    def self.render(body)
      case body
      when Array, Hash
        if json?
          MultiJson.dump(body)
        else
          env.logger.error "ENDPOINT: Can't serialize response automatically"
          raise Errors::NoResponse.new
        end
      when String
        body
      else
        env.logger.debug "ENDPOINT: Coerceing response to string. Probably not what you want"
        body.to_s
      end
    end

    def self.error(status, message)
      raise Errors::HTTPError.new(message, status)
    end

    def self.status
      case env['REQUEST_METHOD']
      when 'POST'
        201
      when 'DELETE'
        204
      else
        200
      end
    end

    def self.body
      body = instance_eval(&resource.request_handler(env['REQUEST_METHOD']))
      body = '' if env['REQUEST_METHOD'] == 'DELETE'
      body
    end

    def self.response
      ::Rack::Response.new(render(body), status, headers)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hyperdrive-0.0.15 lib/hyperdrive/endpoint.rb
hyperdrive-0.0.14 lib/hyperdrive/endpoint.rb
hyperdrive-0.0.13 lib/hyperdrive/endpoint.rb
hyperdrive-0.0.12 lib/hyperdrive/endpoint.rb
hyperdrive-0.0.11 lib/hyperdrive/endpoint.rb
hyperdrive-0.0.10 lib/hyperdrive/endpoint.rb
hyperdrive-0.0.9 lib/hyperdrive/endpoint.rb