Module: Yake::API::DSL
- Defined in:
- lib/yake/api.rb
Overview
Yake API DSL
Instance Method Summary collapse
-
#header(headers) ⇒ Object
Set default header.
-
#respond(status_code, body = nil, **headers) ⇒ Object
Transform to API Gateway response.
-
#route(event, context = nil) ⇒ Object
Proxy handler for HTTP requests from API Gateway.
Instance Method Details
#header(headers) ⇒ Object
Set default header
56 57 58 |
# File 'lib/yake/api.rb', line 56 def header(headers) (@headers ||= {}).update(headers) end |
#respond(status_code, body = nil, **headers) ⇒ Object
Transform to API Gateway response
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/yake/api.rb', line 36 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:, body: }.compact end |
#route(event, context = nil) ⇒ Object
Proxy handler for HTTP requests from API Gateway
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/yake/api.rb', line 15 def route(event, context = nil) # 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 |