Module: Yake::API::DSL

Defined in:
lib/yake/api.rb

Instance Method Summary collapse

Instance Method Details

#any(path, &block) ⇒ Object

Define ANY route



61
62
63
# File 'lib/yake/api.rb', line 61

def any(path, &block)
  define_singleton_method("ANY #{ path }") { |*args| instance_exec(*args, &block) }
end

#delete(path, &block) ⇒ Object

Define DELETE route



67
68
69
# File 'lib/yake/api.rb', line 67

def delete(path, &block)
  define_singleton_method("DELETE #{ path }") { |*args| instance_exec(*args, &block) }
end

#get(path, &block) ⇒ Object

Define GET route



73
74
75
# File 'lib/yake/api.rb', line 73

def get(path, &block)
  define_singleton_method("GET #{ path }") { |*args| instance_exec(*args, &block) }
end

#head(path, &block) ⇒ Object

Define HEAD route



79
80
81
# File 'lib/yake/api.rb', line 79

def head(path, &block)
  define_singleton_method("HEAD #{ path }") { |*args| instance_exec(*args, &block) }
end

#header(headers) ⇒ Object

Set default header



55
56
57
# File 'lib/yake/api.rb', line 55

def header(headers)
  (@headers ||= {}).update(headers)
end

#options(path, &block) ⇒ Object

Define OPTIONS route



85
86
87
# File 'lib/yake/api.rb', line 85

def options(path, &block)
  define_singleton_method("OPTIONS #{ path }") { |*args| instance_exec(*args, &block) }
end

#patch(path, &block) ⇒ Object

Define PATCH route



91
92
93
# File 'lib/yake/api.rb', line 91

def patch(path, &block)
  define_singleton_method("PATCH #{ path }") { |*args| instance_exec(*args, &block) }
end

#post(path, &block) ⇒ Object

Define POST route



97
98
99
# File 'lib/yake/api.rb', line 97

def post(path, &block)
  define_singleton_method("POST #{ path }") { |*args| instance_exec(*args, &block) }
end

#put(path, &block) ⇒ Object

Define PUT route



103
104
105
# File 'lib/yake/api.rb', line 103

def put(path, &block)
  define_singleton_method("PUT #{ path }") { |*args| instance_exec(*args, &block) }
end

#respond(status_code, body = nil, **headers) ⇒ Object

Transform to API Gateway response



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/yake/api.rb', line 35

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&.bytesize || 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, &block) ⇒ Object

Proxy handler for HTTP requests from Slack



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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
  res = send(method, event, context)
  block_given? ? yield(res) : res
end