Module: Yake::API::DSL

Defined in:
lib/yake/api.rb

Instance Method Summary collapse

Instance Method Details

#any(path, &block) ⇒ Object

Define ANY route


60
61
62
# 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) ⇒ Object

Define DELETE route


66
67
68
# 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) ⇒ Object

Define GET route


72
73
74
# 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) ⇒ Object

Define HEAD route


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

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

#header(headers) ⇒ Object

Set default header


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

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

#options(path, &block) ⇒ Object

Define OPTIONS route


84
85
86
# 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) ⇒ Object

Define PATCH route


90
91
92
# 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) ⇒ Object

Define POST route


96
97
98
# 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) ⇒ Object

Define PUT route


102
103
104
# 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) ⇒ Object

Transform to API Gateway response


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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&.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


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/yake/api.rb', line 13

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 = event['body'].unpack1('m0')
    event.update('body' => body, 'isBase64Encoded' => false)
  end

  # Execute request
  res = send(method, event, context)
  block_given? ? yield(res) : res
end