Create a gist now

Instantly share code, notes, and snippets.

Short code tutorial for the aws_sdb_bare gem
#remember to install the gems!!
#sudo gem install hungryblank-aws_sdb_bare -s
#sudo gem install open-uri
#The first thing you probably want to do on SimpleDb is creating a domain.
#The domain is a container for SimpleDB records.
#let's require the gems we need
require 'rubygems'
require 'aws_sdb_bare'
require 'open-uri'
require 'nokogiri'
#you can instead require hpricot if you prefer!
#require 'hpricot'
#store in the environment your AWS credentials or uncomment
#the following
#ENV['AMAZON_ACCESS_KEY_ID'] = 'your_amazon_access_key'
#ENV['AMAZON_SECRET_ACCESS_KEY'] = 'your_amazon_secret'
#We're ready to create the first request
request = => 'aws_sdb_bare_test')
#Please note, so far we haven't sent yet the request over to SimpleDB
#we just have an object which contains all the data needed to create the
#The uri of the actual http request (that in SimpleDB is always a GET),
#can be inspected using the #uri method fo the request
puts "this is the create domain request uri"
puts request.uri
#ok, now it's time to actually send the request to AWS
puts "and this is the response!"
puts open(request.uri).read
#If everything went well you shoud see on screen an XML document.
#Looking at XML documents is not that great and that's why aws_sdb_bare provides
#response parsing.
#to save our fingers we include some modules
include AwsSdb
include AwsSdb::Request
#Let's ask SimpleDB to the list of our domains.
request =
response = AwsSdb::Response.parse(open(request.uri))
puts 'these are the domains'
puts 'and this is the box usage'
p response.metadata.box_usage
#Now we can see the domain we created in the previous chunk of code without
#having to read raw XML, and in addition we've got some information about our
#request. Every SimpleDb request comes with some metadata attached, the box
#usage is relevant data since is one of the parameters that will determine
#how much your're going to pay.
#It's time to make some use of the domain populating it
request = => 'aws_sdb_bare_test', :name => 'element_1')
request.attributes = {:shape => 'square', :color => 'green'}
puts open(request.uri).read
#We inserted our first element, to see what the element looks like we can do
#the following
request = => 'aws_sdb_bare_test', :name => 'element_1')
response = Response.parse(open(request.uri))
puts "w00t our first element!!"
p response.attributes
#It's more or less what we specified but note that all keys are strings and
#all the values are array of strings, this is how SimpleDB internally
#stores the data and the response reflects this.
#Let's do something more with elements
puts "let's add something to it"
request = => 'aws_sdb_bare_test', :name => 'element_1', :attributes => {:color => 'blue'})
open request.uri
p Response.parse(open => 'aws_sdb_bare_test', :name => 'element_1').uri).attributes
#Here comes a surprise, instead of overrinding the previous 'color' we've added
#a color to our element_1, to override the original value the request should be
#the following
puts "and tweak it again!"
request = => 'aws_sdb_bare_test', :name => 'element_1')
request.attributes = {:color => {:value => 'blue', :replace => true}}
open request.uri
#mmm how does it look now?
request = => 'aws_sdb_bare_test', :name => 'element_1')
xml_response = open request.uri
p Response.parse(xml_response).attributes
#Please note when defining attributes of a PutAtttribute is allowed to mix
#attributes to replace and not.
#Let's finish this walk through
puts "and here comes the second"
open => 'element_1', :domain => 'aws_sdb_bare_test',
:attributes => {:color => 'red'}).uri
open => 'element_2', :domain => 'aws_sdb_bare_test',
:attributes => {:shape => 'triangle', :color => 'green'}).uri
open => 'element_3', :domain => 'aws_sdb_bare_test',
:attributes => {:shape => 'triangle', :color => 'blue'}).uri
puts "our first select on SimpleDB!!"
select = => "SELECT * FROM aws_sdb_bare_test WHERE color = 'blue'")
response = Response.parse(open(select.uri))
response.items.each do |name, attributes|
puts "#{name} has attributes #{attributes.inspect}"
#clean up our mess?
#request = => 'aws_sdb_bare_test')
#open request.uri
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment