Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active August 29, 2015 14:21
Show Gist options
  • Save Integralist/58b25f860773d8d2dd3f to your computer and use it in GitHub Desktop.
Save Integralist/58b25f860773d8d2dd3f to your computer and use it in GitHub Desktop.
Create Elections style content in DynamoDB and S3 and view is via Spurious Browser
require "aws-sdk-v1"
require "yaml"
require "spurious/ruby/awssdk/helper"
# CONFIGURE FOR SPURIOUS
Spurious::Ruby::Awssdk::Helper.configure
AWS.config(
:access_key_id => "development_access",
:secret_access_key => "development_secret",
:region => "eu-west-1"
)
# CREATE DYNAMO TABLES
sequencer_schema = YAML::load_file "schema/sequencer.yaml"
lookup_schema = YAML::load_file "schema/lookup.yaml"
ddb = AWS::DynamoDB::Client::V20120810.new
ddb.create_table sequencer_schema
ddb.create_table lookup_schema
# CREATE S3 CONTENTS
# /<bucket>/<event>/<env>/<format>/<queue>/<component>/<hash>/<version>
s3 = AWS::S3.new
bucket = s3.buckets.create("private-bucket")
hash = "7e0c33c476b1089500d5f172102ec03e"
lookup = "queue-{letter}/component-{letter}/#{hash}/"
path = "big-event/live/html/#{lookup}"
def create_object(b, p, l, n)
b.objects[p].write(
"<p>html content for letter: #{l} and number: #{n}</p>",
{
:content_type => "text/html"
}
)
end
def create_sequence_item(ddb, k, v)
# Sequencer...
#
# key: <component>/<hash>
# value: <sequence_version>
ddb.put_item(
:table_name => "table_sequencer",
:item => {
"key" => { "S" => k },
"value" => { "N" => v }
}
)
end
def create_lookup_item(ddb, k, v, l)
# Lookup...
#
# component_key: <component>/<hash>
# batch_version: <sequence_version>
# location: <queue>/<component>/<hash>/<sequence_version>
ddb.put_item(
:table_name => "table_lookup",
:item => {
"component_key" => { "S" => k },
"batch_version" => { "N" => v },
"location" => { "S" => l }
}
)
end
("a".."z").to_a.each do |letter|
p = path.gsub(/{letter}/, letter);
sequence_key = "component-{letter}/#{hash}".gsub(/{letter}/, letter)
lookup_key = "component-{letter}/#{hash}".gsub(/{letter}/, letter)
lookup_location = lookup.gsub(/{letter}/, letter)
["1", "2"].each do |n|
create_sequence_item(ddb, sequence_key, n)
create_lookup_item(ddb, lookup_key, n, lookup_location.gsub(/$/, n))
create_object(bucket, p.gsub(/$/, n), letter, n)
end
end
:attribute_definitions:
-
:attribute_name: batch_version
:attribute_type: N
-
:attribute_name: component_key
:attribute_type: S
:table_name: table_lookup
:key_schema:
-
:attribute_name: component_key
:key_type: HASH
-
:attribute_name: batch_version
:key_type: RANGE
:provisioned_throughput:
:read_capacity_units: 10
:write_capacity_units: 10
:attribute_definitions:
-
:attribute_name: key
:attribute_type: S
:table_name: table_sequencer
:key_schema:
-
:attribute_name: key
:key_type: HASH
:provisioned_throughput:
:read_capacity_units: 10
:write_capacity_units: 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment