Created
December 27, 2018 15:38
-
-
Save stympy/79e649d3ebc12773643b44415014d638 to your computer and use it in GitHub Desktop.
Get a list of things from DynamoDB and render them in Lambda using Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'aws-sdk-dynamodb' | |
require 'erb' | |
DB = Aws::DynamoDB::Client.new | |
Views = Hash[Dir['views/*'].map {|v| [ File.basename(v, '.html.erb').to_sym, ERB.new(File.read(v)) ] }] | |
def list(event:, context:) | |
@rows = DB.scan({ table_name: ENV['TABLE_NAME'] }).result.items | |
{ headers: { 'Content-type' => 'text/html' }, statusCode: 200, body: render { Views[:table].result(binding) } } | |
end | |
def render | |
Views[:layout].result(binding) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>A list</title> | |
</head> | |
<body> | |
<%= yield %> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>A List</h1> | |
<table> | |
<thead> | |
<tr> | |
<th>Name</th> | |
</tr> | |
</thead> | |
<tbody> | |
<% @rows.each do |row| %> | |
<tr> | |
<td><%= row['name'] %></td> | |
</tr> | |
<% end %> | |
</tbody> | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment