Skip to content

Instantly share code, notes, and snippets.

@hungryblank
Created May 14, 2009 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hungryblank/111897 to your computer and use it in GitHub Desktop.
Save hungryblank/111897 to your computer and use it in GitHub Desktop.
Short code tutorial for the aws_sdb_bare gem
#http://github.com/hungryblank/aws_sdb_bare
#
#remember to install the gems!!
#sudo gem install hungryblank-aws_sdb_bare -s http://gems.github.com
#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 = AwsSdb::Request::CreateDomain.new(:name => '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
#request.
#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 = ListDomains.new
response = AwsSdb::Response.parse(open(request.uri))
puts 'these are the domains'
p response.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 = PutAttributes.new(:domain => '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 = GetAttributes.new(:domain => '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 = PutAttributes.new(:domain => 'aws_sdb_bare_test', :name => 'element_1', :attributes => {:color => 'blue'})
open request.uri
p Response.parse(open GetAttributes.new(:domain => '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 = PutAttributes.new(:domain => 'aws_sdb_bare_test', :name => 'element_1')
request.attributes = {:color => {:value => 'blue', :replace => true}}
open request.uri
#mmm how does it look now?
request = GetAttributes.new(:domain => '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 PutAttributes.new(:name => 'element_1', :domain => 'aws_sdb_bare_test',
:attributes => {:color => 'red'}).uri
open PutAttributes.new(:name => 'element_2', :domain => 'aws_sdb_bare_test',
:attributes => {:shape => 'triangle', :color => 'green'}).uri
open PutAttributes.new(:name => 'element_3', :domain => 'aws_sdb_bare_test',
:attributes => {:shape => 'triangle', :color => 'blue'}).uri
puts "our first select on SimpleDB!!"
select = Select.new(:query => "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}"
end
#clean up our mess?
#request = DeleteDomain.new(:name => '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