class Puuko::Endpoint

Constants

CALLBACK_KINDS
DEFAULT_HEADERS

Attributes

params[R]
request[R]
response[R]

Public Class Methods

new(request, params) click to toggle source
# File lib/puuko/endpoint.rb, line 56
def initialize(request, params)
  @request = request
  @params = params
  @response = Puuko::Response.new(500, DEFAULT_HEADERS, {})
  @halted = false
end
register_after_callback(&block) click to toggle source
# File lib/puuko/endpoint.rb, line 47
def register_after_callback(&block)
  register_callback(:after, &block)
end
register_before_callback(&block) click to toggle source
# File lib/puuko/endpoint.rb, line 43
def register_before_callback(&block)
  register_callback(:before, &block)
end
register_callback(kind, &block) click to toggle source
# File lib/puuko/endpoint.rb, line 29
def register_callback(kind, &block)
  unless Puuko::Endpoint::CALLBACK_KINDS.include?(kind)
    raise InvalidCallbackKind, "Received invalid callback kind: #{kind}"
  end

  registered_callbacks[kind] << block if block_given?
end
register_rescue_callback(exception_class, &block) click to toggle source
# File lib/puuko/endpoint.rb, line 37
def register_rescue_callback(exception_class, &block)
  register_callback(:rescue) do |e|
    self.instance_exec(&block) if e.is_a?(exception_class)
  end
end
registered_callbacks() click to toggle source
# File lib/puuko/endpoint.rb, line 14
def registered_callbacks
  @registered_callbacks ||= begin
    hash = {}
    CALLBACK_KINDS.each do |kind|
      hash[kind] = []
      if self.superclass.respond_to?(:registered_callbacks)
        superclass.registered_callbacks[kind].each { |cb| hash[kind] << cb }
      end
    end
    hash
  end

  Hash[@registered_callbacks]
end

Public Instance Methods

body() click to toggle source
# File lib/puuko/endpoint.rb, line 78
def body
  @json ||= read_request_body do |data|
    JSON.parse(data, symbolize_names: false)
  rescue JSON::ParserError
    {}
  end
end
execute() click to toggle source
# File lib/puuko/endpoint.rb, line 63
def execute
  result = nil

  begin
    execute_callbacks(:before)
    result = handle unless halted?
    execute_callbacks(:after)
  rescue RuntimeError, StandardError => e
    execute_callbacks(:rescue, e)
    raise e unless halted?
  end

  result
end

Protected Instance Methods

halt!(status: 200, headers: DEFAULT_HEADERS, body: {}) click to toggle source
# File lib/puuko/endpoint.rb, line 86
          def halt!(status: 200, headers: DEFAULT_HEADERS, body: {})
  @response = Puuko::Response.new(status, DEFAULT_HEADERS.merge(headers), body)
  @halted = true
end
halted?() click to toggle source
# File lib/puuko/endpoint.rb, line 91
          def halted?
  @halted
end
redirect_to(location) click to toggle source
# File lib/puuko/endpoint.rb, line 95
          def redirect_to(location)
  render status: 302, headers: { "Location" => location }
end
render(status: 200, headers: DEFAULT_HEADERS, body: {}) click to toggle source
# File lib/puuko/endpoint.rb, line 99
          def render(status: 200, headers: DEFAULT_HEADERS, body: {})
  @response = Puuko::Response.new(status, DEFAULT_HEADERS.merge(headers), body)
end

Private Instance Methods

execute_callbacks(kind, *args) click to toggle source
# File lib/puuko/endpoint.rb, line 103
        def execute_callbacks(kind, *args)
  return if halted?

  unless Puuko::Endpoint::CALLBACK_KINDS.include?(kind)
    raise InvalidCallbackKind, "Received invalid callback kind: #{kind}"
  end

  self.class.registered_callbacks[kind].each do |callback|
    self.instance_exec(*args, &callback)
    break if halted?
  end
end
read_request_body() { |body.read| ... } click to toggle source
# File lib/puuko/endpoint.rb, line 116
        def read_request_body
  request.body.rewind # in case someone already read it
  result = yield(request.body.read)
  request.body.rewind # to allow others read it if they want
  result
end