module Yake::API::DSL

Public Instance Methods

any(path, &block) click to toggle source

Define ANY route

# File lib/yake/api.rb, line 60
def any(path, &block)
  define_singleton_method("ANY #{ path }") { |*args| instance_exec(*args, &block) }
end
delete(path, &block) click to toggle source

Define DELETE route

# File lib/yake/api.rb, line 66
def delete(path, &block)
  define_singleton_method("DELETE #{ path }") { |*args| instance_exec(*args, &block) }
end
get(path, &block) click to toggle source

Define GET route

# File lib/yake/api.rb, line 72
def get(path, &block)
  define_singleton_method("GET #{ path }") { |*args| instance_exec(*args, &block) }
end
head(path, &block) click to toggle source

Define HEAD route

# File lib/yake/api.rb, line 78
def head(path, &block)
  define_singleton_method("HEAD #{ path }") { |*args| instance_exec(*args, &block) }
end
header(headers) click to toggle source

Set default header

# File lib/yake/api.rb, line 54
def header(headers)
  (@headers ||= {}).update(headers)
end
options(path, &block) click to toggle source

Define OPTIONS route

# File lib/yake/api.rb, line 84
def options(path, &block)
  define_singleton_method("OPTIONS #{ path }") { |*args| instance_exec(*args, &block) }
end
patch(path, &block) click to toggle source

Define PATCH route

# File lib/yake/api.rb, line 90
def patch(path, &block)
  define_singleton_method("PATCH #{ path }") { |*args| instance_exec(*args, &block) }
end
post(path, &block) click to toggle source

Define POST route

# File lib/yake/api.rb, line 96
def post(path, &block)
  define_singleton_method("POST #{ path }") { |*args| instance_exec(*args, &block) }
end
put(path, &block) click to toggle source

Define PUT route

# File lib/yake/api.rb, line 102
def put(path, &block)
  define_singleton_method("PUT #{ path }") { |*args| instance_exec(*args, &block) }
end
respond(status_code, body = nil, **headers) click to toggle source

Transform to API Gateway response

# File lib/yake/api.rb, line 34
def respond(status_code, body = nil, **headers)
  # Log response
  log = "RESPONSE [#{ status_code }] #{ body }"
  Yake.logger&.send(status_code.to_i >= 400 ? :error : :info, log)

  # Set headers
  content_length = (body&.length || 0).to_s
  to_s_downcase  = -> (key) { key.to_s.downcase }
  headers = {
    "content-length" => content_length,
    **(@headers || {}),
    **headers,
  }.transform_keys(&to_s_downcase).compact

  # Send response
  { statusCode: status_code, headers: headers, body: body }.compact
end
route(event, context = nil) { |res| ... } click to toggle source

Proxy handler for HTTP requests from Slack

# File lib/yake/api.rb, line 14
def route(event, context = nil, &block)
  # Extract route method
  method = event["routeKey"]
  raise Yake::Errors::UndeclaredRoute, method unless respond_to?(method)

  # Normalize headers
  event["headers"]&.transform_keys!(&:downcase)

  # Decode body if Base64-encoded
  if event["isBase64Encoded"]
    body = Base64.strict_decode64(event["body"])
    event.update("body" => body, "isBase64Encoded" => false)
  end

  # Execute request
  send(method, event, context).then { |res| block_given? ? yield(res) : res }
end